-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
391 lines (324 loc) · 8.54 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
)
func getEnvInt(key string, defaultValue int) int {
tmpValue, err := strconv.Atoi(os.Getenv(key))
if err != nil {
return defaultValue
}
return tmpValue
}
type zonedTimestamp struct {
time.Time
}
func (t *zonedTimestamp) UnmarshalJSON(buf []byte) error {
tt, err := time.Parse("2006-01-02T15:04:05.999-0700", strings.Trim(string(buf), `"`))
if err != nil {
return err
}
t.Time = tt
return nil
}
type User struct {
UserName string `json:"name"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
TimeZone string `json:"timeZone"`
}
type HistoryItem struct {
Field string `json:"field"`
From string `json:"from"`
FromString string `json:"fromString"`
To string `json:"to"`
ToString string `json:"toString"`
}
type History struct {
Id string `json:"id"`
Author User `json:"author"`
Created zonedTimestamp `json:"created"`
Items []HistoryItem `json:"items"`
}
type PagedChangelog struct {
StartAt int `json:"startAt"`
MaxResults int `json:"maxResults"`
Total int `json:"total"`
Histories []History `json:"histories"`
}
//type IssueStatus struct {
// Name string `json:"name"`
//}
type IssueFields struct {
Summary string `json:"summary"`
//Status IssueStatus `json:"status"`
//Updated zonedTimestamp `json:"updated"`
//Assignee User `json:"assignee"`
// NOTE: this custom field name might vary by deployment?
EpicName string `json:"customfield_12024"`
}
type Issue struct {
Id string `json:"id"`
Key string `json:"key"`
Fields IssueFields `json:"fields"`
Changelog PagedChangelog `json:"changelog"`
// computed:
Status string
StatusTime time.Time
Assigned User
StatusBusinessDays int
}
type PagedIssues struct {
StartAt int `json:"startAt"`
MaxResults int `json:"maxResults"`
Total int `json:"total"`
Issues []Issue `json:"issues"`
}
type IssueList []*Issue
func (issues IssueList) Len() int {
return len(issues)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (issues IssueList) Less(i, j int) bool {
return issues[i].StatusBusinessDays > issues[j].StatusBusinessDays
}
// Swap swaps the elements with indexes i and j.
func (issues IssueList) Swap(i, j int) {
issues[i], issues[j] = issues[j], issues[i]
}
func cachedGet(cacheFilename string, url string, cl *http.Client) (issuesJsonBody io.ReadCloser, err error) {
log.Printf("GET '%s'\n", url)
cacheHit := false
cacheAvailable := false
if getEnvInt("JIRA_NOCACHE", 0) == 0 {
stat, statErr := os.Stat(cacheFilename)
cacheHit = statErr == nil || !os.IsNotExist(statErr)
cacheAvailable = cacheHit
if cacheHit && stat != nil {
if stat.ModTime().Before(time.Now().Add(-time.Hour)) {
cacheHit = false
}
}
}
respondCache := func() io.ReadCloser {
if !cacheAvailable {
return nil
}
b, err := ioutil.ReadFile(cacheFilename)
if err != nil {
return nil
}
log.Printf("cached response\n")
issuesJsonBody = ioutil.NopCloser(bytes.NewReader(b))
return issuesJsonBody
}
if cacheHit {
issuesJsonBody = respondCache()
if issuesJsonBody != nil {
return issuesJsonBody, nil
}
cacheHit = false
}
if !cacheHit {
var req *http.Request
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(os.Getenv("JIRA_USERNAME"), os.Getenv("JIRA_PASSWORD"))
var rsp *http.Response
rsp, err = cl.Do(req)
if err != nil {
issuesJsonBody = respondCache()
if issuesJsonBody != nil {
return issuesJsonBody, nil
}
log.Printf("http: %v\n", err)
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode >= 300 {
log.Printf("http: status %s\n", rsp.Status)
issuesJsonBody = respondCache()
if issuesJsonBody != nil {
return issuesJsonBody, nil
}
httpErr := fmt.Errorf("HTTP response %s", rsp.Status)
return nil, httpErr
}
var b []byte
b, err = ioutil.ReadAll(rsp.Body)
if err != nil {
issuesJsonBody = respondCache()
if issuesJsonBody != nil {
return issuesJsonBody, nil
}
return nil, err
}
// cache response in file:
ioutil.WriteFile(cacheFilename, b, 0600)
issuesJsonBody = ioutil.NopCloser(bytes.NewReader(b))
return issuesJsonBody, nil
}
return
}
func main() {
fmt.Println(`environment variables:
JIRA_URL = base URL of JIRA website without trailing slash
JIRA_USERNAME = username to authenticate with
JIRA_PASSWORD = password to authenticate with
JIRA_BOARDID = board ID to query status of
JIRA_JQL = custom JQL filter to apply; default='status not in (closed, canceled, open, reopened, Analysis, "Analysis - 1")'
`)
args := os.Args[1:]
//boardId := 2924
//boardId := 3612
//boardId := 3581
//boardId := 4085
//boardId := 4454
boardId := getEnvInt("JIRA_BOARDID", 4454)
if len(args) >= 1 {
intValue, err := strconv.Atoi(args[0])
if err == nil {
boardId = intValue
}
}
if os.Getenv("JIRA_URL") == "" {
os.Setenv("JIRA_URL", "https://ultidev")
}
jql := os.Getenv("JIRA_JQL")
if jql == "" {
jql = `status not in (closed, canceled, open, reopened, Analysis, "Analysis - 1")`
}
cl := &http.Client{
// Disable TLS cert verification:
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
var issues []Issue
startAt := 0
total := 1
for startAt < total {
cacheFilename := fmt.Sprintf("board.%d.issue.%d.json", boardId, startAt)
jiraUrl := os.ExpandEnv("$JIRA_URL/rest/agile/1.0/board")
url := fmt.Sprintf(
"%s/%d/issue?expand=changelog&startAt=%d&jql=%s",
jiraUrl,
boardId,
startAt,
url.QueryEscape(jql),
)
// Fetch from cache or network:
issuesJsonBody, err := cachedGet(cacheFilename, url, cl)
if err != nil {
log.Fatal(err)
}
// Decode list of issues:
pagedIssues := &PagedIssues{}
dec := json.NewDecoder(issuesJsonBody)
err = dec.Decode(pagedIssues)
if err != nil {
log.Fatal(err)
}
// Advance to next page:
total = pagedIssues.Total
startAt = pagedIssues.StartAt + len(pagedIssues.Issues)
if issues == nil {
issues = make([]Issue, 0, pagedIssues.Total)
}
// Append page:
issues = append(issues, pagedIssues.Issues...)
}
now := time.Now()
today := DateOf(now)
// Discover latest status per issue:
aging := make(map[string][]*Issue)
for i := range issues {
issue := &issues[i]
if issue.Fields.EpicName != "" {
continue
}
issue.StatusTime = time.Unix(0, 0)
for _, history := range issue.Changelog.Histories {
for _, item := range history.Items {
// Ignore any fields except status changes:
if item.Field != "status" {
continue
}
issue.Status = item.ToString
if item.ToString == "In Progress" {
issue.StatusTime = history.Created.Time
}
issue.Assigned = history.Author
}
}
if issue.Status == "" {
continue
}
//if issue.Status == "Open" || issue.Status == "Reopened" || issue.Status == "Closed" {
// continue
//}
// Determine age in business days:
issue.StatusBusinessDays = DateOf(issue.StatusTime).BusinessDaysUntil(today)
// Add to status map:
aging[issue.Status] = append(aging[issue.Status], issue)
}
//keys := []string{
// "In Progress", // In Development
// "In Progress - 1", // PR
// "In Progress - 2", // Ready for QA
// "In Testing", // In Testing
// //"Approved",
//}
names := map[string]string{
"In Progress": "In Development",
"In Progress - 1": "PR",
"In Progress - 2": "Ready for QA",
"In Testing": "In Testing",
}
keys := make([]string, 0, len(aging))
for key := range aging {
keys = append(keys, key)
}
sort.Strings(keys)
timeLayout := "Mon Jan 02"
fmt.Printf("Now: %s\n", now.Format(timeLayout))
for _, status := range keys {
// sort issues by time descending:
statusIssues := IssueList(aging[status])
sort.Sort(statusIssues)
friendlyName, ok := names[status]
if ok {
friendlyName = fmt.Sprintf(" (%s)", friendlyName)
}
fmt.Printf("%s%s: [\n", status, friendlyName)
for _, issue := range statusIssues {
//jb, _ := json.Marshal(issue)
//fmt.Printf("%s\n", string(jb))
time.Now().Sub(issue.StatusTime)
fmt.Printf(
" %20s: %s (%2d days old since %s); %s\n",
issue.Assigned.UserName,
issue.Key,
issue.StatusBusinessDays,
issue.StatusTime.Format(timeLayout),
issue.Fields.Summary,
)
}
fmt.Printf("]\n")
}
}