-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (170 loc) · 5.19 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/google/go-github/github"
flag "github.com/spf13/pflag"
"golang.org/x/oauth2"
)
// Config holds TOML file data
type Config struct {
Repositories []string `toml:"repositories"`
Labels []Label `toml:"label"`
}
// Label represents an Issue label
type Label struct {
Name string `toml:"name"`
Color string `toml:"color"`
Mappings []string `toml:"mappings,omitempty"`
Delete bool `toml:"delete,omitempty"`
}
func main() {
var configFile = flag.StringP("config", "c", "", "Path to configuration file")
flag.Parse()
// Set default config file
if *configFile == "" {
*configFile = "/etc/repo-gopher/config.toml"
}
// Load config file
tomlData, err := ioutil.ReadFile(*configFile)
if err != nil {
fmt.Printf("Unable to load config file: %s\n", *configFile)
os.Exit(1)
}
var conf Config
if _, err := toml.Decode(string(tomlData), &conf); err != nil {
fmt.Println("Error decoding toml.")
os.Exit(1)
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
for _, repo := range conf.Repositories {
if conf.Labels != nil {
for _, label := range conf.Labels {
fmt.Printf("Working on label '%v' for %s.\n", label.Name, repo)
if err := ensureLabel(ctx, repo, label, client); err != nil {
fmt.Printf("Error label '%v' on repository %s: %v\n", label.Name, repo, err)
}
}
}
}
}
// Some ground rules:
// We'll never rename a label
// To do mappings, we'll create the new label, and then for each issue
// with the old label, we'll add the new label, then remove the old one.
func ensureLabel(ctx context.Context, repo string, label Label, gh *github.Client) error {
splitRepo := strings.Split(repo, "/")
owner := splitRepo[0]
repo = splitRepo[1]
mappings := label.Mappings
// Get all labels in repository
allLabels, _, err := gh.Issues.ListLabels(ctx, owner, repo, &github.ListOptions{})
if err != nil {
return err
}
// Delete labels with the delete flag
if label.Delete && isLabelInSlice(label.Name, allLabels) {
resp, err := gh.Issues.DeleteLabel(ctx, owner, repo, label.Name)
if err != nil {
return github.CheckResponse(resp.Response)
}
fmt.Printf("Deleted label: %s\n", label.Name)
return nil
} else if label.Delete && !isLabelInSlice(label.Name, allLabels) {
return nil
}
// Create labels if they don't exist; otherwise make color match
if !isLabelInSlice(label.Name, allLabels) {
alreadyExistsError := false
_, _, err := gh.Issues.CreateLabel(ctx, owner, repo,
&github.Label{Name: &label.Name, Color: &label.Color})
if err != nil && strings.Contains(err.Error(), "already_exists") {
alreadyExistsError = true
} else if err != nil {
return err
}
if alreadyExistsError == false {
fmt.Println("Successfully added label.")
}
} else {
resp, _, err := gh.Issues.GetLabel(ctx, owner, repo, label.Name)
if err != nil {
return err
}
if resp.Color != &label.Color {
_, _, err := gh.Issues.EditLabel(ctx, owner, repo, label.Name,
&github.Label{Name: &label.Name, Color: &label.Color})
if err != nil {
return err
}
}
}
// Map new labels onto issues with old labels
for _, oldLabel := range mappings {
if isLabelInSlice(oldLabel, allLabels) {
fmt.Printf("Working on %s mapping for %s.\n", oldLabel, label.Name)
count := 0
issues, err := issuesWith(ctx, owner, repo, "label", oldLabel, gh)
if err != nil {
return err
}
if len(issues) > 0 {
for _, k := range issues {
fmt.Printf("Updating issue %s/%s#%d.\n", owner, repo, k.GetNumber())
fmt.Printf("Adding label: %s.\n", label.Name)
_, _, err := gh.Issues.AddLabelsToIssue(ctx, owner, repo, k.GetNumber(), []string{label.Name})
if err != nil {
return err
}
fmt.Printf("Removing label %s.\n", oldLabel)
_, err = gh.Issues.RemoveLabelForIssue(ctx, owner, repo, k.GetNumber(), oldLabel)
if err != nil {
return err
}
count++
fmt.Printf("-----\n")
}
}
// Check once more before deleting label
remaining, err := issuesWith(ctx, owner, repo, "label", oldLabel, gh)
if err != nil {
return err
}
if len(remaining) == 0 || len(remaining) == count {
fmt.Printf("Deleting label %s from %s.\n", oldLabel, repo)
_, err := gh.Issues.DeleteLabel(ctx, owner, repo, oldLabel)
if err != nil {
return err
}
} else {
fmt.Printf("There are remaining issues returned from search - you should manually check that the %s label has no tickets assigned (or rerun this script in a few minutes time)\n", oldLabel)
}
}
}
return nil
}
func issuesWith(ctx context.Context, owner, repo, kind, label string, gh *github.Client) ([]github.Issue, error) {
query := fmt.Sprintf("repo:%s/%s %s:\"%s\"", owner, repo, kind, label)
resp, _, err := gh.Search.Issues(ctx, query, &github.SearchOptions{})
if err != nil {
fmt.Println("help")
}
return resp.Issues, nil
}
func isLabelInSlice(a string, list []*github.Label) bool {
for _, b := range list {
if b.GetName() == a {
return true
}
}
return false
}