-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraph.go
157 lines (132 loc) · 3.56 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
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
package graph
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"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) {
jql := fmt.Sprintf(`id=%s`, key)
issues, err := getIssuesJQL(jc, jql)
if err != nil {
return issue{}, err
}
if len(issues) == 0 {
return issue{}, errBadStatus{http.StatusNotFound}
}
return issues[0], nil
}
func getEpicInfos(jc jiraClient, keys []string) map[string]epicInfo {
type singleEpicResult struct {
key string
info epicInfo
}
ch := make(chan singleEpicResult)
for _, key := range keys {
go func(key string) {
info, err := getEpicInfo(jc, key)
if err != nil {
log.Printf("failed to get epic info: %v", err)
ch <- singleEpicResult{key: key}
return
}
ch <- singleEpicResult{key: key, info: info}
}(key)
}
result := map[string]epicInfo{}
for _ = range keys {
r := <-ch
result[r.key] = r.info
}
return result
}
type epicInfo struct {
name string
color string
}
func getEpicInfo(jc jiraClient, key string) (epicInfo, error) {
resp, err := jc.Get(fmt.Sprintf("/rest/agile/1.0/epic/%s", key), url.Values{})
if err != nil {
return epicInfo{}, err
}
defer resp.Body.Close()
resultBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return epicInfo{}, err
}
parsed := gjson.ParseBytes(resultBytes)
name := parsed.Get("name").String()
color := parsed.Get("color.key").String()
return epicInfo{name: name, color: color}, nil
}
func getIssues(jc jiraClient, epicKeys ...string) ([]issue, error) {
if len(epicKeys) == 0 {
return nil, errors.New("at least one epic key is required")
}
jql := fmt.Sprintf(`"%s" IN (%s)`, jc.fieldConfig.EpicLink, strings.Join(epicKeys, ","))
return getIssuesJQL(jc, jql)
}
func getMilestoneEpics(jc jiraClient, milestoneKey string) ([]issue, error) {
jql := fmt.Sprintf(`issue IN linkedIssues("%s") AND type=epic`, milestoneKey)
return getIssuesJQL(jc, jql)
}
func getIssuesJQL(jc jiraClient, jql string) ([]issue, error) {
result := []issue{}
epicKeys := map[string]struct{}{}
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)
epicKeys[iss.EpicKey] = struct{}{}
}
total := parsed.Get("total").Int()
if len(result) >= int(total) {
break
}
}
dedupedEpicKeys := make([]string, 0, len(epicKeys))
for k := range epicKeys {
dedupedEpicKeys = append(dedupedEpicKeys, k)
}
log.Printf("JQL %s returned %s", jql, dedupedEpicKeys)
epicToInfo := getEpicInfos(jc, dedupedEpicKeys)
for i := range result {
info := epicToInfo[result[i].EpicKey]
result[i].Color = info.color
result[i].EpicName = info.name
}
return result, nil
}