forked from alexellis/derek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullRequestHandler.go
131 lines (105 loc) · 3.57 KB
/
pullRequestHandler.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
package main
import (
"context"
"fmt"
"strings"
"os"
log "github.com/Sirupsen/logrus"
"github.com/google/go-github/github"
"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/types"
)
func handlePullRequest(req types.PullRequestOuter) {
ctx := context.Background()
token := os.Getenv("access_token")
if len(token) == 0 {
newToken, tokenErr := auth.MakeAccessTokenForInstallation(
os.Getenv("application"),
req.Installation.ID,
os.Getenv("private_key"))
if tokenErr != nil {
log.Fatalln(tokenErr.Error())
}
token = newToken
}
client := auth.MakeClient(ctx, token)
hasUnsignedCommits, err := hasUnsigned(req, client)
if err != nil {
log.Fatal(err)
} else if hasUnsignedCommits {
fmt.Println("May need to apply labels on item.")
issue, _, labelErr := client.Issues.Get(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number)
if labelErr != nil {
log.Fatalln(labelErr)
}
fmt.Println("Current labels ", issue.Labels)
if hasNoDcoLabel(issue) == false {
fmt.Println("Applying label")
_, res, assignLabelErr := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, []string{"no-dco"})
if assignLabelErr != nil {
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, res.Limit, res.Remaining)
}
link := fmt.Sprintf("https://github.com/%s/%s/blob/master/CONTRIBUTING.md", req.Repository.Owner.Login, req.Repository.Name)
body := `Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off.
That's something we need before your Pull Request can be merged. Please see our [contributing guide](` + link + `).`
comment := &github.IssueComment{
Body: &body,
}
comment, resp, err := client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment)
if err != nil {
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, resp.Limit, resp.Remaining)
log.Fatal(err)
}
fmt.Println(comment, resp.Rate)
}
} else {
fmt.Println("Things look OK right now.")
issue, res, labelErr := client.Issues.Get(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number)
if labelErr != nil {
log.Fatalf("%s limit: %d, remaining: %d", labelErr, res.Limit, res.Remaining)
log.Fatalln()
}
if hasNoDcoLabel(issue) {
fmt.Println("Removing label")
_, removeLabelErr := client.Issues.RemoveLabelForIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, "no-dco")
if removeLabelErr != nil {
log.Fatal(removeLabelErr)
}
}
}
}
func hasNoDcoLabel(issue *github.Issue) bool {
if issue != nil {
for _, label := range issue.Labels {
if label.GetName() == "no-dco" {
return true
}
}
}
return false
}
func hasUnsigned(req types.PullRequestOuter, client *github.Client) (bool, error) {
hasUnsigned := false
ctx := context.Background()
var err error
listOpts := &github.ListOptions{
Page: 0,
}
commits, resp, err := client.PullRequests.ListCommits(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, listOpts)
if err != nil {
log.Fatalf("Error getting PR %d\n%s", req.PullRequest.Number, err.Error())
return hasUnsigned, err
}
fmt.Println("Rate limiting", resp.Rate)
for _, commit := range commits {
if commit.Commit != nil && commit.Commit.Message != nil {
if isSigned(*commit.Commit.Message) == false {
hasUnsigned = true
}
}
}
return hasUnsigned, err
}
func isSigned(msg string) bool {
return strings.Contains(msg, "Signed-off-by:")
}