/
format.go
238 lines (199 loc) · 5.63 KB
/
format.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package lib
import (
"bytes"
"context"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"github.com/google/go-github/github"
)
// Format is Formatter
type Format struct {
ctx context.Context
client *github.Client
debug bool
}
// NewFormat is an initializer
func NewFormat(ctx context.Context, client *github.Client, debug bool) *Format {
return &Format{ctx: ctx, client: client, debug: debug}
}
// Line is line infomation
type Line struct {
title string
repoName string
url string
user string
status string
}
// NewLineByIssue is an initializer by Issue
func NewLineByIssue(repoName string, issue github.Issue) Line {
return Line{
title: *issue.Title,
repoName: repoName,
url: *issue.HTMLURL,
user: *issue.User.Login,
status: getIssueStatus(issue),
}
}
// NewLineByPullRequest is an initializer by PR
func NewLineByPullRequest(repoName string, pr github.PullRequest) Line {
return Line{
title: *pr.Title,
repoName: repoName,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
}
}
// Line returns Issue/PR info retrieving from GitHub
func (f *Format) Line(event *github.Event, i int) Line {
if f.debug {
fmt.Printf("%2d %s\n", i, htmlURL(event))
}
payload := event.Payload()
var line Line
switch *event.Type {
case "IssuesEvent":
e := payload.(*github.IssuesEvent)
issue := getIssue(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
if issue != nil {
if issue.PullRequestLinks == nil {
line = NewLineByIssue(*event.Repo.Name, *issue)
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
line = NewLineByPullRequest(*event.Repo.Name, *pr)
}
} else {
line = NewLineByIssue(*event.Repo.Name, *e.Issue)
}
case "IssueCommentEvent":
e := payload.(*github.IssueCommentEvent)
issue := getIssue(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
if issue != nil {
if issue.PullRequestLinks == nil {
line = NewLineByIssue(*event.Repo.Name, *issue)
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
line = NewLineByPullRequest(*event.Repo.Name, *pr)
}
} else {
line = NewLineByIssue(*event.Repo.Name, *e.Issue)
}
case "PullRequestEvent":
e := payload.(*github.PullRequestEvent)
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, e.GetNumber())
if pr != nil {
line = NewLineByPullRequest(*event.Repo.Name, *pr)
} else {
line = NewLineByPullRequest(*event.Repo.Name, *e.PullRequest)
}
case "PullRequestReviewCommentEvent":
e := payload.(*github.PullRequestReviewCommentEvent)
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.PullRequest.Number)
if pr != nil {
line = NewLineByPullRequest(*event.Repo.Name, *pr)
} else {
line = NewLineByPullRequest(*event.Repo.Name, *e.PullRequest)
}
}
return line
}
func getIssue(ctx context.Context, client *github.Client, repoFullName string, number int) *github.Issue {
owner, repo := getOwnerRepo(repoFullName)
issue, _, _ := client.Issues.Get(ctx, owner, repo, number)
return issue
}
func getPullRequest(ctx context.Context, client *github.Client, repoFullName string, number int) *github.PullRequest {
owner, repo := getOwnerRepo(repoFullName)
pr, _, _ := client.PullRequests.Get(ctx, owner, repo, number)
return pr
}
func getIssueStatus(issue github.Issue) string {
result := ""
if *issue.State == "closed" {
result = "closed"
}
return result
}
func getPullRequestStatus(pr github.PullRequest) string {
result := ""
if *pr.Merged {
result = "merged"
} else if *pr.State == "closed" {
result = "closed"
}
return result
}
func getOwnerRepo(repoFullName string) (string, string) {
s := strings.Split(repoFullName, "/")
owner := s[0]
repo := s[1]
return owner, repo
}
// All returns all lines which are formatted and sorted
func (f *Format) All(lines Lines) (string, error) {
var result, prevRepoName, currentRepoName string
var settings Settings
if err := settings.Init(); err != nil {
return "", err
}
sort.Sort(lines)
for _, line := range lines {
currentRepoName = line.repoName
if currentRepoName != prevRepoName {
prevRepoName = currentRepoName
result += fmt.Sprintf("\n%s\n\n", formatSubject(settings, currentRepoName))
}
result += fmt.Sprintf("%s\n", formatLine(settings, line))
}
return result, nil
}
// Lines has sort.Interface
type Lines []Line
func (l Lines) Len() int {
return len(l)
}
func (l Lines) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func (l Lines) Less(i, j int) bool {
return l[i].url < l[j].url
}
func formatSubject(settings Settings, repoName string) string {
formatSubject := convertNamedParameters(settings.Format.Subject)
m := map[string]interface{}{"subject": repoName}
t := template.Must(template.New("").Parse(formatSubject))
var rendered bytes.Buffer
t.Execute(&rendered, m)
return rendered.String()
}
func formatLine(settings Settings, line Line) string {
formatLine := convertNamedParameters(settings.Format.Line)
m := map[string]interface{}{
"title": line.title,
"url": line.url,
"user": line.user,
"status": formatStatus(settings, line.status),
}
t := template.Must(template.New("").Parse(formatLine))
var rendered bytes.Buffer
t.Execute(&rendered, m)
return strings.TrimSpace(rendered.String())
}
func formatStatus(settings Settings, status string) string {
switch status {
case "merged":
return settings.Dictionary.Status.Merged
case "closed":
return settings.Dictionary.Status.Closed
default:
return ""
}
}
// "%{hoge}" => "{{.hoge}}"
func convertNamedParameters(str string) string {
re := regexp.MustCompile("%{([^}]+)}")
return re.ReplaceAllString(str, "{{.$1}}")
}