You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

templates.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package httplistener
  2. import (
  3. "bytes"
  4. "errors"
  5. "github.com/irccloud/irccat/util"
  6. "gopkg.in/go-playground/webhooks.v5/github"
  7. "strings"
  8. "text/template"
  9. )
  10. var defaultTemplates = map[string]string{
  11. "github.release": "[{{b .Repository.Name}}] release {{h .Release.TagName}} has been published by {{g .Release.Author.Login}}: {{.Release.HTMLURL}}",
  12. "github.push": `[{{b .Repository.Name}}] {{g .Sender.Login}} pushed {{.Commits|len}} commits to {{.Ref|refType}} {{.Ref|refName|h}}: {{.Compare}}
  13. {{range commitLimit . 3}}
  14. {{g .Username}} ({{.Sha|truncateSha|h}}): {{.Message}}
  15. {{end}}`,
  16. "github.issue": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{.Action}} issue #{{.Issue.Number}}: {{.Issue.Title}} {{.Issue.HTMLURL}}",
  17. "github.issuecomment": "[{{b .Repository.Name}}] {{g .Comment.User.Login}} commented on issue #{{.Issue.Number}}: {{trunc .Comment.Body 150}} {{.Issue.HTMLURL}}",
  18. "github.pullrequest": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{.Action}} pull request #{{.PullRequest.Number}} (\x0303{{.PullRequest.Base.Ref}}…{{.PullRequest.Head.Ref}}\x0f): {{.PullRequest.Title}} {{.PullRequest.HTMLURL}}",
  19. }
  20. func refName(ref string) string {
  21. parts := strings.Split(ref, "/")
  22. return parts[2]
  23. }
  24. func refType(ref string) string {
  25. parts := strings.Split(ref, "/")
  26. if parts[1] == "heads" {
  27. return "branch"
  28. } else if parts[1] == "tags" {
  29. return "tag"
  30. }
  31. return ""
  32. }
  33. func truncateSha(sha string) string {
  34. return sha[len(sha)-7:]
  35. }
  36. // Colour helper functions to try and declutter
  37. func boldFormat(text string) string {
  38. return "\x02" + text + "\x0f"
  39. }
  40. func greyFormat(text string) string {
  41. return "\x0314" + text + "\x0f"
  42. }
  43. func highlightFormat(text string) string {
  44. return "\x0303" + text + "\x0f"
  45. }
  46. func parseTemplates() *template.Template {
  47. funcMap := template.FuncMap{
  48. "trunc": util.Truncate,
  49. "truncateSha": truncateSha,
  50. "refType": refType,
  51. "refName": refName,
  52. "commitLimit": commitLimit,
  53. "b": boldFormat,
  54. "g": greyFormat,
  55. "h": highlightFormat,
  56. }
  57. t := template.New("irccat")
  58. for k, v := range defaultTemplates {
  59. template.Must(t.New(k).Funcs(funcMap).Parse(v))
  60. }
  61. return t
  62. }
  63. func (hl *HTTPListener) renderTemplate(tpl_name string, data interface{}) ([]string, error) {
  64. var out bytes.Buffer
  65. t := hl.tpls.Lookup(tpl_name)
  66. if t == nil {
  67. return []string{}, errors.New("Nonexistent template")
  68. }
  69. t.Execute(&out, data)
  70. return strings.Split(out.String(), "\n"), nil
  71. }
  72. // We need this additional struct and function because the GitHub webhook package represents
  73. // commits as anonymous inner structs and Go's type system is bad. Unless I'm missing something very obvious.
  74. type Commit struct {
  75. Message string
  76. Username string
  77. Sha string
  78. }
  79. func commitLimit(pl github.PushPayload, length int) []Commit {
  80. res := make([]Commit, 0)
  81. i := 0
  82. for _, c := range pl.Commits {
  83. if !c.Distinct {
  84. continue
  85. }
  86. res = append(res, Commit{Message: c.Message, Username: c.Author.Username, Sha: c.Sha})
  87. i += 1
  88. if i == length {
  89. break
  90. }
  91. }
  92. return res
  93. }