forked from larsks/action-label-syncer
/
github.go
228 lines (207 loc) · 5.63 KB
/
github.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2020 micnncim
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package github
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)
type Client struct {
githubClient *github.Client
token string
}
type Label struct {
// If "import" is present, all other fields are ignored.
Import string `yaml:"import"`
Name string `yaml:"name"`
Alias string `yaml:"alias"`
Aliases []string `yaml:"aliases"`
Description string `yaml:"description"`
Color string `yaml:"color"`
}
func FromManifestToLabels(path string) ([]Label, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var labels []Label
err = yaml.Unmarshal(buf, &labels)
if err != nil {
return nil, err
}
var flatLabels []Label
for _, l := range labels {
if l.Import == "" {
// Data checks and normalization.
if len(l.Description) > 100 {
return nil, fmt.Errorf("Description of \"%s\" exceeds 100 characters", l.Name)
}
if strings.Contains(l.Name, "?") {
return nil, fmt.Errorf("Label name cannot contain question marks: \"%s\"", l.Name)
}
if l.Alias != "" {
l.Aliases = append(l.Aliases, l.Alias)
}
flatLabels = append(flatLabels, l)
} else {
// Handle imports of labels from another file
importPath := filepath.Join(filepath.Dir(path), l.Import)
importedLabels, err := FromManifestToLabels(importPath)
if err != nil {
return nil, err
}
flatLabels = append(flatLabels, importedLabels...)
}
}
return flatLabels, err
}
func NewClient(token string) *Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
return &Client{
githubClient: github.NewClient(tc),
}
}
func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label, prune bool, dryRun bool) error {
if dryRun {
fmt.Printf("Dry run! No actual changes will be made.\n")
}
labelMap := make(map[string]Label)
aliasMap := make(map[string]Label)
for _, l := range labels {
labelMap[l.Name] = l
for _, alias := range l.Aliases {
aliasMap[alias] = l
}
}
currentLabels, err := c.getLabels(ctx, owner, repo)
if err != nil {
return err
}
currentLabelMap := make(map[string]Label)
for _, l := range currentLabels {
currentLabelMap[l.Name] = l
}
// Delete labels.
if prune {
for _, currentLabel := range currentLabels {
currentLabel := currentLabel
_, name_ok := labelMap[currentLabel.Name]
_, alias_ok := aliasMap[currentLabel.Name]
if !alias_ok && !name_ok {
err := c.deleteLabel(ctx, owner, repo, currentLabel.Name, dryRun)
if err != nil {
return err
}
}
}
}
// Create and/or update labels.
for _, l := range labels {
l := l
currentLabel, ok := currentLabelMap[l.Name]
if !ok {
for _, alias := range l.Aliases {
currentLabel, ok = currentLabelMap[alias]
if ok {
break
}
}
}
if !ok {
err := c.createLabel(ctx, owner, repo, l, dryRun)
if err != nil {
return err
}
} else if currentLabel.Description != l.Description || currentLabel.Color != l.Color || currentLabel.Name != l.Name {
err := c.updateLabel(ctx, owner, repo, currentLabel.Name, l, dryRun)
if err != nil {
return err
}
} else {
//fmt.Printf("Not changed: \"%s\" on %s/%s\n", l.Name, owner, repo)
}
}
return nil
}
func (c *Client) createLabel(ctx context.Context, owner, repo string, label Label, dryRun bool) error {
l := &github.Label{
Name: &label.Name,
Description: &label.Description,
Color: &label.Color,
}
fmt.Printf("Created: \"%s\" on %s/%s\n", label.Name, owner, repo)
if dryRun {
return nil
}
_, _, err := c.githubClient.Issues.CreateLabel(ctx, owner, repo, l)
return err
}
func (c *Client) getLabels(ctx context.Context, owner, repo string) ([]Label, error) {
opt := &github.ListOptions{
PerPage: 50,
}
var labels []Label
for {
ls, resp, err := c.githubClient.Issues.ListLabels(ctx, owner, repo, opt)
if err != nil {
return nil, err
}
for _, l := range ls {
labels = append(labels, Label{
Name: l.GetName(),
Description: l.GetDescription(),
Color: l.GetColor(),
})
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return labels, nil
}
func (c *Client) updateLabel(ctx context.Context, owner, repo, labelName string, label Label, dryRun bool) error {
l := &github.Label{
Name: &label.Name,
Description: &label.Description,
Color: &label.Color,
}
if labelName != label.Name {
fmt.Printf("Renamed: \"%s\" => \"%s\" on %s/%s\n", labelName, label.Name, owner, repo)
} else {
fmt.Printf("Updated: \"%s\" on %s/%s\n", label.Name, owner, repo)
}
if dryRun {
return nil
}
_, _, err := c.githubClient.Issues.EditLabel(ctx, owner, repo, labelName, l)
return err
}
func (c *Client) deleteLabel(ctx context.Context, owner, repo, name string, dryRun bool) error {
fmt.Printf("Deleted: \"%s\" on %s/%s\n", name, owner, repo)
if dryRun {
return nil
}
_, err := c.githubClient.Issues.DeleteLabel(ctx, owner, repo, name)
return err
}