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

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