-
Notifications
You must be signed in to change notification settings - Fork 34
/
Jira.js
292 lines (257 loc) · 8.71 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
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
import "@babel/polyfill";
import JiraApi from 'jira-client';
import PromiseThrottle from 'promise-throttle';
import Slack from './Slack';
const promiseThrottle = new PromiseThrottle({
requestsPerSecond: 10,
promiseImplementation: Promise
});
/**
* Generate changelog by matching source control commit logs to jiar tickets.
*/
export default class Jira {
constructor(config) {
this.config = config;
this.slack = new Slack(config);
this.jira = undefined;
this.releaseVersions = [];
this.ticketPromises = {};
const { host, username, password} = config.jira.api;
let { email, token } = config.jira.api;
if (!token && typeof password !== 'undefined') {
console.warn('WARNING: Jira password is deprecated. Use an API token instead.');
token = password
}
if (!email && typeof username !== 'undefined') {
console.warn('WARNING: Jira username is deprecated for API authentication. Use user email instead.');
email = username
}
if (config.jira.api.host) {
this.jira = new JiraApi({
host,
username: email,
password: token,
protocol: 'https',
apiVersion: 2,
strictSSL: true
});
} else {
console.error('ERROR: Cannot configure Jira without a host configuration.');
}
}
/**
* Generate changelog by matching source control commit logs to jira tickets
* and, optionally, creating the release version.
*
* @param {Array} commitLogs - A list of source control commit logs.
* @param {String} releaseVersion - The name of the release version to create.
* @return {Object}
*/
async generate(commitLogs, releaseVersion) {
const logs = [];
this.releaseVersions = [];
try {
const promises = commitLogs.map((commit) => {
return this.findJiraInCommit(commit, releaseVersion)
.then((log) => { logs.push(log); });
});
promises.push(Promise.resolve()); // ensure at least one
return Promise.all(promises)
// Add to release version
.then(() => {
// Get all Jira tickets (filter out duplicates by keying on ID)
let ticketsHash = {};
let ticketsList = [];
logs.forEach((log) => {
log.tickets.forEach(ticket => ticketsHash[ticket.id] = ticket);
});
ticketsList = Object.keys(ticketsHash).map(k => ticketsHash[k]);
// If there are Jira tickets, create a release for them
if (ticketsList.length && releaseVersion) {
return this.addTicketsToReleaseVersion(ticketsList, releaseVersion).then(() => logs);
}
return logs;
});
} catch(e) {
throw new Error(e);
}
}
/**
* Find JIRA ticket numbers in a commit log, and automatically load the
* ticket info for it.
*
* @param {Object} commitLog - Commit log object
* @param {String} releaseVersion - Release version eg, mobileweb-1.8.0
* @return {Promsie} Resolves to an object with a jira array property
*/
findJiraInCommit(commitLog) {
const log = Object.assign({tickets: []}, commitLog);
const promises = [];
const found = {};
const configPattern = this.config.jira.ticketIDPattern;
const ticketPattern = new RegExp(configPattern.source, configPattern.flags.replace('g', ''));
// Search for jira ticket numbers in the commit text
const tickets = this.getTickets(log);
tickets.forEach((ticketMatch) => {
// Get the ticket key, and skip loading if we already got this one
let key = ticketMatch.match(ticketPattern);
key = (key.length > 1) ? key[1] : key[0];
if (!key) {
return;
}
key = key.toUpperCase();
if (found[key]){
return;
}
found[key] = true;
promises.push(
this.getJiraTicketForCommit(log, key).catch(() => {}) // ignore errors
);
});
// Resolve log when all jira promises are done
return Promise.all(promises).then(() => log);
}
/**
* Load a Jira issue ticket for a commit object
*
* @param {Object} commitLog - Commit log object
* @param {String} ticketKey - The Jira ticket ID key
*
* @return {Promise}
*/
getJiraTicketForCommit(commitLog, ticketKey) {
if (!ticketKey) {
return Promise.resolve();
}
// Get Jira issue ticket object
let promise = this.ticketPromises[ticketKey];
if (!promise) {
promise = promiseThrottle.add(
this.getJiraIssue.bind(this, ticketKey)
);
promise.catch(() => {
console.log(`Ticket ${ticketKey} not found`);
});
this.ticketPromises[ticketKey] = promise;
}
// Add to commit
promise.then((ticket) => {
if (this.includeTicket(ticket)) {
commitLog.tickets.push(ticket);
return ticket;
}
});
return promise;
}
/**
* Creates a release version and assigns tickets to it.
*
* @param {Array} ticket - List of Jira ticket objects
* @param {String} versionName - The name of the release version to add the ticket to.
* @return {Promise}
*/
async addTicketsToReleaseVersion(tickets, versionName) {
const versionPromises = {};
this.releaseVersions = [];
// Create version and add it to a ticket
function updateTicketVersion(ticket) {
const project = ticket.fields.project.key;
// Create version on project
let verPromise;
if (versionPromises[project]) {
verPromise = versionPromises[project];
} else {
verPromise = this.createProjectVersion(versionName, project);
versionPromises[project] = verPromise;
// Add to list of releases
verPromise.then((ver) => {
ver.projectKey = project;
this.releaseVersions.push(ver);
});
}
// Add version to ticket
return verPromise
.then((versionObj) => {
const { fixVersions} = ticket.fields;
fixVersions.push({ name: versionObj.name });
return this.jira.updateIssue(ticket.id, {
fields: { fixVersions }
})
});
}
// Loop through tickets and throttle the promises.
const promises = tickets.map((ticket) => {
return promiseThrottle
.add(updateTicketVersion.bind(this, ticket))
.catch((err) => {
console.log(JSON.stringify(err, null, ' '));
console.log(`Could not assign ticket ${ticket.key} to release '${versionName}'!`);
});
});
return Promise.all(promises);
}
/**
* Add a version to a single project, if it doesn't current exist
* @param {String} versionName - The version name
* @param {Array} projectKey - The project key
* @return {Promise<String>} Resolves to version name string, as it exists in JIRA
*/
async createProjectVersion(versionName, projectKey) {
let searchName = versionName.toLowerCase();
const versions = await this.jira.getVersions(projectKey);
const exists = versions.find(v => v.name.toLowerCase() == searchName);
if (exists) {
return exists;
}
const result = await this.jira.createVersion({
name: versionName,
project: projectKey
});
return result;
}
/**
* Retreive the jira issue by ID.
* Also attempt to match a slack user to the reporter's email address.
*
* @param {String} ticketId - The ticket ID of the issue to retrieve.
* @return {Promise} Resolves a jira issue object, with added `slackUser` property.
*/
async getJiraIssue(ticketId) {
if (!this.jira) {
return Promise.reject('Jira is not configured.');
}
return this.jira.findIssue(ticketId).then((origTicket) => {
const ticket = Object.assign({}, origTicket);
return this.slack.findUser(ticket.fields.reporter.emailAddress, ticket.fields.reporter.displayName)
.then((slackUser) => {
ticket.slackUser = slackUser;
return ticket;
})
.catch(() => ticket);
});
}
/**
* Should ticket be included in changelog
* @param {Object} ticket - Jira ticket object
* @returns {Boolean}
*/
includeTicket(ticket) {
const type = ticket.fields.issuetype.name;
if (Array.isArray(this.config.jira.includeIssueTypes) && this.config.jira.includeIssueTypes.length) {
return this.config.jira.includeIssueTypes.includes(type);
}
else if (Array.isArray(this.config.jira.excludeIssueTypes)) {
return !this.config.jira.excludeIssueTypes.includes(type);
}
}
/**
* Gets all tickets associated with a commit
* @param {Object} log - A commit's log object
* @returns {Array} List of tickets in commit
*/
getTickets(log) {
const configPattern = this.config.jira.ticketIDPattern;
const searchPattern = new RegExp(configPattern.source, `${configPattern.flags || ''}g`);
return log.fullText.match(searchPattern) || [];
}
}