-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTicketKeys.js
61 lines (53 loc) · 2.23 KB
/
getTicketKeys.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
const bent = require('bent');
/**
* Given a list of Jira project prefixes, returns an array of unique ticket keys
* contained in the pull request title, source branch name and commit messages.
* @param {string} jiraPrefixes comma separated list of Jira project prefix codes
* @param {string} pullRequestTitle GitHub title for pull request
* @param {string} pullRequestSource GitHub source branch for pull request
* @param {string} commitsUrl URL for Jira endpoint used to obtain commit
* messages associated with pull request
* @param {string} githubToken Authentiction token to access GitHub endpoints
* @returns {Array} Jira ticket keys
*/
const getTicketKeys = async (jiraPrefixes, pullRequestTitle, pullRequestSource,
commitsUrl, githubToken) => {
// Define regular expressions patterns to find Jira Ticket IDs
const prefixes = jiraPrefixes.replace(/,/g, '|');
const ticketKeyAtBeginning = new RegExp(`^(${prefixes})-\\d+`, 'g');
const ticketKeyAnywhere = new RegExp(`\\b(${prefixes})-\\d+`, 'g');
// Define endpoint used to obtain commit messages
const getCommits = bent(commitsUrl, 'GET', 'json');
const gitHeader = {
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'node/12',
Authorization: `bearer ${githubToken}`
};
try {
// Retrieve commit messages and extract any Jira Ticket Keys
let commitsArray = await getCommits(null, null, gitHeader);
let ticketKeys = [];
for (let element of commitsArray) {
let ticketKeysInMessage = element.commit && element.commit.message ?
element.commit.message.match(ticketKeyAnywhere) : undefined ;
if (ticketKeysInMessage) {
ticketKeys = ticketKeys.concat(ticketKeysInMessage);
}
}
// Get source branch, PR title and extract any Jira Ticket Keys
let ticketKeyInSource = pullRequestSource.match(ticketKeyAtBeginning);
if (ticketKeyInSource) {
ticketKeys = ticketKeys.concat(ticketKeyInSource);
}
let ticketKeyInTitle = pullRequestTitle.match(ticketKeyAtBeginning);
if (ticketKeyInTitle) {
ticketKeys = ticketKeys.concat(ticketKeyInTitle);
}
// remove duplicates and return array of ticket keys
return [...new Set(ticketKeys)];
}
catch (error) {
throw new Error(`Failed to obtain ticket keys ${error.message}`);
}
};
module.exports = getTicketKeys;