/
local.go
149 lines (129 loc) · 4.22 KB
/
local.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
package local
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/stephen-soltesz/github-webhook-poc/events/issues/iface"
"github.com/stephen-soltesz/pretty"
"github.com/stephen-soltesz/github-webhook-poc/githubx"
"github.com/google/go-github/github"
"github.com/stephen-soltesz/github-webhook-poc/events/issues"
)
var (
// ErrNewClient is returned when a new Github client fails.
ErrNewClient = fmt.Errorf("Failed to allocate new Github client")
)
// Config contains
type Config struct {
// Delay is
Delay time.Duration
getIface func(client *github.Client) iface.Issues
}
// NewConfig creates a new config instantce.
func NewConfig(delay time.Duration) *Config {
// Initialize the new config instance with a default getIface function.
return &Config{Delay: delay, getIface: getIface}
}
// Client collects local data needed for these operations.
type getInstallationer interface {
GetInstallation() *github.Installation
}
func getSafeID(event getInstallationer) int64 {
if event.GetInstallation() != nil {
return event.GetInstallation().GetID()
}
return 0
}
func getIface(client *github.Client) iface.Issues {
return iface.NewIssues(client.Issues)
}
func genWeekLabels(now time.Time) []string {
year, week := now.ISOWeek()
weekLabel := fmt.Sprintf("Week %d", week)
yearLabel := fmt.Sprintf("%d", year)
return []string{weekLabel, yearLabel}
}
// IssuesEvent .
func (c *Config) IssuesEvent(event *github.IssuesEvent) error {
client := githubx.NewClient(getSafeID(event))
if client == nil {
return ErrNewClient
}
ev := issues.NewEvent(c.getIface(client), event)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// Lose the race for loading page load after "Submit new issue"
// so that the new label is visible to user.
// time.Sleep(c.Delay)
var err error
var resp *github.Response
var issue *github.Issue
var labels []*github.Label
log.Println("IssuesEvent:", ev.GetAction(), ev.GetIssue().GetHTMLURL())
switch {
case ev.GetAction() == "opened" || ev.GetAction() == "reopened":
pretty.Print(event)
labels, resp, err = ev.AddIssueLabels(ctx, []string{"review/triage"})
case ev.GetAction() == "closed":
pretty.Print(event)
resp, err = ev.RemoveIssueLabels(ctx, []string{"review/triage"})
case ev.GetAction() == "labeled":
// Note: the issue labels always include the label triggering the event.
fmt.Println("label")
pretty.Print(event)
current := []string{}
for _, label := range event.GetIssue().Labels {
current = append(current, label.GetName())
}
eventLabel := event.GetLabel().GetName()
fmt.Println("Event label:", eventLabel)
fmt.Println("Current labels:", current)
switch strings.ToLower(eventLabel) {
case "backlog":
resp, err = ev.RemoveIssueLabels(ctx, []string{"review/triage", "current", "closed"})
case "current":
_, resp, err = ev.AddIssueLabels(ctx, genWeekLabels(time.Now()))
if err == nil && len(current) != 0 {
resp, err = ev.RemoveIssueLabels(ctx, []string{"review/triage", "backlog", "closed"})
}
// current = append(current, genSprintLabels(time.Now())...)
// current = slice.FilterStrings(current, []string{"review/triage", "backlog", "closed"})
// issue, resp, err = ev.SetIssueLabels(ctx, current)
case "closed":
issue, resp, err = ev.CloseIssue(ctx, nil)
}
case ev.GetAction() == "unlabeled":
pretty.Print(event)
fmt.Println("unlabel")
default:
log.Println("IssuesEvent: ignoring unsupported action:", ev.GetAction())
}
if err != nil {
log.Println("IssuesEvent: error: ", resp, err)
}
if labels != nil {
log.Println("IssuesEvent: okay: ", resp, labels)
}
if issue != nil {
labels := ""
for _, currentLabel := range issue.Labels {
labels += currentLabel.GetName() + " "
}
log.Println("IssuesEvent: okay: ", resp, labels)
}
return nil
}
// InstallationEvent handles events when an application is installed for the
// first time.
func InstallationEvent(event *github.InstallationEvent) error {
pretty.Print(event)
return nil
}
// InstallationRepositoriesEvent handles events when repositories are added or
// removed from a particular application installation.
func InstallationRepositoriesEvent(event *github.InstallationRepositoriesEvent) error {
pretty.Print(event)
return nil
}