-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
378 lines (322 loc) · 8.81 KB
/
main.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
374
375
376
377
378
package jiraissue
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
"sync"
)
// JiraIssue represents the structure of the issue to be created
type JiraIssue struct {
Fields Fields `json:"fields"`
}
// Fields represent the Jira issue fields
type Fields struct {
Summary string `json:"summary"`
Description *ContentDef `json:"description,omitempty"`
Components []*FieldProps `json:"components,omitempty"`
Issuetype Issuetype `json:"issuetype"`
Project Project `json:"project"`
Priority FieldProps `json:"priority"`
Assignee FieldProps `json:"assignee"`
Parent *FieldProps `json:"parent,omitempty"` // EpicKey
Labels []string `json:"labels"`
FixVersions []*FixVersion `json:"fixVersions,omitempty"`
// To use this setting is needed to follow the steps here https://community.atlassian.com/t5/Jira-Content-Archive-questions/Unable-to-set-TimeTracking-using-JIRA-API/qaq-p/1917217#M246455
TimeTracking *TimeTracking `json:"timetracking,omitempty"`
}
type FieldProps struct {
Key string `json:"key,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type Issuetype struct {
Name string `json:"name"`
}
type Project struct {
Key string `json:"key"`
}
type TimeTracking struct {
OriginalEstimate string `json:"originalEstimate,omitempty"`
RemainingEstimate string `json:"remainingEstimate,omitempty"`
}
type ContentDef struct {
Type string `json:"type"`
Content []*ContentDef `json:"content,omitempty"`
Text string `json:"text,omitempty"`
Version int `json:"version,omitempty"`
}
type Client struct {
httpClient *http.Client
APIToken string
Debug bool
DryRun bool
}
type FixVersion struct {
Self string `json:"self,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
Archived *bool `json:"archived,omitempty"`
Released *bool `json:"released,omitempty"`
ReleaseDate string `json:"releaseDate,omitempty"`
}
// CreateJiraIssue creates an issue in Jira
func CreateJiraIssue(summary, timeEstimate, description, epicKey, issueType, priorityID, assigneeID, fixVersionName, csvPath string, components, labels []string, isDebugEnabled, isDryRunning bool) error {
// [ ] TODO: refactor params into an options struct for easier arguments management and validate this env vars at cmd.
jiraProjectKey, ok := os.LookupEnv("JIRA_PROJECT_KEY")
if !ok {
log.Fatal("Environment Variable JIRA_PROJECT_KEY not set")
}
jiraAPIToken, ok := os.LookupEnv("JIRA_API_TOKEN")
if !ok {
log.Fatal("Environment Variable JIRA_API_TOKEN not set")
}
var issues = make([]*JiraIssue, 0)
if csvPath != "" {
var errCSV error
issues, errCSV = expandIssuesFromCSV(csvPath, priorityID, issueType, jiraProjectKey, assigneeID)
if errCSV != nil {
return errCSV
}
}
if csvPath == "" && summary == "" {
log.Fatal("When creating single Jira issues --summary flag is required")
}
if csvPath == "" && summary != "" {
issues = append(issues, createIssuePayload(IssuePayloadContent{
summary: summary,
description: description,
issueType: issueType,
jiraProjectKey: jiraProjectKey,
priorityID: priorityID,
assigneeID: assigneeID,
epicKey: epicKey,
timeEstimate: timeEstimate,
components: components,
labels: labels,
fixVersionName: fixVersionName,
}))
}
c := &Client{
httpClient: &http.Client{},
APIToken: jiraAPIToken,
Debug: isDebugEnabled,
DryRun: isDryRunning,
}
ctx := context.Background()
err := handleJiraIssuesCreation(ctx, c, issues)
if err != nil {
return err
}
return nil
}
func createJiraIssueAPICall(ctx context.Context, c *Client, issue *JiraIssue) (string, error) {
issueJSON, err := json.Marshal(issue)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://pagerduty.atlassian.net/rest/api/3/issue", bytes.NewBuffer(issueJSON))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+c.APIToken)
// [ ] TODO: Implement logging in roundtripper
if c.Debug || c.DryRun {
dumpedReq, err := httputil.DumpRequest(req, true)
if err == nil {
log.Println("[DEBUG] Request::>", string(dumpedReq))
}
}
if c.DryRun {
return fmt.Sprintf("%s-dry-run", issue.Fields.Project.Key), nil
}
resp, err := c.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// [ ] TODO: Implement logging in roundtripper
if c.Debug {
dumpedResp, err := httputil.DumpResponse(resp, true)
if err == nil {
log.Println("[DEBUG] Response::>", string(dumpedResp))
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result map[string]interface{}
json.Unmarshal(body, &result)
if resp.StatusCode != http.StatusOK {
if errors, ok := result["errors"]; ok {
errMessage, _ := json.MarshalIndent(errors, "", " ")
return "", fmt.Errorf("Errors: %v", string(errMessage))
}
}
issueID, ok := result["key"].(string)
if !ok {
return "", fmt.Errorf("error parsing issue ID from response")
}
return issueID, nil
}
func handleJiraIssuesCreation(ctx context.Context, c *Client, issues []*JiraIssue) (err error) {
var wg sync.WaitGroup
jirasubdomain := os.Getenv("JIRA_SUBDOMAIN")
for _, iss := range issues {
wg.Add(1)
go func(issue *JiraIssue) {
defer wg.Done()
var issueKey string
issueKey, err = createJiraIssueAPICall(ctx, c, issue)
var successMsg string
if jirasubdomain == "" {
successMsg = fmt.Sprintf("Issue created. %s", issueKey)
}
successMsg = fmt.Sprintf("Issue created. Link to issue https://%s.atlassian.net/browse/%s", jirasubdomain, issueKey)
fmt.Println(successMsg)
}(iss)
if err != nil {
return err
}
}
wg.Wait()
return err
}
type IssuePayloadContent struct {
summary string
description string
issueType string
jiraProjectKey string
priorityID string
assigneeID string
epicKey string
timeEstimate string
components []string
labels []string
fixVersionName string
}
func createIssuePayload(c IssuePayloadContent) *JiraIssue {
var p JiraIssue = JiraIssue{
Fields: Fields{
Summary: c.summary,
Description: &ContentDef{
Content: []*ContentDef{
{
Content: []*ContentDef{
{
Text: c.description,
Type: "text",
},
},
Type: "paragraph",
},
},
Type: "doc",
Version: 1,
},
Issuetype: Issuetype{
Name: c.issueType,
},
Project: Project{
Key: c.jiraProjectKey,
},
Priority: FieldProps{
ID: c.priorityID,
},
Assignee: FieldProps{
ID: c.assigneeID,
},
Parent: &FieldProps{
Key: c.epicKey,
},
// TimeTracking: &TimeTracking{
// OriginalEstimate: c.timeEstimate,
// },
FixVersions: []*FixVersion{
{
Name: c.fixVersionName,
},
},
},
}
issueComponents := []*FieldProps{}
for _, c := range c.components {
issueComponents = append(issueComponents, &FieldProps{Name: c})
}
issueLabels := []string{}
for _, l := range c.labels {
issueLabels = append(issueLabels, l)
}
p.Fields.Components = issueComponents
p.Fields.Labels = issueLabels
return &p
}
func expandIssuesFromCSV(csvPath, priorityID, issueType, jiraProjectKey, assigneeID string) ([]*JiraIssue, error) {
var issues = make([]*JiraIssue, 0)
csvBytes, err := os.ReadFile(csvPath)
if err != nil {
return nil, err
}
csvContent := string(csvBytes)
r := csv.NewReader(strings.NewReader(csvContent))
r.Comma = ';'
r.Comment = '#'
r.TrimLeadingSpace = true
for i := 0; i >= 0; i++ {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if i < 1 {
continue
}
// summary;description;time;epic;components;labels;fixVersionName
var (
summary = record[0]
description = record[1]
timeEstimate = record[2]
epic = record[3]
components = expandListedCellValues(record[4])
labels = expandListedCellValues(record[5])
fixVersionName = record[6]
)
issues = append(
issues,
createIssuePayload(IssuePayloadContent{
summary: summary,
description: description,
issueType: issueType,
jiraProjectKey: jiraProjectKey,
priorityID: priorityID,
assigneeID: assigneeID,
epicKey: epic,
timeEstimate: timeEstimate,
components: components,
labels: labels,
fixVersionName: fixVersionName,
}),
)
}
return issues, nil
}
func expandListedCellValues(r string) []string {
v := strings.Split(r, ",")
var result []string
for _, c := range v {
result = append(result, strings.TrimSpace(c))
}
return result
}