-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira.go
328 lines (282 loc) · 8.24 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
package jt
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type JiraConfig struct {
URL string
Email string
Token string
}
type JiraClient struct {
c *http.Client
config JiraConfig
}
type basicAuthTransport struct {
username string
password string
}
func (t *basicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.SetBasicAuth(t.username, t.password)
return http.DefaultTransport.RoundTrip(req)
}
func NewJiraClient(conf JiraConfig) *JiraClient {
return &JiraClient{
c: &http.Client{
Transport: &basicAuthTransport{
username: conf.Email,
password: conf.Token,
},
},
config: conf,
}
}
type IssueConfig struct {
Summary string
Description string
ProjectKey string
IssueType string
ComponentNames []string
ParentIssueKey string
}
type CreateIssueRequest struct {
Fields Fields `json:"fields,omitempty"`
Update struct {
Labels []string `json:"labels,omitempty"`
} `json:"update"`
}
const (
IssueTypeBug = "Bug"
IssueTypeTask = "Task"
IssueTypeStory = "Story"
IssueTypeEpic = "Epic"
IssueTypeSubTask = "Sub-task"
IssueTypeInitiative = "Initiative"
)
type Field string
const (
FieldSummary Field = "summary"
FieldDescription Field = "description"
FieldProject Field = "project"
FieldIssuetype Field = "issuetype"
FieldComponents Field = "components"
FieldParent Field = "parent"
)
type Fields struct {
Components []Components `json:"components,omitempty"`
Issuetype Issuetype `json:"issuetype,omitempty"`
Parent *Parent `json:"parent,omitempty"`
Project Project `json:"project,omitempty"`
Description *Description `json:"description,omitempty"`
Summary string `json:"summary,omitempty"`
}
type Components struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type Issuetype struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Subtask bool `json:"subtask"`
}
type Parent struct {
Key string `json:"key,omitempty"`
}
type Project struct {
ID string `json:"id,omitempty"`
Key string `json:"key,omitempty"`
}
type ContentBlock struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
}
type Attrs struct {
}
type Content struct {
Type string `json:"type,omitempty"`
Content []ContentBlock `json:"content,omitempty"`
}
type Description struct {
Version int `json:"version,omitempty"`
Type string `json:"type,omitempty"`
Content []Content `json:"content,omitempty"`
}
type JQLSearchRequest struct {
JQL string `json:"jql"`
IncludedFields []Field `json:"fields"`
NextPageToken string `json:"nextPageToken,omitempty"`
}
type JQLSearchResponse struct {
Issues []Issue `json:"issues"`
NextPageToken string `json:"nextPageToken,omitempty"`
}
type Issue struct {
ID string `json:"id"`
Key string `json:"key"`
Self string `json:"self"`
Fields Fields `json:"fields,omitempty"`
}
type Component struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
type CreatedIssueResponse struct {
ID string `json:"id"`
Key string `json:"key"`
Self string `json:"self"`
Transition struct {
Status int `json:"status"`
ErrorCollection struct {
ErrorMessages []string `json:"errorMessages"`
Errors struct{} `json:"errors"`
} `json:"errorCollection"`
} `json:"transition"`
ErrorMessages []string `json:"errorMessages"`
Errors map[string]string `json:"errors"`
}
// NewJIRAIssue creates a new JIRA issue using the JIRA REST API v3.
// The function returns the key of the created issue and an error if the issue could not be created.
// https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-post
func (jc JiraClient) NewJIRAIssue(conf IssueConfig) (string, error) {
// Build the body of the request using a CreateIssueRequest
reqBody := CreateIssueRequest{}
reqBody.Fields.Summary = conf.Summary
reqBody.Fields.Project.Key = conf.ProjectKey
reqBody.Fields.Issuetype.Name = conf.IssueType
reqBody.Fields.Components = make([]Components, len(conf.ComponentNames))
for i, name := range conf.ComponentNames {
reqBody.Fields.Components[i].Name = name
}
if conf.Description != "" {
reqBody.Fields.Description = setDescription(conf.Description)
}
if conf.ParentIssueKey != "" {
reqBody.Fields.Parent = &Parent{Key: conf.ParentIssueKey}
}
jsonBody, err := json.MarshalIndent(reqBody, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal body, %w", err)
}
req, err := http.NewRequest("POST", jc.config.URL+"/rest/api/3/issue", bytes.NewReader(jsonBody))
if err != nil {
return "", fmt.Errorf("failed to create request, %w", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := jc.c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read body, %w", err)
}
createResponse := CreatedIssueResponse{}
err = json.Unmarshal(b, &createResponse)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response, %w", err)
}
if resp.StatusCode != http.StatusCreated {
var errMsg string
if len(createResponse.ErrorMessages) != 0 {
errMsg = strings.Join(createResponse.ErrorMessages, ", ")
} else {
for k, v := range createResponse.Errors {
errMsg = errMsg + fmt.Sprintf("%s: %s", k, v)
}
}
return "", fmt.Errorf("non-200 status %d, %s", resp.StatusCode, errMsg)
}
return createResponse.Key, nil
}
func setDescription(msg string) *Description {
desc := Description{
Type: "doc",
Version: 1,
Content: []Content{
{
Type: "paragraph",
Content: []ContentBlock{
{
Type: "text",
Text: msg,
},
},
},
},
}
return &desc
}
// SearchJiraIssues searches for JIRA issues using the JIRA REST API v3.
// The function returns a slice of JQLSearchResponse and an error if the search request failed.
func (jc JiraClient) SearchJiraIssues(jqlReq JQLSearchRequest) ([]Issue, error) {
var allIssues []Issue
var nextPageToken string
for {
reqBody := struct {
JQL string `json:"jql"`
Fields []string `json:"fields"`
NextPageToken string `json:"nextPageToken,omitempty"`
}{
JQL: jqlReq.JQL,
Fields: convertFields(jqlReq.IncludedFields),
NextPageToken: nextPageToken,
}
queryResp, err := jc.doJiraSearchRequest(reqBody)
if err != nil {
return nil, fmt.Errorf("search request failed: %w", err)
}
allIssues = append(allIssues, queryResp.Issues...)
// Check for the next page token
if queryResp.NextPageToken == "" {
break // No more pages, exit the loop
}
nextPageToken = queryResp.NextPageToken
}
return allIssues, nil
}
// doJiraSearchRequest is a helper to perform the request and handle pagination token
func (jc JiraClient) doJiraSearchRequest(reqBody interface{}) (JQLSearchResponse, error) {
var queryResp JQLSearchResponse
jsonBody, err := json.Marshal(reqBody)
if err != nil {
return queryResp, fmt.Errorf("failed to marshal body, %w", err)
}
req, err := http.NewRequest("POST", jc.config.URL+"/rest/api/3/search/jql", bytes.NewReader(jsonBody))
if err != nil {
return queryResp, fmt.Errorf("failed to create request, %w", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := jc.c.Do(req)
if err != nil {
return queryResp, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return queryResp, fmt.Errorf("failed to read body, %w", err)
}
if resp.StatusCode != http.StatusOK {
return queryResp, fmt.Errorf("non-200 status %d\nmessage: %s", resp.StatusCode, string(b))
}
if err := json.Unmarshal(b, &queryResp); err != nil {
return queryResp, fmt.Errorf("failed to unmarshal response: %w", err)
}
return queryResp, nil
}
// convertFields converts the IncludedFields to a slice of strings
func convertFields(fields []Field) []string {
result := make([]string, len(fields))
for i, f := range fields {
result[i] = string(f)
}
return result
}