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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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}} {{if .Forced}}force-{{end}}{{if .Deleted}}deleted{{else}}pushed{{end}} {{if .Commits}}{{.Commits|len}} commit{{if .Commits|len|lt 1}}s{{end}} to {{end}}{{.Ref|refType}} {{.Ref|refName|h}}: {{.Compare}}
  13. {{range commitLimit . 3}}
  14. • {{g .Username}} ({{.Sha|truncateSha|h}}): {{trunc .Message 150}}
  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}} {{.Comment.HTMLURL}}",
  18. "github.pullrequest": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{if .PullRequest.Merged}}merged{{else}}{{.Action}}{{end}} pull request #{{.PullRequest.Number}} (\x0303{{.PullRequest.Base.Ref}}…{{.PullRequest.Head.Ref}}\x0f): {{.PullRequest.Title}} {{.PullRequest.HTMLURL}}",
  19. "prometheus.alert": `{{range .Alerts}}[{{b "Prometheus"}}] {{if (eq .Status "firing")}}{{b "alerting"}}{{else}}{{h "resolved"}}{{end}}: {{.Annotations.summary}}
  20. {{end}}`,
  21. }
  22. func refName(ref string) string {
  23. parts := strings.Split(ref, "/")
  24. return parts[2]
  25. }
  26. func refType(ref string) string {
  27. parts := strings.Split(ref, "/")
  28. if parts[1] == "heads" {
  29. return "branch"
  30. } else if parts[1] == "tags" {
  31. return "tag"
  32. }
  33. return ""
  34. }
  35. func truncateSha(sha string) string {
  36. if len(sha) < 8 {
  37. return ""
  38. }
  39. return sha[:8]
  40. }
  41. // Colour helper functions to try and declutter
  42. func boldFormat(text string) string {
  43. return "\x02" + text + "\x0f"
  44. }
  45. func greyFormat(text string) string {
  46. return "\x0314" + text + "\x0f"
  47. }
  48. func highlightFormat(text string) string {
  49. return "\x0303" + text + "\x0f"
  50. }
  51. func parseTemplates() *template.Template {
  52. funcMap := template.FuncMap{
  53. "trunc": util.Truncate,
  54. "truncateSha": truncateSha,
  55. "refType": refType,
  56. "refName": refName,
  57. "commitLimit": commitLimit,
  58. "b": boldFormat,
  59. "g": greyFormat,
  60. "h": highlightFormat,
  61. }
  62. t := template.New("irccat")
  63. for k, v := range defaultTemplates {
  64. template.Must(t.New(k).Funcs(funcMap).Parse(v))
  65. }
  66. return t
  67. }
  68. func (hl *HTTPListener) renderTemplate(tpl_name string, data interface{}) ([]string, error) {
  69. var out bytes.Buffer
  70. t := hl.tpls.Lookup(tpl_name)
  71. if t == nil {
  72. return []string{}, errors.New("Nonexistent template")
  73. }
  74. t.Execute(&out, data)
  75. // The \r character is also a delimiter in IRC so strip it out.
  76. outStr := strings.Replace(out.String(), "\r", "", -1)
  77. return strings.Split(outStr, "\n"), nil
  78. }
  79. // We need this additional struct and function because the GitHub webhook package represents
  80. // commits as anonymous inner structs and Go's type system is bad. Unless I'm missing something very obvious.
  81. type Commit struct {
  82. Message string
  83. Username string
  84. Sha string
  85. }
  86. func commitLimit(pl github.PushPayload, length int) []Commit {
  87. res := make([]Commit, 0)
  88. i := 0
  89. for _, c := range pl.Commits {
  90. if !c.Distinct {
  91. continue
  92. }
  93. res = append(res, Commit{Message: c.Message, Username: c.Author.Username, Sha: c.ID})
  94. i += 1
  95. if i == length {
  96. break
  97. }
  98. }
  99. return res
  100. }