forked from alexellis/derek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentHandler.go
More file actions
187 lines (143 loc) · 4.76 KB
/
commentHandler.go
File metadata and controls
187 lines (143 loc) · 4.76 KB
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
package main
import (
"context"
"fmt"
"os"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/types"
"github.com/google/go-github/github"
)
const open = "open"
const closed = "closed"
func makeClient(installation int) (*github.Client, context.Context) {
ctx := context.Background()
token := os.Getenv("access_token")
if len(token) == 0 {
applicationID := os.Getenv("application")
privateKeyPath := os.Getenv("private_key")
newToken, tokenErr := auth.MakeAccessTokenForInstallation(applicationID, installation, privateKeyPath)
if tokenErr != nil {
log.Fatalln(tokenErr.Error())
}
token = newToken
}
client := auth.MakeClient(ctx, token)
return client, ctx
}
func handleComment(req types.IssueCommentOuter) {
command := parse(req.Comment.Body)
switch command.Type {
case "AddLabel":
fmt.Printf("%s wants to %s of %s to issue %d\n", req.Comment.User.Login, command.Type, command.Value, req.Issue.Number)
found := false
for _, label := range req.Issue.Labels {
if label.Name == command.Value {
found = true
break
}
}
if found == true {
fmt.Println("Label already exists.")
return
}
client, ctx := makeClient(req.Installation.ID)
_, res, err := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, []string{command.Value})
if err != nil {
log.Fatalf("%s, limit: %d, remaining: %d", err, res.Limit, res.Remaining)
}
fmt.Println("Label added successfully or already existed.")
break
case "RemoveLabel":
fmt.Printf("%s wants to %s of %s to issue %d \n", req.Comment.User.Login, command.Type, command.Value, req.Issue.Number)
found := false
for _, label := range req.Issue.Labels {
if label.Name == command.Value {
found = true
break
}
}
if found == false {
fmt.Println("Label didn't exist on issue.")
return
}
client, ctx := makeClient(req.Installation.ID)
_, err := client.Issues.RemoveLabelForIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, command.Value)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Label removed successfully or already removed.")
break
case "Assign":
fmt.Printf("%s wants to %s user %s to issue %d\n", req.Comment.User.Login, command.Type, command.Value, req.Issue.Number)
client, ctx := makeClient(req.Installation.ID)
assignee := command.Value
if assignee == "me" {
assignee = req.Comment.User.Login
}
_, _, err := client.Issues.AddAssignees(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, []string{assignee})
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s assigned successfully or already assigned.\n", command.Value)
break
case "Unassign":
fmt.Printf("%s wants to %s user %s from issue %d\n", req.Comment.User.Login, command.Type, command.Value, req.Issue.Number)
client, ctx := makeClient(req.Installation.ID)
assignee := command.Value
if assignee == "me" {
assignee = req.Comment.User.Login
}
_, _, err := client.Issues.RemoveAssignees(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, []string{assignee})
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s unassigned successfully or already unassigned.\n", command.Value)
break
case "close", "reopen":
fmt.Printf("%s wants to %s issue #%d\n", req.Comment.User.Login, command.Type, req.Issue.Number)
client, ctx := makeClient(req.Installation.ID)
var state string
if command.Type == "close" {
state = closed
} else if command.Type == "reopen" {
state = open
}
input := &github.IssueRequest{State: &state}
_, _, err := client.Issues.Edit(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, input)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Request to %s issue #%d by %s was successful.\n", command.Type, req.Issue.Number, req.Comment.User.Login)
break
default:
log.Fatalln("Unable to work with comment: " + req.Comment.Body)
break
}
}
func parse(body string) *types.CommentAction {
commentAction := types.CommentAction{}
commands := map[string]string{
"Derek add label: ": "AddLabel",
"Derek remove label: ": "RemoveLabel",
"Derek assign: ": "Assign",
"Derek unassign: ": "Unassign",
"Derek close": "close",
"Derek reopen": "reopen",
}
for trigger, commandType := range commands {
if isValidCommand(body, trigger) {
val := body[len(trigger):]
val = strings.Trim(val, " \t.,\n\r")
commentAction.Type = commandType
commentAction.Value = val
break
}
}
return &commentAction
}
func isValidCommand(body string, trigger string) bool {
return (len(body) > len(trigger) && body[0:len(trigger)] == trigger) ||
(body == trigger && !strings.HasSuffix(trigger, ": "))
}