forked from kennylevinsen/jirafs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
354 lines (307 loc) · 8.66 KB
/
utils.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
package main
import (
"fmt"
"net/url"
"strings"
"github.com/andygrunwald/go-jira"
"github.com/joushou/qp"
)
type SearchResult struct {
Issues []jira.Issue `json:"issues"`
}
func GetProject(jc *Client, projectKey string) (*jira.Project, error) {
var project jira.Project
url := fmt.Sprintf("/rest/api/2/project/%s", projectKey)
if err := jc.RPC("GET", url, nil, &project); err != nil {
return nil, fmt.Errorf("could not query projects: %v", err)
}
return &project, nil
}
func GetProjects(jc *Client) ([]jira.Project, error) {
var projects []jira.Project
if err := jc.RPC("GET", "/rest/api/2/project", nil, &projects); err != nil {
return nil, fmt.Errorf("could not query projects: %v", err)
}
return projects, nil
}
func GetTypesForProject(jc *Client, projectKey string) ([]string, error) {
p, err := GetProject(jc, projectKey)
if err != nil {
return nil, err
}
ss := make([]string, len(p.IssueTypes))
for i, tp := range p.IssueTypes {
ss[i] = tp.Name
}
return ss, nil
}
func GetKeysForSearch(jc *Client, query string, max int) ([]string, error) {
var s SearchResult
url := fmt.Sprintf("/rest/api/2/search?fields=key&maxResults=%d&jql=%s", max, url.QueryEscape(query))
if err := jc.RPC("GET", url, nil, &s); err != nil {
return nil, fmt.Errorf("could not execute search: %v", err)
}
ss := make([]string, len(s.Issues))
for i, issue := range s.Issues {
ss[i] = issue.Key
}
return ss, nil
}
func GetKeysForNIssuesInProject(jc *Client, project string, max int) ([]string, error) {
var s SearchResult
url := fmt.Sprintf("/rest/api/2/search?fields=key&maxResults=%d&jql=project=%s", max, project)
if err := jc.RPC("GET", url, nil, &s); err != nil {
return nil, fmt.Errorf("could not execute search: %v", err)
}
ss := make([]string, len(s.Issues))
for i, issue := range s.Issues {
s := strings.Split(issue.Key, "-")
if len(s) != 2 {
continue
}
ss[i] = s[1]
}
return ss, nil
}
func GetIssue(jc *Client, key string) (*jira.Issue, error) {
var i jira.Issue
u := fmt.Sprintf("/rest/api/2/issue/%s", key)
if err := jc.RPC("GET", u, nil, &i); err != nil {
return nil, fmt.Errorf("could not query issue: %v", err)
}
return &i, nil
}
type CreateIssueResult struct {
ID string `json:"id,omitempty"`
Key string `json:"key,omitempty"`
}
func CreateIssue(jc *Client, issue *jira.Issue) (string, error) {
var cir CreateIssueResult
if err := jc.RPC("POST", "/rest/api/2/issue", issue, &cir); err != nil {
return "", fmt.Errorf("could not create issue: %v", err)
}
return cir.Key, nil
}
func DeleteIssue(jc *Client, issue string) error {
url := fmt.Sprintf("/rest/api/2/issue/%s", issue)
if err := jc.RPC("DELETE", url, nil, nil); err != nil {
return fmt.Errorf("could not delete issue: %v", err)
}
return nil
}
func DeleteIssueLink(jc *Client, issueLinkID string) error {
url := fmt.Sprintf("/rest/api/2/issueLink/%s", issueLinkID)
if err := jc.RPC("DELETE", url, nil, nil); err != nil {
return fmt.Errorf("could not delete issue link: %v", err)
}
return nil
}
func LinkIssues(jc *Client, inwardKey, outwardKey, relation string) error {
issueLink := &jira.IssueLink{
Type: jira.IssueLinkType{
Name: relation,
},
InwardIssue: &jira.Issue{
Key: inwardKey,
},
OutwardIssue: &jira.Issue{
Key: outwardKey,
},
}
if err := jc.RPC("POST", "/rest/api/2/issueLink", issueLink, nil); err != nil {
return fmt.Errorf("could not create issue link: %v", err)
}
return nil
}
func GetWorklogForIssue(jc *Client, issue string) (*jira.Worklog, error) {
var w jira.Worklog
url := fmt.Sprintf("/rest/api/2/issue/%s/worklog", issue)
if err := jc.RPC("GET", url, nil, &w); err != nil {
return nil, fmt.Errorf("could not get worklog: %v", err)
}
return &w, nil
}
func GetSpecificWorklogForIssue(jc *Client, issue, worklog string) (*jira.WorklogRecord, error) {
var w jira.WorklogRecord
url := fmt.Sprintf("/rest/api/2/issue/%s/worklog/%s", issue, worklog)
if err := jc.RPC("GET", url, nil, &w); err != nil {
return nil, fmt.Errorf("could not get worklog: %v", err)
}
return &w, nil
}
type Transition struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Fields *jira.IssueFields `json:"fields,omitempty"`
}
type TransitionResult struct {
Transitions []Transition `json:"transitions,omitempty"`
}
func GetTransitionsForIssue(jc *Client, issue string) ([]Transition, error) {
var tr TransitionResult
url := fmt.Sprintf("/rest/api/2/issue/%s/transitions", issue)
if err := jc.RPC("GET", url, nil, &tr); err != nil {
return nil, fmt.Errorf("could not get transitions: %v", err)
}
return tr.Transitions, nil
}
func TransitionIssue(jc *Client, issue, transition string) error {
transition = strings.Replace(transition, "\n", "", -1)
transitions, err := GetTransitionsForIssue(jc, issue)
if err != nil {
return err
}
var id string
for _, t := range transitions {
if transition == t.Name {
id = t.ID
break
}
}
if id == "" {
return fmt.Errorf("no such transition")
}
post := map[string]interface{}{
"transition": map[string]interface{}{
"id": id,
},
}
url := fmt.Sprintf("/rest/api/2/issue/%s/transitions", issue)
if err := jc.RPC("POST", url, post, nil); err != nil {
return fmt.Errorf("could not transition issue: %v", err)
}
return nil
}
func SetIssueRaw(jc *Client, issueNo string, b []byte) error {
url := fmt.Sprintf("/rest/api/2/issue/%s", issueNo)
if err := jc.RPC("PUT", url, b, nil); err != nil {
return fmt.Errorf("could not set issue: %v", err)
}
return nil
}
func SetFieldInIssue(jc *Client, issue, field, val string) error {
switch field {
case "type":
field = "issuetype"
}
url := fmt.Sprintf("/rest/api/2/issue/%s", issue)
method := "PUT"
var value interface{}
if val == "" {
value = nil
} else {
value = val
}
fields := make(map[string]interface{})
post := map[string]interface{}{
"fields": fields,
}
switch field {
case "labels":
var labels []string
if val != "" && val != "\n" {
labels = strings.Split(val, "\n")
if labels[len(labels)-1] == "" {
labels = labels[:len(labels)-1]
}
}
fields[field] = labels
case "components":
componentThing := []map[string]string{}
components := strings.Split(val, "\n")
for _, s := range components {
if s == "" || s == "\n" {
continue
}
thing := map[string]string{
"name": s,
}
componentThing = append(componentThing, thing)
}
fields[field] = componentThing
case "issuetype", "assignee", "reporter", "creator", "priority", "resolution":
fields[field] = map[string]interface{}{
"name": value,
}
default:
fields[field] = value
}
if err := jc.RPC(method, url, post, nil); err != nil {
return fmt.Errorf("could not set field for issue: %v", err)
}
return nil
}
type CommentResult struct {
Comments []jira.Comment `json:"comments,omitempty"`
}
func GetCommentsForIssue(jc *Client, issue string) ([]string, error) {
var cr CommentResult
url := fmt.Sprintf("/rest/api/2/issue/%s/comment?maxResults=1000", issue)
if err := jc.RPC("GET", url, nil, &cr); err != nil {
return nil, fmt.Errorf("could not get comments: %v", err)
}
var ss []string
for _, c := range cr.Comments {
ss = append(ss, c.ID)
}
return ss, nil
}
func GetComment(jc *Client, issue, id string) (*jira.Comment, error) {
var c jira.Comment
url := fmt.Sprintf("/rest/api/2/issue/%s/comment/%s", issue, id)
if err := jc.RPC("GET", url, nil, &c); err != nil {
return nil, fmt.Errorf("could not get comment: %v", err)
}
return &c, nil
}
func SetComment(jc *Client, issue, id, body string) error {
c := jira.Comment{
Body: body,
}
url := fmt.Sprintf("/rest/api/2/issue/%s/comment/%s", issue, id)
if err := jc.RPC("PUT", url, c, nil); err != nil {
return fmt.Errorf("could not set comment: %v", err)
}
return nil
}
func AddComment(jc *Client, issue, body string) error {
c := jira.Comment{
Body: body,
}
url := fmt.Sprintf("/rest/api/2/issue/%s/comment/", issue)
if err := jc.RPC("POST", url, c, nil); err != nil {
return fmt.Errorf("could not add comment: %v", err)
}
return nil
}
func RemoveComment(jc *Client, issue, id string) error {
url := fmt.Sprintf("/rest/api/2/issue/%s/comment/%s", issue, id)
if err := jc.RPC("DELETE", url, nil, nil); err != nil {
return fmt.Errorf("could not delete comment: %v", err)
}
return nil
}
func StringsToStats(strs []string, Perm qp.FileMode, user, group string) []qp.Stat {
var stats []qp.Stat
for _, str := range strs {
stat := qp.Stat{
Name: str,
UID: user,
GID: group,
MUID: user,
Mode: Perm,
}
stats = append(stats, stat)
}
return stats
}
func StringExistsInSets(str string, sets ...[]string) bool {
for _, set := range sets {
for _, s := range set {
if str == s {
return true
}
}
}
return false
}