-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira.js
102 lines (83 loc) · 2.92 KB
/
jira.js
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
class Jira {
constructor ({ baseUrl, token, email }) {
this.baseUrl = baseUrl || 'https://mitarbeiterapp.atlassian.net';
this.email = email || '';
this.token = token
}
async getIssue (issueId) {
//console.log('getIssue ' + issueId + ' with baseURL ' + this.baseUrl);
const response = await fetch(`${this.baseUrl}/rest/api/3/issue/${issueId}`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${this.email}:${this.token}`).toString('base64')}`,
},
});
if (!response.ok) {
const errorMsg = await response.text();
throw new Error(errorMsg)
}
return response.json()
}
async getIssueOrSubtaskParentIssue (issueId) {
let issue = await this.getIssue(issueId);
if (issue.fields.issuetype.subtask) {
const parentIssueId = issue.fields.parent.key;
console.log("Update parent story: " + parentIssueId + " instead sub-task: " +issueId);
issue = await this.getIssue(parentIssueId);
}
return issue;
}
async updateIssues ({ issueIds, releaseDate, tagName, componentName, notifyUsers = false }) {
const calls = issueIds.map(async (issueId) => {
try {
const issue = await this.getIssueOrSubtaskParentIssue(issueId);
if (issue.fields.customfield_11108) {
const oldReleaseDate = new Date(issue.fields.customfield_11108);
if (oldReleaseDate > releaseDate) {
releaseDate = oldReleaseDate
}
}
await this.updateIssue({ issue, releaseDate, tagName, componentName, notifyUsers });
return null
} catch (ex) {
return `Unable to update ${issueId}: ${ex.message}`
}
});
const results = await Promise.all(calls);
return results.filter((item) => !!item)
}
// PUT /rest/api/3/issue/{issueIdOrKey}
// see: https://developer.atlassian.com/cloud/jira/platform/rest/v3/?utm_source=%2Fcloud%2Fjira%2Fplatform%2Frest%2F&utm_medium=302#api-rest-api-3-issue-issueIdOrKey-put
async updateIssue ({ issue, releaseDate, tagName, componentName, notifyUsers }) {
const issueId = issue.key;
//console.log('Updating ' + issueId);
const response = await fetch(`${this.baseUrl}/rest/api/3/issue/${issueId}?notifyUsers=${notifyUsers}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(`${this.email}:${this.token}`).toString('base64')}`,
},
body: JSON.stringify({
update: {
labels: [
{
add: componentName,
},
{
add: `${componentName}-${tagName}`,
},
],
},
fields: {
customfield_11108: releaseDate.toISOString(),
},
}),
});
if (!response.ok) {
const errorMsg = await response.text();
throw new Error(errorMsg)
}
console.log('Updated ' + issueId);
}
}
module.exports = Jira;