-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
301 lines (268 loc) · 7.15 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/clok/cdocs"
"github.com/clok/ghlabels/helpers"
"github.com/clok/ghlabels/types"
"github.com/clok/kemba"
"github.com/urfave/cli/v2"
)
var (
version string
client = types.Client{}
k = kemba.New("ghlabels")
)
func main() {
k.Println("executing")
im, err := cdocs.InstallManpageCommand(&cdocs.InstallManpageCommandInput{
AppName: "ghlabels",
Hidden: true,
})
if err != nil {
log.Fatal(err)
}
app := cli.NewApp()
app.Name = "ghlabels"
app.Version = version
app.Usage = "label sync for repos and organizations"
app.Commands = []*cli.Command{
{
Name: "sync",
Usage: "sync labels - rename, sync, delete",
Subcommands: []*cli.Command{
{
Name: "all",
Usage: "Sync labels across ALL repos within an org or for a user",
UsageText: helpers.SyncAllUsageText,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "org",
Aliases: []string{"o"},
Usage: "GitHub Organization to view. Cannot be used with User flag.",
},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "GitHub User to view. Cannot be used with Organization flag.",
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to config file withs labels to sync.",
},
&cli.BoolFlag{
Name: "merge-with-defaults",
Aliases: []string{"m"},
Usage: "Merge provided config with defaults, otherwise only use the provided config.",
},
},
Action: func(c *cli.Context) error {
if err := helpers.ValidateOrgUserArgs(c); err != nil {
return cli.Exit(err, 2)
}
config, err := helpers.DetermineConfig(c)
if err != nil {
return cli.Exit(err, 2)
}
client.GenerateClient()
repos := helpers.GetAllRepos(c, &client)
k.Printf("Found %d repos", len(repos))
qualified := helpers.ExtractQualifiedRepos(repos)
if len(qualified) == 0 {
fmt.Println("No repos to update")
return nil
}
// Confirm?
fmt.Printf("Found %d repos that qualify\n", len(qualified))
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Sync labels on %d qualifying repos?", len(qualified)),
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return cli.Exit(err, 3)
}
if !confirm {
return cli.Exit("No action. Goodbye!", 0)
}
// NOTE: This is expensive. On a large org, it will make many API calls.
for i, repo := range qualified {
err = helpers.SyncLabels(repo, &client, config, i)
if err != nil {
return cli.Exit(err, 2)
}
}
return nil
},
},
{
Name: "repo",
Usage: "Sync labels for a single repo",
UsageText: helpers.SyncRepoUsageText,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: "Repo name including owner. Examlple: clok/ghlabels",
Required: true,
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to config file withs labels to sync.",
},
&cli.BoolFlag{
Name: "merge-with-defaults",
Aliases: []string{"m"},
Usage: "Merge provided config with defaults, otherwise only use the provided config.",
},
},
Action: func(c *cli.Context) error {
kl := k.Extend("sync:repo")
config, err := helpers.DetermineConfig(c)
if err != nil {
return cli.Exit(err, 2)
}
repo := types.NewRepo(c.String("repo"))
kl.Log(repo)
// Confirm?
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Sync labels on the %s repository?", repo.FullName()),
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return cli.Exit(err, 3)
}
if !confirm {
return cli.Exit("No action. Goodbye!", 0)
}
client.GenerateClient()
err = helpers.SyncLabels(repo, &client, config, 0)
if err != nil {
return cli.Exit(err, 2)
}
return nil
},
},
},
},
{
Name: "config",
Usage: "commands for viewing or generating configuration",
Subcommands: []*cli.Command{
{
Name: "defaults",
Usage: "print default labels yaml to STDOUT",
Action: func(c *cli.Context) error {
fmt.Println(helpers.GetDefaultConfig())
return nil
},
},
{
Name: "pull",
Usage: "pull labels from a Repo and print to STDOUT",
UsageText: `
$ ghlabels config generate repo [<owner>/<repo_name>]
Example:
$ ghlabels config generate repo clok/ghlabels
`,
Action: func(c *cli.Context) error {
kl := k.Extend("config:generate:repo")
if c.NArg() <= 0 {
_ = cli.ShowSubcommandHelp(c)
return cli.Exit("ERROR: no repo provided", 2)
}
client.GenerateClient()
repo := types.NewRepo(c.Args().Get(0))
repo.SetLabels(client.GetLabels(repo))
kl.Log(repo)
var labels []types.Label
for _, l := range repo.Labels() {
labels = append(labels, types.Label{
Name: l.GetName(),
Color: l.GetColor(),
Description: l.GetDescription(),
})
}
kl.Log(labels)
config := types.Config{Sync: labels}
kl.Log(config)
output, err := yaml.Marshal(&config)
if err != nil {
return cli.Exit(err, 2)
}
fmt.Printf("\n---\n%s\n", string(output))
return nil
},
},
},
},
{
Name: "stats",
Usage: "prints out repository stats",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "org",
Aliases: []string{"o"},
Usage: "GitHub Organization to view. Cannot be used with User flag.",
},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "GitHub User to view. Cannot be used with Organization flag.",
},
},
Action: func(c *cli.Context) error {
if err := helpers.ValidateOrgUserArgs(c); err != nil {
return cli.Exit(err, 2)
}
client.GenerateClient()
repos := helpers.GetAllRepos(c, &client)
k.Printf("Found %d repos", len(repos))
counts := map[string]int{"archived": 0, "forks": 0, "public": 0, "private": 0, "other": 0}
for _, r := range repos {
switch {
case *r.Archived:
counts["archived"]++
case *r.Fork:
counts["forks"]++
case *r.Private:
counts["private"]++
case *r.Visibility == "public":
counts["public"]++
default:
counts["other"]++
}
}
fmt.Printf("public: %d | private: %d | forks: %d | archived: %d\n", counts["public"], counts["private"], counts["forks"], counts["archived"])
return nil
},
},
im,
}
if os.Getenv("DOCS_MD") != "" {
docs, err := cdocs.ToMarkdown(app)
if err != nil {
panic(err)
}
fmt.Println(docs)
return
}
if os.Getenv("DOCS_MAN") != "" {
docs, err := cdocs.ToMan(app)
if err != nil {
panic(err)
}
fmt.Println(docs)
return
}
err = app.Run(os.Args)
if err != nil {
panic(err)
}
}