-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira.go
373 lines (334 loc) · 9.2 KB
/
jira.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
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/andygrunwald/go-jira"
"golang.org/x/sync/errgroup"
)
const defaultMaxResults = 100
// Jira encapsulates interaction with the Jira API. It exposes a subset of the
// possible interactions in order to simplify the workflow tailored to the
// user.
type Jira struct {
client *jira.Client
user *jira.User
config Config
maxResults int
}
// NewJira returns a Jira client based on the given username and password.
func NewJira() (Jira, error) {
config, err := LoadConfig()
if err != nil {
return Jira{}, fmt.Errorf("NewJira: %w", err)
}
tp := jira.BasicAuthTransport{
Username: config.Username,
Password: config.Password,
}
client, err := jira.NewClient(tp.Client(), config.Endpoint)
if err != nil {
return Jira{}, fmt.Errorf("NewClient: %w", err)
}
user, _, err := client.User.GetSelf()
if err != nil {
return Jira{}, fmt.Errorf("GetSelf: %w", err)
}
return Jira{
client: client,
user: user,
config: config,
maxResults: defaultMaxResults,
}, nil
}
// ListIssues fetches all issues according to a specific JQL query.
func (j Jira) ListIssues(ctx context.Context, project string) (Issues, error) {
conditions := []string{
"project = " + project,
"issueType IN (Task, Story, Bug)",
"assignee = \"" + j.user.DisplayName + "\"",
"status NOT IN (Closed, Done)",
}
jql := strings.Join(conditions, " AND ")
issues, err := j.search(ctx, jql)
if err != nil {
return nil, fmt.Errorf("ListIssues: %w", err)
}
return issues, nil
}
// ListSprintIssues fetches all issues assigned to the current sprint.
func (j Jira) ListSprintIssues(ctx context.Context) (Issues, error) {
conditions := []string{
"project = " + j.config.Project,
"issueType IN (Story, Task, Bug)",
"assignee = \"" + j.user.DisplayName + "\"",
"sprint in openSprints()",
}
jql := strings.Join(conditions, " AND ")
issues, err := j.search(ctx, jql)
if err != nil {
return nil, fmt.Errorf("ListSprintIssues: %w", err)
}
return issues, nil
}
// ListEpics returns a list of epics associated with the current project.
func (j Jira) ListEpics(ctx context.Context, project string) (Issues, error) {
conditions := []string{
"project = " + project,
"issueType = Epic",
"assignee = \"" + j.user.DisplayName + "\"",
"status != Closed",
}
// include query for labels if configured
if len(j.config.Labels) > 0 {
label := "labels IN (" + strings.Join(j.config.Labels, ",") + ")"
conditions = append(conditions, label)
}
jql := strings.Join(conditions, " AND ")
issues, err := j.search(ctx, jql)
if err != nil {
return nil, fmt.Errorf("ListEpics: %w", err)
}
return issues, nil
}
// ListInitiatives returns a list of initiatives associated with the current project.
func (j Jira) ListInitiatives(ctx context.Context, project string) (Issues, error) {
conditions := []string{
"project = " + project,
"issueType = Initiative",
"status != Closed",
}
jql := strings.Join(conditions, " AND ")
issues, err := j.search(ctx, jql)
if err != nil {
return nil, fmt.Errorf("ListInitiative: %w", err)
}
return issues, nil
}
// ListSprints fetches all active and future sprints for the configured board
// and the specified keyword.
func (j Jira) ListSprints(boardID int) (Sprints, error) {
sprints, _, err := j.client.Board.GetAllSprintsWithOptions(
boardID,
&jira.GetAllSprintsOptions{
State: "active,future",
},
)
if err != nil {
return nil, err
}
// only return sprints that contain the configured keyword
filtered := make([]jira.Sprint, 0, len(sprints.Values))
for _, s := range sprints.Values {
if strings.Contains(s.Name, j.config.SprintKeyword) {
filtered = append(filtered, s)
}
}
return NewSprints(filtered), nil
}
// GetBoardID returns the board ID for a given project.
func (j Jira) GetBoardID(project string) (int, error) {
req, err := j.client.NewRequest("GET", "/rest/agile/1.0/board?projectKeyOrId="+project, nil)
if err != nil {
return 0, err
}
resp, err := j.client.Do(req, nil)
if err != nil {
return 0, fmt.Errorf("GetBoardID: %w", parseResponseError(resp))
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("GetBoardID.ReadAll: %w", err)
}
var result struct {
Values []struct {
ID int `json:"id"`
} `json:"values"`
}
if err := json.Unmarshal(b, &result); err != nil {
return 0, err
}
return result.Values[0].ID, nil
}
// ListSprintsForBoard returns a list of sprints for the given board ID.
func (j Jira) ListSprintsForBoard(boardID int) (Sprints, error) {
sprints, _, err := j.client.Board.GetAllSprintsWithOptions(
boardID,
&jira.GetAllSprintsOptions{
State: "active,future",
},
)
if err != nil {
return nil, fmt.Errorf("ListSprintsForBoard: %w", err)
}
return NewSprints(sprints.Values), nil
}
// CreateIssues creates the given issues in parallel.
func (j Jira) CreateIssues(ctx context.Context, issues []*jira.Issue) error {
var (
mu sync.Mutex
lastIssueCreated string
)
g, ctx := errgroup.WithContext(ctx)
for _, issue := range issues {
// allocate variable to avoid scope capturing
issue := issue
// create issues concurrency
g.Go(func() error {
newIssue, resp, err := j.client.Issue.CreateWithContext(ctx, issue)
if err != nil {
return parseResponseError(resp)
}
fmt.Printf("Created %s - %s\n", newIssue.Key, issue.Fields.Summary)
mu.Lock()
lastIssueCreated = newIssue.Key
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
data, err := LoadData()
if err != nil {
return err
}
data.LastIssueCreated = lastIssueCreated
return data.WriteFile()
}
func (j Jira) UpdateIssue(ctx context.Context, key string, issue Issue) error {
data := map[string]interface{}{
"update": make(map[string]interface{}),
}
data["update"] = map[string][]map[string]interface{}{
"summary": {
{
"set": issue.Summary,
},
},
}
if issue.SprintID != 0 {
updates := data["update"].(map[string][]map[string]interface{})
updates[j.config.CustomFields.Sprints] = []map[string]interface{}{
{
"set": issue.SprintID,
},
}
}
resp, err := j.client.Issue.UpdateIssueWithContext(ctx, key, data)
if err != nil {
return parseResponseError(resp)
}
fmt.Printf("Updated issue %s\n", key)
return nil
}
// CreateSprint creates a new sprint.
func (j Jira) CreateSprint(name string, month, day, boardID int) error {
// configure start and end date
now := time.Now()
tz := now.Location()
layout := "2006-01-02T15:04:05.000-07:00"
startDate := time.Date(now.Year(), time.Month(month), day, 0, 0, 0, 0, tz)
// define end date based on configured sprint duration
endDate := startDate.Add(time.Duration(j.config.SprintDuration+1) * 24 * time.Hour)
// define payload
payload := struct {
Name string `json:"name"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
OriginBoardID int `json:"originBoardId"`
Goal string `json:"goal,omitempty"`
}{
Name: fmt.Sprintf("%s %d/%d", name, month, day),
StartDate: startDate.Format(layout),
EndDate: endDate.Format(layout),
OriginBoardID: boardID,
}
req, err := j.client.NewRequest("POST", "/rest/agile/1.0/sprint", payload)
if err != nil {
return err
}
resp, err := j.client.Do(req, nil)
if err != nil {
return ErrCreateSprint(parseResponseError(resp).Error())
}
return nil
}
type issueTransition struct {
issueKey string
transition Transition
}
// TransitionIssues performs batch transitions on a set of issues.
func (j Jira) TransitionIssues(ctx context.Context, issueTransitions []issueTransition) error {
g, ctx := errgroup.WithContext(ctx)
for _, t := range issueTransitions {
// allocate variable to avoid scope capturing
t := t
g.Go(func() error {
resp, err := j.client.Issue.DoTransitionWithContext(
ctx,
t.issueKey,
t.transition.ID,
)
if err != nil {
return fmt.Errorf("TranitionIssues: %w", parseResponseError(resp))
}
fmt.Printf("%s - Status changed to %s\n", t.issueKey, t.transition.Name)
return nil
})
}
return g.Wait()
}
func (j Jira) MoveIssuesToBacklog(ctx context.Context, keys []string) error {
if len(keys) == 0 {
return nil
}
req, err := j.client.NewRequest("POST", "/rest/agile/1.0/backlog/issue", map[string]interface{}{
"issues": keys,
})
if err != nil {
return err
}
resp, err := j.client.Do(req, nil)
if err != nil {
return fmt.Errorf("MoveIssuesToBacklog: %w", parseResponseError(resp))
}
for _, key := range keys {
fmt.Printf("%s - Moved to backlog\n", key)
}
return nil
}
func (j Jira) search(ctx context.Context, jql string) (Issues, error) {
var (
result []jira.Issue
startAt int
)
for {
list, resp, err := j.client.Issue.SearchWithContext(ctx, jql, &jira.SearchOptions{
StartAt: startAt,
MaxResults: j.maxResults,
Expand: "transitions",
})
if err != nil {
return nil, fmt.Errorf("search: %w", parseResponseError(resp))
}
result = append(result, list...)
if len(result) >= resp.Total {
break
}
startAt += len(list)
}
return NewIssues(result, j.config.CustomFields)
}
func parseResponseError(resp *jira.Response) error {
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(b))
}