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

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