-
Notifications
You must be signed in to change notification settings - Fork 547
/
main.go
101 lines (84 loc) · 3.18 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
package main
import (
"context"
"log"
"os"
"strconv"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
var (
maintainers = []string{"dependabot[bot]", "jacobbednarz", "patryk"}
goDependencyMessage = "This project handles dependency version bumps (including upstream changes from cloudflare-go) independently of the standard PR process using automation. This allows the dependency upgrades to land without causing merge conflicts in multiple branches and handled in a consistent way. The exception to this is security related dependency upgrades but they should be co-ordinated with the maintainer team privately.\n\nPlease remove the changes to the `go.mod` or `go.sum` files from this PR in order to proceed with review and merging."
changelogMessage = "This pull request contains a CHANGELOG.md file which should only be modified by maintainers.\n\nIf you are looking to include a CHANGELOG entry, you should use the process documented at https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/contributing/changelog-process.md instead.\n\nIn order for this pull request to be merged, you need remove the modifications to CHANGELOG.md."
)
func main() {
ctx := context.Background()
if len(os.Args) < 2 {
log.Fatalf("Usage: maintainer-only-file-check PR#\n")
}
pr := os.Args[1]
prNo, err := strconv.Atoi(pr)
if err != nil {
log.Fatalf("error parsing PR %q as a number: %s", pr, err)
}
owner := os.Getenv("GITHUB_OWNER")
repo := os.Getenv("GITHUB_REPO")
token := os.Getenv("GITHUB_TOKEN")
if owner == "" {
log.Fatalf("GITHUB_OWNER not set")
}
if repo == "" {
log.Fatalf("GITHUB_REPO not set")
}
if token == "" {
log.Fatalf("GITHUB_TOKEN not set")
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
pullRequest, _, err := client.PullRequests.Get(ctx, owner, repo, prNo)
if err != nil {
log.Fatalf("error retrieving pull request %s/%s#%d: %s", owner, repo, prNo, err)
}
// Don't worry about continuing if the PR was opened by a maintainer...or bot
// that does maintenance tasks on our behalf.
if contains(maintainers, *pullRequest.User.Login) {
os.Exit(0)
}
files, _, _ := client.PullRequests.ListFiles(ctx, owner, repo, prNo, &github.ListOptions{})
if err != nil {
log.Fatalf("error retrieving files on pull request %s/%s#%d: %s", owner, repo, prNo, err)
}
for _, file := range files {
if file.GetFilename() == "go.mod" || file.GetFilename() == "go.sum" {
_, _, err = client.Issues.CreateComment(ctx, owner, repo, prNo, &github.IssueComment{
Body: &goDependencyMessage,
})
if err != nil {
log.Fatalf("error creating comment on pull request %s/%s#%d: %s", owner, repo, prNo, err)
}
os.Exit(1)
}
if file.GetFilename() == "CHANGELOG.md" {
_, _, err = client.Issues.CreateComment(ctx, owner, repo, prNo, &github.IssueComment{
Body: &changelogMessage,
})
if err != nil {
log.Fatalf("error creating comment on pull request %s/%s#%d: %s", owner, repo, prNo, err)
}
os.Exit(1)
}
}
os.Exit(0)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}