-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
412 lines (340 loc) · 10.5 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/google/go-github/github"
"k8s.io/klog"
"sigs.k8s.io/yaml"
)
var (
credentialsFile = path.Join(os.Getenv("HOME"), ".shipbot/github_token")
basedir = path.Join(os.Getenv("HOME"), ".shipbot/data/")
)
type Config struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
}
func main() {
klog.InitFlags(nil)
configFile := ""
flag.StringVar(&configFile, "config", "", "config file to use")
format := ""
flag.StringVar(&format, "format", format, "output format")
flag.Set("logtostderr", "true")
flag.Parse()
if configFile == "" {
klog.Fatalf("must specify -config")
}
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
klog.Fatalf("error reading config file %q: %v", configFile, err)
}
config := &Config{}
if err := yaml.Unmarshal(configBytes, config); err != nil {
klog.Fatalf("error parsing config file %q: %v", configFile, err)
}
shipbot := &RelnotesBuilder{
Config: config,
DataDir: path.Join(basedir, config.Owner, config.Repo),
Format: format,
}
{
credBytes, err := ioutil.ReadFile(credentialsFile)
if err != nil {
klog.Fatalf("error reading github token from %q: %v", credentialsFile, err)
}
creds := strings.TrimSpace(string(credBytes))
tokens := strings.Split(creds, ":")
if len(tokens) != 2 {
klog.Fatalf("unexpected credentials format in %q", credentialsFile)
}
basicAuthTransport := &github.BasicAuthTransport{
Username: tokens[0],
Password: tokens[1],
}
//ts := oauth2.StaticTokenSource(
// &oauth2.Token{AccessToken: creds},
//)
//tc := oauth2.NewClient(oauth2.NoContext, ts)
//shipbot.Client = github.NewClient(tc)
shipbot.Client = github.NewClient(basicAuthTransport.Client())
}
var out bytes.Buffer
if err := shipbot.BuildRelnotes(os.Stdin, &out); err != nil {
klog.Fatalf("unexpected error: %v", err)
}
fmt.Print(out.String())
}
type RelnotesBuilder struct {
Client *github.Client
Config *Config
DataDir string
Format string
}
func (b *RelnotesBuilder) BuildRelnotes(in io.Reader, out *bytes.Buffer) error {
var prs []int
scanner := bufio.NewScanner(in)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
line = strings.TrimPrefix(line, "#")
if line == "" {
continue
}
pr, err := strconv.Atoi(line)
if err != nil {
klog.Fatalf("error parsing line: %s", line)
}
prs = append(prs, pr)
}
ctx := context.Background()
for _, prNumber := range prs {
//pr, _, err := sb.Client.PullRequests.Get(ctx, sb.Config.Owner, sb.Config.Repo, prNumber)
pr, err := b.readPR(ctx, prNumber)
if err != nil {
return err
}
commits, err := b.readCommits(ctx, prNumber)
if err != nil {
return err
}
comments, err := b.readIssueComments(ctx, pr.GetNumber())
if err != nil {
return err
}
for _, comment := range comments {
klog.V(2).Infof("comment: %v", comment)
}
//klog.Infof("PR #%d: %s", prNumber, pr.Title)
var authors []string
seen := make(map[string]bool)
for _, commit := range commits {
klog.V(2).Infof("commit %s", commit)
author := commit.Author.GetLogin()
if author == "" {
continue
}
if seen[author] {
continue
}
seen[author] = true
authors = append(authors, fmt.Sprintf("[@%s](%s)", author, commit.Author.GetHTMLURL()))
}
title := pr.GetTitle()
if strings.HasPrefix(title, "Automated cherry pick of ") {
title = strings.Replace(title, "Automated cherry pick of", "Cherry-pick of", 1)
}
if strings.HasPrefix(title, "Cherry pick of ") {
title = strings.Replace(title, "Cherry pick of", "Cherry-pick of", 1)
}
if strings.HasPrefix(title, "cherrypick #") {
title = strings.Replace(title, "cherrypick #", "Cherry-pick of #", 1)
}
if strings.HasPrefix(title, "Cherry-pick of #") {
picked := strings.TrimPrefix(title, "Cherry-pick of #")
picked = strings.Split(picked, " ")[0]
picked = strings.Trim(picked, ":")
prNumber, err := strconv.Atoi(picked)
if err != nil {
return fmt.Errorf("error parsing cherry pick line %q", title)
}
pickedPR, err := b.readPR(ctx, prNumber)
if err != nil {
return err
}
pr = pickedPR
}
if b.Format == "author" {
for _, a := range authors {
fmt.Fprintf(out, "%s\n", a)
}
} else {
fmt.Fprintf(out, "* %s %s [#%d](%s)\n", pr.GetTitle(), strings.Join(authors, ","), pr.GetNumber(), pr.GetHTMLURL())
}
}
return nil
}
func (b *RelnotesBuilder) readPR(ctx context.Context, prNumber int) (*github.PullRequest, error) {
p := filepath.Join(b.DataDir, "repos", b.Config.Owner, b.Config.Repo, "pulls", strconv.Itoa(prNumber), "data.json")
var data []byte
{
b, err := ioutil.ReadFile(p)
if err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("error reading cache file %q: %v", p, err)
}
} else {
klog.V(2).Infof("Using cached file for %s", p)
data = b
}
}
if data == nil {
u := fmt.Sprintf("repos/%v/%v/pulls/%d", b.Config.Owner, b.Config.Repo, prNumber)
req, err := b.Client.NewRequest("GET", u, nil)
if err != nil {
return nil, fmt.Errorf("error building request for %s: %v", u, err)
}
var buf bytes.Buffer
if _, err := b.Client.Do(ctx, req, &buf); err != nil {
return nil, fmt.Errorf("error requesting PR for %s: %v", u, err)
}
data = buf.Bytes()
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return nil, fmt.Errorf("error creating directories for %q: %v", p, err)
}
if err := ioutil.WriteFile(p, data, 0644); err != nil {
return nil, fmt.Errorf("error writing cache file %q: %v", p, err)
}
klog.V(2).Infof("Wrote cached file for %s", p)
}
pr := new(github.PullRequest)
if err := json.NewDecoder(bytes.NewReader(data)).Decode(pr); err != nil {
return nil, fmt.Errorf("error parsing PR for %s: %v", p, err)
}
return pr, nil
}
func (b *RelnotesBuilder) readCommits(ctx context.Context, prNumber int) ([]*github.RepositoryCommit, error) {
p := filepath.Join(b.DataDir, "repos", b.Config.Owner, b.Config.Repo, "pulls", strconv.Itoa(prNumber), "commits.json")
var data []byte
{
b, err := ioutil.ReadFile(p)
if err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("error reading cache file %q: %v", p, err)
}
} else {
klog.V(2).Infof("Using cached file for %s", p)
data = b
}
}
if data == nil {
var commits []*github.RepositoryCommit
//sb.Client.PullRequests.ListCommits()...
page := 1
perPage := 50
for {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/commits?page=%d&per_page=%d", b.Config.Owner, b.Config.Repo, prNumber, page, perPage)
req, err := b.Client.NewRequest("GET", u, nil)
if err != nil {
return nil, fmt.Errorf("error requesting commits for %s: %v", u, err)
}
var buf bytes.Buffer
resp, err := b.Client.Do(ctx, req, &buf)
if err != nil {
return nil, fmt.Errorf("error requesting commits for %s: %v", u, err)
}
var thisPage []*github.RepositoryCommit
if err := json.NewDecoder(&buf).Decode(&thisPage); err != nil {
return nil, fmt.Errorf("error parsing commits for %s: %v", u, err)
}
commits = append(commits, thisPage...)
page = resp.NextPage
if page == 0 {
break
}
}
jsonBytes, err := json.MarshalIndent(commits, "", " ")
if err != nil {
return nil, fmt.Errorf("error serializing commits for %s: %v", p, err)
}
data = jsonBytes
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return nil, fmt.Errorf("error creating directories for %q: %v", p, err)
}
if err := ioutil.WriteFile(p, data, 0644); err != nil {
return nil, fmt.Errorf("error writing cache file %q: %v", p, err)
}
klog.V(2).Infof("Wrote cached file for %s", p)
}
{
var commits []*github.RepositoryCommit
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&commits); err != nil {
return nil, fmt.Errorf("error parsing commits for %s: %v", p, err)
}
return commits, nil
}
}
func (b *RelnotesBuilder) readIssueComments(ctx context.Context, issueNumber int) ([]*github.IssueComment, error) {
p := filepath.Join(b.DataDir, "repos", b.Config.Owner, b.Config.Repo, "issues", strconv.Itoa(issueNumber), "comments.json")
var data []byte
{
b, err := ioutil.ReadFile(p)
if err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("error reading cache file %q: %v", p, err)
}
} else {
klog.V(2).Infof("Using cached file for %s", p)
data = b
}
}
if data == nil {
var all []*github.IssueComment
//sb.Client.PullRequests.ListCommits()...
page := 1
perPage := 50
for {
u := fmt.Sprintf("repos/%v/%v/issues/%d/comments?page=%d&per_page=%d", b.Config.Owner, b.Config.Repo, issueNumber, page, perPage)
req, err := b.Client.NewRequest("GET", u, nil)
if err != nil {
return nil, fmt.Errorf("error requesting issue comments for %s: %v", u, err)
}
var buf bytes.Buffer
resp, err := b.Client.Do(ctx, req, &buf)
if err != nil {
return nil, fmt.Errorf("error requesting issue comments for %s: %v", u, err)
}
var thisPage []*github.IssueComment
if err := json.NewDecoder(&buf).Decode(&thisPage); err != nil {
return nil, fmt.Errorf("error parsing issue comments for %s: %v", u, err)
}
all = append(all, thisPage...)
page = resp.NextPage
if page == 0 {
break
}
}
jsonBytes, err := json.MarshalIndent(all, "", " ")
if err != nil {
return nil, fmt.Errorf("error serializing issue comments for %s: %v", p, err)
}
data = jsonBytes
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return nil, fmt.Errorf("error creating directories for %q: %v", p, err)
}
if err := ioutil.WriteFile(p, data, 0644); err != nil {
return nil, fmt.Errorf("error writing cache file %q: %v", p, err)
}
klog.V(2).Infof("Wrote cached file for %s", p)
}
{
var comments []*github.IssueComment
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&comments); err != nil {
return nil, fmt.Errorf("error parsing issue comments for %s: %v", p, err)
}
return comments, nil
}
}