forked from irccloud/irccat
/
templates.go
129 lines (110 loc) · 4.56 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package httplistener
import (
"bytes"
"errors"
"regexp"
"strings"
"text/template"
"github.com/irccloud/irccat/util"
"gopkg.in/go-playground/webhooks.v5/github"
)
var defaultTemplates = map[string]string{
"github.release.irc": "[{{b .Repository.Name}}] release {{h .Release.TagName}} has been published by {{g .Release.Author.Login}}: {{.Release.HTMLURL}}",
"github.push.irc": `[{{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}}: {{if .Deleted}}{{.Repository.HTMLURL}}/commits/{{.Before}}{{else}}{{.Compare}}{{end}}
{{range commitLimit . 3}}
• {{g .Username}} ({{.Sha|truncateSha|h}}): {{trunc .Message 150}}
{{end}}`,
"github.issue.irc": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{.Action}} {{if contains .Issue.HTMLURL \"/pull/\"}}PR{{else}}issue{{end}} #{{.Issue.Number}}: {{.Issue.Title}} {{.Issue.HTMLURL}}",
"github.issuecomment.irc": "[{{b .Repository.Name}}] {{g .Comment.User.Login}} commented on {{if contains .Issue.HTMLURL \"/pull/\"}}PR{{else}}issue{{end}} #{{.Issue.Number}}: {{trunc .Comment.Body 150}} ({{.Issue.Title}}) {{.Comment.HTMLURL}}",
"github.pullrequestreviewcomment.irc": "[{{b .Repository.Name}}] {{g .Comment.User.Login}} review commented on PR #{{.PullRequest.Number}}: {{trunc .Comment.Body 150}} ({{.PullRequest.Title}}) {{.Comment.HTMLURL}}",
"github.pullrequestreview.irc": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{.Action}} a review on PR #{{.PullRequest.Number}}: {{trunc .Review.Body 150}} ({{.Review.State}}) ({{.PullRequest.Title}}) {{.Review.HTMLURL}}",
"github.pullrequest.irc": "[{{b .Repository.Name}}] {{g .Sender.Login}} {{.Action}} pull request #{{.PullRequest.Number}} [{{if .PullRequest.Merged}}merged{{else}}open{{end}}] (\x0303{{.PullRequest.Base.Ref}}...{{.PullRequest.Head.Ref}}\x0f): {{.PullRequest.Title}} {{.PullRequest.HTMLURL}}",
"github.commit.twitter": "{{.URL}} ({{.Sha|truncateSha}}): {{printf \"%.150s\" .Message}}",
"github.wiki": "[{{b .Repository.Name}}] {{range .Pages}}wiki page {{ .PageName }} {{ .Action }} {{if .Summary }} ({{.Summary}}) {{end}} {{.HTMLURL}} {{end}}",
}
func refName(ref string) string {
parts := strings.Split(ref, "/")
return strings.Join(parts[2:], "/")
}
func refType(ref string) string {
parts := strings.Split(ref, "/")
if parts[1] == "heads" {
return "branch"
} else if parts[1] == "tags" {
return "tag"
}
return ""
}
func stripQuoteAndTruncate(in string, length int) string {
var quote = regexp.MustCompile(`>.*\\n`)
return util.Truncate(quote.ReplaceAllString(in, ""), length)
}
func truncateSha(sha string) string {
if len(sha) < 8 {
return ""
}
return sha[:8]
}
// Colour helper functions to try and declutter
func boldFormat(text string) string {
return "\x02" + text + "\x0f"
}
func greyFormat(text string) string {
return "\x02" + text + "\x0f"
}
func highlightFormat(text string) string {
return "\x0303" + text + "\x0f"
}
func parseTemplates() *template.Template {
funcMap := template.FuncMap{
"trunc": stripQuoteAndTruncate,
"truncateSha": truncateSha,
"refType": refType,
"refName": refName,
"commitLimit": commitLimit,
"b": boldFormat,
"g": greyFormat,
"h": highlightFormat,
"contains": strings.Contains,
}
t := template.New("irccat")
for k, v := range defaultTemplates {
template.Must(t.New(k).Funcs(funcMap).Parse(v))
}
return t
}
func (hl *HTTPListener) renderTemplate(tpl_name string, data interface{}) ([]string, error) {
var out bytes.Buffer
t := hl.tpls.Lookup(tpl_name)
if t == nil {
return []string{}, errors.New("Nonexistent template")
}
t.Execute(&out, data)
// The \r character is also a delimiter in IRC so strip it out.
outStr := strings.Replace(out.String(), "\r", "", -1)
return strings.Split(outStr, "\n"), nil
}
// We need this additional struct and function because the GitHub webhook package represents
// commits as anonymous inner structs and Go's type system is bad. Unless I'm missing something very obvious.
type Commit struct {
Message string
Username string
Sha string
URL string
}
func commitLimit(pl github.PushPayload, length int) []Commit {
res := make([]Commit, 0)
i := 0
for _, c := range pl.Commits {
if !c.Distinct {
continue
}
res = append(res, Commit{Message: c.Message, Username: c.Author.Username, Sha: c.ID, URL: c.URL})
log.Infof("commit message=%s", c.Message)
i += 1
if i == length {
break
}
}
return res
}