forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_request_tpl.go
63 lines (52 loc) · 1.36 KB
/
pull_request_tpl.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
package commands
import (
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
)
const pullRequestTmpl = `{{if .InitMsg}}{{.InitMsg}}
{{end}}
{{.CS}} Requesting a pull to {{.Base}} from {{.Head}}
{{.CS}}
{{.CS}} Write a message for this pull request. The first block
{{.CS}} of text is the title and the rest is the description.{{if .HasCommitLogs}}
{{.CS}}
{{.CS}} Changes:
{{.CS}}
{{.FormattedCommitLogs}}{{end}}`
type pullRequestMsg struct {
InitMsg string
CS string
Base string
Head string
CommitLogs string
}
func (p *pullRequestMsg) HasCommitLogs() bool {
return len(p.CommitLogs) > 0
}
func (p *pullRequestMsg) FormattedCommitLogs() string {
startRegexp := regexp.MustCompilePOSIX("^")
endRegexp := regexp.MustCompilePOSIX(" +$")
commitLogs := strings.TrimSpace(p.CommitLogs)
commitLogs = startRegexp.ReplaceAllString(commitLogs, fmt.Sprintf("%s ", p.CS))
commitLogs = endRegexp.ReplaceAllString(commitLogs, "")
return commitLogs
}
func renderPullRequestTpl(initMsg, cs, base, head string, commitLogs string) (string, error) {
t, err := template.New("pullRequestTmpl").Parse(pullRequestTmpl)
if err != nil {
return "", err
}
msg := &pullRequestMsg{
InitMsg: initMsg,
CS: cs,
Base: base,
Head: head,
CommitLogs: commitLogs,
}
var b bytes.Buffer
err = t.Execute(&b, msg)
return b.String(), err
}