forked from babarot/github-labeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
188 lines (162 loc) · 3.95 KB
/
cli.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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/b4b4r07/github-labeler/pkg/github"
"github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
yaml "gopkg.in/yaml.v2"
)
type CLI struct {
Stdout io.Writer
Stderr io.Writer
Option Option
Defined Config
Actual Config
GitHub *github.Client
}
type Option struct {
DryRun bool `long:"dry-run" description:"Just dry run"`
Config string `long:"config" description:"Path to YAML file that labels are defined" default:"labels.yaml"`
Import bool `long:"import" description:"Import existing labels if enabled"`
Version bool `long:"version" description:"Show version"`
}
func (c *CLI) Run(args []string) error {
if c.Option.Version {
fmt.Fprintf(c.Stdout, "%s (%s)\n", Version, Revision)
return nil
}
cfg, err := loadConfig(c.Option.Config)
if err != nil {
return err
}
c.Defined = cfg
client, err := github.NewClient(c.Option.DryRun)
if err != nil {
return err
}
c.GitHub = client
if err := c.Validate(); err != nil {
// fmt.Fprintf(c.Stderr, "Note: %s\n", err)
// return nil
return err
}
if c.Option.Import {
return c.Import()
}
return c.Sync()
}
// applyLabels creates/edits labels described in YAML
func (c *CLI) applyLabels(owner, repo string, label github.Label) error {
ghLabel, err := c.GitHub.GetLabel(owner, repo, label)
if err != nil {
return c.GitHub.CreateLabel(owner, repo, label)
}
if ghLabel.Description != label.Description || ghLabel.Color != label.Color {
return c.GitHub.EditLabel(owner, repo, label)
}
return nil
}
// deleteLabels deletes the label not described in YAML but exists on GitHub
func (c *CLI) deleteLabels(owner, repo string) error {
labels, err := c.GitHub.ListLabels(owner, repo)
if err != nil {
return err
}
for _, label := range labels {
if c.Defined.checkIfRepoHasLabel(owner+"/"+repo, label.Name) {
// no need to delete
continue
}
err := c.GitHub.DeleteLabel(owner, repo, label)
if err != nil {
return err
}
}
return nil
}
func (c *CLI) syncLabels(repo github.Repo) error {
slugs := strings.Split(repo.Name, "/")
if len(slugs) != 2 {
return fmt.Errorf("repository name %q is invalid", repo.Name)
}
for _, labelName := range repo.Labels {
label, err := c.Defined.getDefinedLabel(labelName)
if err != nil {
return err
}
err = c.applyLabels(slugs[0], slugs[1], label)
if err != nil {
return err
}
}
return c.deleteLabels(slugs[0], slugs[1])
}
func checkLabelDuplication(labels []github.Label, target github.Label) bool {
for _, label := range labels {
if cmp.Equal(label, target) {
return true
}
}
return false
}
func (c *CLI) Validate() error {
if len(c.Defined.Repos) == 0 {
return fmt.Errorf("no repos found in %s", c.Option.Config)
}
var cfg Config
for _, repo := range c.Defined.Repos {
slugs := strings.Split(repo.Name, "/")
if len(slugs) != 2 {
// TODO: log
continue
}
labels, err := c.GitHub.ListLabels(slugs[0], slugs[1])
if err != nil {
log.Printf("[ERROR] failed to fetch labels %s/%s: %v", slugs[0], slugs[1], err)
continue
}
var ls []string
for _, label := range labels {
ls = append(ls, label.Name)
}
repo.Labels = ls
cfg.Repos = append(cfg.Repos, repo)
for _, label := range labels {
if checkLabelDuplication(cfg.Labels, label) {
log.Printf("[WARN] %s is duplicate, so skip to add", label.Name)
continue
}
cfg.Labels = append(cfg.Labels, label)
}
}
// used for Import func
c.Actual = cfg
if cmp.Equal(cfg, c.Defined) {
return errors.New("existing labels and defined labels are the same")
}
return nil
}
func (c *CLI) Import() error {
f, err := os.Create(c.Option.Config)
if err != nil {
return err
}
defer f.Close()
return yaml.NewEncoder(f).Encode(&c.Actual)
}
// Sync syncs labels based on YAML
func (c *CLI) Sync() error {
var eg errgroup.Group
for _, repo := range c.Defined.Repos {
repo := repo
eg.Go(func() error {
return c.syncLabels(repo)
})
}
return eg.Wait()
}