/
gerrit-status.go
261 lines (210 loc) · 6.41 KB
/
gerrit-status.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
package main
import (
"fmt"
"strings"
"flag"
g "github.com/sergle/go-gerrit"
"github.com/sergle/go-gerrit/change"
)
const ConfigFile = "gerrit.conf"
var gerrit *g.Gerrit
var theme *ColorTheme
var proj_alias map[string]string
var proj_ci map[string]string
// default limit of concurrent connections
var Concurrent_GETs int = 10
func main() {
only_new, conf_file := parse_args()
cfg, err := ReadConfig(conf_file)
if err != nil {
fmt.Printf("Error reading config - %s\n", err)
return
}
gerrit = g.New(cfg.Gerrit.User, cfg.Gerrit.Password, cfg.Gerrit.Host, cfg.Gerrit.CI)
if cfg.Gerrit.Connections > 0 {
Concurrent_GETs = cfg.Gerrit.Connections
}
theme = NewColorTheme(&cfg.Color)
// format: alias:fullname
proj_alias = make(map[string]string)
for _, alias := range cfg.Project.Alias {
parts := strings.SplitN(alias, ":", 2)
proj_alias[ parts[1] ] = parts[0]
}
// format: alias:CIname
proj_ci = make(map[string]string)
for _, line := range cfg.Project.CI {
parts := strings.SplitN(line, ":", 2)
proj_ci[ parts[0] ] = parts[1]
}
// callback func
ci_name_cb := func(p string) string {
proj, found := proj_alias[p]
if ! found {
proj = p
}
ci, found := proj_ci[proj]
if ! found {
ci = cfg.Gerrit.CI
}
return ci
}
gerrit.SetCICallback(ci_name_cb)
dashboard("?q=owner:self+status:open", only_new)
dashboard("?q=is:reviewer+status:open+-owner:self", only_new)
}
func parse_args() (bool, string) {
var only_new = flag.Bool("u", false, "Show only not reviewed changes")
var conf_file = flag.String("f", ConfigFile, "Path to config file")
flag.Parse()
return *only_new, *conf_file
}
// get list of changes
func dashboard(query_string string, only_new bool) () {
change_list, err := gerrit.FetchChangeList(query_string)
if err != nil {
return
}
num_changes := len(change_list)
fmt.Printf("%sTotal %d changes%s\n", theme.Title, num_changes, theme.Reset)
if num_changes == 0 {
return
}
// get changes in parallel
ch_out := make(chan *change.LongChange, num_changes)
ch_in := make(chan string, num_changes)
// limit concurrent get's
num_workers := Concurrent_GETs
if num_workers > num_changes {
num_workers = num_changes
}
// start 'workers'
for i := 0; i < num_workers; i++ {
go get_change(ch_in, ch_out)
}
for _, change := range change_list {
ch_in <- change.Id
}
var processed = 0;
ch_list := make([]*change.LongChange, 0)
Loop:
for {
select {
case change := <-ch_out:
processed++
ch_list = append(ch_list, change)
if processed == num_changes {
break Loop
}
}
}
// and signal exit to worker goroutines
for i := 0; i < num_workers; i++ {
ch_in <- "EXIT"
}
// sort by Updated date
gerrit.SortChanges(ch_list)
print_change_list(ch_list, only_new)
return
}
func get_change(ch_in <-chan string, ch_out chan<- *change.LongChange) () {
for {
select {
case id := <-ch_in:
if id == "EXIT" {
return
}
detail, err := gerrit.GetChange(id)
if err != nil {
fmt.Printf("Failed to fetch change: %s\n", id)
}
ch_out <- detail
}
}
return
}
func print_change_list(list []*change.LongChange, only_new bool) () {
for _, ch := range list {
if only_new {
skip := false
// skip changes which I already reviewed
for _, p := range ch.Labels.CodeReview.All {
if gerrit.IsMyself(p.Username) && p.Value != 0 {
// reviewed
skip = true
break
}
}
if skip {
continue
}
}
ci_username, verified := gerrit.IsVerified(ch)
var rating int8
if verified < 0 {
rating = -2
} else {
for _, p := range ch.Labels.CodeReview.All {
if p.Value == -1 {
if rating > -1 {
rating = -1
}
} else if p.Value == -2 {
rating = -2
break
} else if p.Value == 1 && rating == 0 {
rating = 1
} else if p.Value == 2 && rating >= 0 {
rating = 2
}
}
}
if ! ch.Mergeable {
fmt.Printf("%s⨂%s ", theme.Bad, theme.Reset);
} else if rating == 2 {
fmt.Printf("%s✔%s ", theme.OK, theme.Reset);
} else if rating == 1 {
fmt.Printf("%s+%s ", theme.OK, theme.Reset);
} else if rating == -1 {
fmt.Printf("%s-%s ", theme.Bad, theme.Reset);
} else if rating == -2 {
fmt.Printf("%s✘%s ", theme.Bad, theme.Reset);
} else {
fmt.Printf(" ");
}
subj := ch.Subject
if len(subj) > 50 {
subj = subj[0:50]
}
proj, found := proj_alias[ch.Project]
if ! found {
proj = ch.Project
}
fmt.Printf("%-10s %-14s %-15s %-50s %s", proj, ch.Branch, ch.Owner.Username, subj, ch.Updated[0:16])
ch_color := theme.Reset
if verified == 1 {
ch_color = theme.Verified
} else if verified == -1 {
ch_color = theme.Bad
} else if verified == change.NotVerified {
ch_color = theme.Absent
}
fmt.Printf(" %s%s%s", ch_color, ci_username, theme.Reset)
for _, p := range ch.Labels.CodeReview.All {
// exclude CI username (shown for verified)
if gerrit.IsCI(p.Username, ch) {
continue
}
rv_color := theme.Reset
if p.Value == -1 || p.Value == -2 {
rv_color = theme.Bad
} else if p.Value == 1 {
rv_color = theme.Plus
} else if p.Value == 2 {
rv_color = theme.OK
}
fmt.Printf(" %s%s%s", rv_color, p.Username, theme.Reset)
}
fmt.Printf("\n")
}
}