This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
forked from gerad/release-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues.go
209 lines (181 loc) · 5.85 KB
/
issues.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
package main
import (
"bytes"
"fmt"
"html/template"
"os"
"regexp"
"strings"
"time"
)
const ITEMS_PER_PAGE = 100
type IssuesApp struct {
config Config
github GithubGateway
}
func NewIssuesApp(config Config, github GithubGateway) IssuesApp {
return IssuesApp{config, github}
}
func (app IssuesApp) Issues() []Issue {
issues := []Issue{}
secondsPerRepo := 9
repos := strings.Fields(app.config.repos)
numRepos := len(repos)
shouldThrottle := numRepos > 7
timeRemaining := numRepos * secondsPerRepo
if numRepos > 7 {
fmt.Fprintf(os.Stderr, "processing: approximately ")
fmt.Fprintf(os.Stderr, "\033[s")
fmt.Fprintf(os.Stderr, "%d seconds remaining", timeRemaining)
}
for _, repo := range repos {
start := time.Now()
issues = append(issues, app.repoIssues(repo)...)
elapsed := int(time.Since(start).Round(time.Second).Seconds())
if shouldThrottle {
timeRemaining = processingWait(secondsPerRepo-elapsed, timeRemaining-elapsed)
}
}
if shouldThrottle {
fmt.Fprintf(os.Stderr, "\033[2K")
fmt.Fprintf(os.Stderr, "\033[1E")
}
return issues
}
func processingWait(seconds, timeRemaining int) int {
for i := 0; i < seconds; i++ {
fmt.Fprintf(os.Stderr, "\033[u")
fmt.Fprintf(os.Stderr, "\033[s")
fmt.Fprintf(os.Stderr, "%d", timeRemaining-i)
time.Sleep(time.Second)
}
return timeRemaining - seconds
}
type Item struct {
Title string `json:"title"`
ClosedAt string `json:"closed_at"`
UpdatedAt string `json:"updated_at"`
Number int `json:"number"`
Body string `json:"body"`
PullRequest struct {
URL string `json:"html_url"`
} `json:"pull_request"`
User struct {
Login string `json:"login"`
} `json:"user`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
}
func (i Item) toIssue(repo string, isHotfix bool, isMerged bool) Issue {
var labels []string
for _, label := range i.Labels {
labels = append(labels, label.Name)
}
return Issue{
Author: i.User.Login,
Repo: repo,
Num: fmt.Sprintf("%d", i.Number),
URL: i.PullRequest.URL,
Title: i.Title,
Labels: labels,
ReleaseNotes: strings.TrimSpace(getReleaseNotes(i.Body)),
Testing: strings.TrimSpace(getTesting(i.Body)),
IsHotfix: isHotfix,
IsMerged: isMerged,
}
}
type Items []Item
func (app IssuesApp) repoIssues(repo string) []Issue {
issues := []Issue{}
since := releaseToTimestamp(app.config.base)
until := releaseToTimestamp(app.config.head)
query := fmt.Sprintf("search/issues?q=repo:sagansystems/%s+is:merged+merged:%s..%s+base:master", repo, since, until)
if addIssues, err := app.getIssuesForQuery(query, repo, false, true); err != nil {
fmt.Printf("error fetching issues merged into master branch of %s: %v", repo, err)
os.Exit(1)
} else {
issues = append(issues, addIssues...)
}
if app.config.head != "" {
query = fmt.Sprintf("search/issues?q=repo:sagansystems/%s+is:merged+base:%s", repo, app.config.head)
if addIssues, err := app.getIssuesForQuery(query, repo, true, true); err != nil {
fmt.Printf("error fetching issues merged into release branch [%s] of %s: %v", app.config.head, repo, err)
os.Exit(1)
} else {
issues = append(issues, addIssues...)
}
query = fmt.Sprintf("search/issues?q=repo:sagansystems/%s+is:open+base:%s", repo, app.config.head)
if addIssues, err := app.getIssuesForQuery(query, repo, true, false); err != nil {
fmt.Printf("error fetching open issues for release branch [%s] of %s : %v", app.config.head, repo, err)
os.Exit(1)
} else {
issues = append(issues, addIssues...)
}
}
query = fmt.Sprintf("search/issues?q=repo:sagansystems/%s+is:open+base:master", repo)
if addIssues, err := app.getIssuesForQuery(query, repo, false, false); err != nil {
fmt.Printf("error fetching open issues for master branch of %s: %v", repo, err)
os.Exit(1)
} else {
issues = append(issues, addIssues...)
}
return issues
}
func (app IssuesApp) getIssuesForQuery(url, repo string, isHotfix, isMerged bool) ([]Issue, error) {
type Response struct {
TotalCount int `json:"total_count"`
Items Items `json:"items"`
}
var issues []Issue
for page := 1; true; page++ {
var res Response
path := fmt.Sprintf("%s&page=%d&per_page=%d", url, page, ITEMS_PER_PAGE)
if err := app.github.Get(path, &res); err != nil {
return nil, err
}
for _, item := range res.Items {
issues = append(issues, item.toIssue(repo, isHotfix, isMerged))
}
if res.TotalCount-ITEMS_PER_PAGE*page <= 0 {
break
}
}
return issues, nil
}
const tsRegexp = ".*-(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})Z"
const timeFmt = "%s-%s-%sT%s:%s:%sZ"
func releaseToTimestamp(release string) string {
re := regexp.MustCompile(tsRegexp)
matches := re.FindStringSubmatch(release)
if len(matches) < 7 {
return time.Now().UTC().Format(fmt.Sprintf(timeFmt, "2006", "01", "02", "15", "04", "05"))
}
return fmt.Sprintf(timeFmt, matches[1], matches[2], matches[3], matches[4], matches[5], matches[6])
}
func getSectionRegex(sectionString string) string {
const sectionTemplate = "(?:#+\\s*|\\*\\*){{.}}\\s*\\**[\\r\\n]+((?s:.)*?)(?:\\z|\\*\\*|#+|JIRA: \\[)"
tmpl := template.New("sectionTemplate")
tmpl, _ = tmpl.Parse(sectionTemplate)
var tpl bytes.Buffer
tmpl.Execute(&tpl, sectionString)
return tpl.String()
}
func getReleaseNotes(description string) string {
notesRegexp := getSectionRegex("(?i:release\\s*notes)")
re := regexp.MustCompile(notesRegexp)
matches := re.FindStringSubmatch(description)
if len(matches) < 2 {
return ""
}
return strings.TrimSpace(strings.TrimRight(strings.Replace(matches[1], "\r", "", -1), "\n\r "))
}
func getTesting(description string) string {
notesRegexp := getSectionRegex("(?i:testing)")
re := regexp.MustCompile(notesRegexp)
matches := re.FindStringSubmatch(description)
if len(matches) < 2 {
return ""
}
return strings.TrimSpace(strings.TrimRight(strings.Replace(matches[1], "\r", "", -1), "\n\r "))
}