/
github.go
69 lines (64 loc) · 1.89 KB
/
github.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
package main
import (
"context"
"fmt"
"time"
"github.com/google/go-github/v29/github"
"go.uber.org/zap"
)
// getAllPullRequestFiles returns all commit files in a pull request.
func getAllPullRequestFiles(
ctx context.Context,
client *github.Client,
owner, repo string,
number int,
) ([]*github.CommitFile, error) {
var res []*github.CommitFile
for offset := 0; ; offset++ {
files, resp, err := client.PullRequests.ListFiles(
ctx,
owner, repo, number,
&github.ListOptions{Page: offset, PerPage: 100},
)
if err != nil {
logger.Error("Failed to remove a label from a pull request",
zap.String("owner", owner),
zap.String("repo", repo),
zap.Int("number", number),
zap.Int("offset", offset),
zap.Error(err),
)
return nil, fmt.Errorf("list commit files: %w", err)
}
res = append(res, files...)
if offset >= resp.LastPage {
break
}
}
return res, nil
}
// addToChangeLog appends the files to the changelog file and adds the file to the commit.
func createChangeLogIssue(
ctx context.Context,
client *github.Client,
owner, repo string,
number int,
) (bool, error) {
files, err := getAllPullRequestFiles(ctx, client, owner, repo, number)
if err != nil {
return false, fmt.Errorf("get all commit files: %w", err)
}
currentTime := time.Now().String()
printableString := fmt.Sprintf("\nTo be added to change Log on %s: \n Owner: %s\n Number: %d\nFiles Changed:\n", currentTime, owner, number)
for _, file := range files {
printableString = fmt.Sprintf("%s %s\n", printableString, *file.Filename)
}
newIssue := new(github.IssueRequest)
presetTitle := fmt.Sprintf("\nChange Log Additions at %s for PR #%d:", currentTime, number)
newIssue.Title = &presetTitle
newIssue.Body = &printableString
if _, _, err := client.Issues.Create(ctx, owner, repo, newIssue); err != nil {
return false, fmt.Errorf("Could not create Issue: %w", err)
}
return true, nil
}