/
label.go
149 lines (132 loc) · 3.17 KB
/
label.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 main
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v2"
"github.com/codegangsta/cli"
"github.com/google/go-github/github"
)
var (
showLabelsCommand = cli.Command{
Name: "show-labels",
Usage: "output labels for a project suitable for use in config",
Action: func(c *cli.Context) {
opts, err := NewOptions(c)
if err != nil {
logger.Errorln("Invalid options", err)
os.Exit(1)
}
project := c.Args().First()
err = cmdShowLabels(opts, project)
if err != nil {
SoftExit(opts, err)
}
},
}
setLabelsCommand = cli.Command{
Name: "set-labels",
Usage: "set labels for a project based on our config",
Action: func(c *cli.Context) {
opts, err := NewOptions(c)
if err != nil {
logger.Errorln("Invalid options", err)
os.Exit(1)
}
project := c.Args().First()
err = cmdSetLabels(opts, project)
if err != nil {
SoftExit(opts, err)
}
},
}
)
// cmdShowLabels prints the labels for a project for easy inclusion
// in the config
func cmdShowLabels(opts *Options, project string) error {
tc := AuthClient(opts)
client := github.NewClient(tc)
owner, repo, err := ownerRepo(project)
if err != nil {
return err
}
labels, _, err := client.Issues.ListLabels(owner, repo, &github.ListOptions{})
if err != nil {
return err
}
out := []Label{}
for _, label := range labels {
out = append(out, Label{*label.Name, *label.Color})
}
d, err := yaml.Marshal(out)
if err != nil {
return err
}
fmt.Printf("%s", d)
return nil
}
func cmdSetLabels(opts *Options, target string) error {
tc := AuthClient(opts)
client := github.NewClient(tc)
config, err := LoadConfig(opts)
if err != nil {
return err
}
ourLabels := []Label{}
for _, p := range config.Priorities {
ourLabels = append(ourLabels, Label(p))
}
for _, t := range config.Types {
ourLabels = append(ourLabels, Label(t))
}
ourLabelsMap := map[string]string{}
for _, label := range ourLabels {
ourLabelsMap[label.Name] = label.Color
}
var projects []string
if target == "all" {
projects = config.Projects
} else {
projects = strings.Split(target, " ")
}
for _, project := range projects {
logger.Debugln("Setting labels for:", project)
owner, repo, err := ownerRepo(project)
if err != nil {
return err
}
theirLabels, _, err := client.Issues.ListLabels(owner, repo, &github.ListOptions{})
if err != nil {
return err
}
theirLabelsMap := map[string]github.Label{}
for _, label := range theirLabels {
theirLabelsMap[*label.Name] = label
}
for _, ours := range ourLabels {
theirs, ok := theirLabelsMap[ours.Name]
// check if we already exist but don't have the same color
if ok && *theirs.Color != ours.Color {
*theirs.Color = ours.Color
logger.Debugln(" updating color:", ours.Name)
_, _, err = client.Issues.EditLabel(owner, repo, ours.Name, &theirs)
if err != nil {
return err
}
} else if !ok {
logger.Debugln(" creating:", ours.Name)
_, _, err = client.Issues.CreateLabel(
owner,
repo,
&github.Label{Name: &ours.Name, Color: &ours.Color},
)
if err != nil {
return err
}
} else {
logger.Debugln(" found existing:", ours.Name)
}
}
}
return nil
}