/
issues.go
157 lines (140 loc) · 3.8 KB
/
issues.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
package issues
import (
"context"
"log"
"github.com/stephen-soltesz/github-webhook-poc/slice"
"github.com/stephen-soltesz/github-webhook-poc/events/issues/iface"
"github.com/stephen-soltesz/pretty"
"github.com/google/go-github/github"
)
// Event encapsulates operations on a *github.IssuesEvent.
type Event struct {
// *github.Client
*github.IssuesEvent
iface.Issues
}
// NewEvent creates a new Event based on the given client and event.
// func NewEvent(client *github.Client, event *github.IssuesEvent) *Event {
func NewEvent(issues iface.Issues, event *github.IssuesEvent) *Event {
return &Event{
IssuesEvent: event,
Issues: issues,
}
}
// EditIssue updates the event issue using the given request.
func (ev *Event) EditIssue(
ctx context.Context,
req *github.IssueRequest) (*github.Issue, *github.Response, error) {
log.Println("Issues.Edit:",
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(),
pretty.SprintPlain(req))
return ev.Issues.Edit(
ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(),
req)
}
func (ev *Event) CloseIssue(ctx context.Context, labels []string) (*github.Issue, *github.Response, error) {
var req *github.IssueRequest
closed := "closed"
if labels == nil {
req = &github.IssueRequest{
State: &closed,
}
} else {
req = &github.IssueRequest{
State: &closed,
Labels: &labels,
}
}
return ev.EditIssue(ctx, req)
}
func (ev *Event) SetIssueLabels(ctx context.Context, labels []string) (
*github.Issue, *github.Response, error) {
return ev.EditIssue(
ctx,
&github.IssueRequest{
Labels: &labels,
},
)
}
// AddIssueLabel adds a label to the event issue.
func (ev *Event) AddIssueLabels(
ctx context.Context, labels []string) ([]*github.Label, *github.Response, error) {
/*
// Check curent labels for the new label and collect the list.
currentLabels := []string{}
for _, current := range ev.GetIssue().Labels {
currentLabels = append(currentLabels, current.GetName())
}
// Append the new label to the current labels.
for _, label := range labels {
if !slice.ContainsString(currentLabels, label) {
currentLabels = append(currentLabels, label)
}
}
if len(ev.GetIssue().Labels) == len(currentLabels) {
return nil, nil, nil
}
return ev.EditIssue(
ctx,
&github.IssueRequest{
Labels: ¤tLabels,
},
)
*/
log.Println("Issues.AddLabelsToIssue:",
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(), labels)
return ev.Issues.AddLabelsToIssue(
ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(), labels)
}
// RemoveIssueLabels removes the given label from the event issue. If the label
// is not found, no action is taken.
func (ev *Event) RemoveIssueLabels(
ctx context.Context, labels []string) (*github.Response, error) {
/*
currentLabels := filterLabels(ev.GetIssue().Labels, labels)
if len(currentLabels) != len(ev.GetIssue().Labels) {
return ev.EditIssue(
ctx,
&github.IssueRequest{
Labels: ¤tLabels,
},
)
}
return nil, nil, nil
*/
var resp *github.Response
var err error
for _, label := range labels {
resp, err = ev.Issues.RemoveLabelForIssue(ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(), label)
if err != nil {
//TODO: skip 404 errors.
log.Println(err)
// return nil, err
}
}
return resp, nil
}
func filterLabels(labels []github.Label, remove []string) []string {
currentLabels := []string{}
for _, currentLabel := range labels {
if slice.ContainsString(remove, currentLabel.GetName()) {
// Skip this label since we want it removed.
continue
}
currentLabels = append(currentLabels, currentLabel.GetName())
}
return currentLabels
}