forked from andrei-m/jira-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
113 lines (92 loc) · 2.44 KB
/
graph.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
package graph
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/tidwall/gjson"
)
func issuesToBlocksGraph(issues []issue) map[string][]string {
blocksGraph := map[string][]string{}
for _, iss := range issues {
for _, blockedBy := range iss.blockedByKeys {
blocksGraph[blockedBy] = append(blocksGraph[blockedBy], iss.Key)
}
_, exists := blocksGraph[iss.Key]
if !exists {
blocksGraph[iss.Key] = []string{}
}
}
return blocksGraph
}
type errBadStatus struct {
statusCode int
}
func (e errBadStatus) Error() string {
return fmt.Sprintf("code: %d", e.statusCode)
}
func getSingleIssue(jc jiraClient, key string) (issue, error) {
q := url.Values{"fields": jc.getRequestFields()}
resp, err := jc.Get(fmt.Sprintf("/rest/api/2/issue/%s", key), q)
if err != nil {
return issue{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return issue{}, errBadStatus{resp.StatusCode}
}
resultBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return issue{}, err
}
parsed := gjson.ParseBytes(resultBytes)
issue := jc.unmarshallIssue(parsed)
if issue.Type == "Epic" {
colorCode, err := getEpicColorCode(jc, key)
if err != nil {
log.Printf("failed to get epic color code: %v", err)
}
issue.Color = colorCode
}
return issue, nil
}
func getEpicColorCode(jc jiraClient, key string) (string, error) {
resp, err := jc.Get(fmt.Sprintf("/rest/agile/1.0/epic/%s", key), url.Values{})
if err != nil {
return "", err
}
defer resp.Body.Close()
resultBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
parsed := gjson.ParseBytes(resultBytes)
color := parsed.Get("color.key").String()
return color, nil
}
func getIssues(jc jiraClient, epicKey string) ([]issue, error) {
jql := fmt.Sprintf(`"Epic Link" = %s`, epicKey)
result := []issue{}
for {
b, err := jc.Search(jql, jc.getRequestFields(), len(result))
if err != nil {
return nil, err
}
parsed := gjson.ParseBytes(b)
for _, parsedIssue := range parsed.Get("issues").Array() {
iss := jc.unmarshallIssue(parsedIssue)
parsedBlocks := parsedIssue.Get(`fields.issuelinks.#[type.name=="Blocks"]#.inwardIssue.key`).Array()
iss.blockedByKeys = make([]string, len(parsedBlocks))
for i := range parsedBlocks {
iss.blockedByKeys[i] = parsedBlocks[i].String()
}
result = append(result, iss)
}
total := parsed.Get("total").Int()
if len(result) >= int(total) {
break
}
}
return result, nil
}