-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (77 loc) · 3.07 KB
/
index.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
const core = require("@actions/core");
const findRelevantCommits = require("./genealogist");
let baseDir, commitId, mainBranch;
(async () => {
try {
commitId = core.getInput("commitId");
mainBranch = core.getInput("mainBranch");
baseDir = core.getInput("baseDir");
const commitMessages = await findRelevantCommits(commitId, mainBranch, baseDir);
const jiraIssueKeys = getJiraIssueKeys(commitMessages).join(',');
core.info("Found the following JIRA issue keys: " + jiraIssueKeys);
core.setOutput('issueKeys', jiraIssueKeys);
} catch (error) {
core.setFailed(error.message);
}
})();
/**
* Default Regular Expression which will match standard Jira Issue Identifiers. These consist of
* the following three parts:
* <ol>
* <li>A Jira Project identifier, consisting of capital letters only</li>
* <li>A hyphen character</li>
* <li>A Jira Issue Number within the Project, which is a positive integer number</li>
* </ol>
* Example: <code>GLIX-4711</code>
* <p>
* Atlassian mentions the following standard pattern in their documentation:
*
* <pre>
* ((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)
* </pre>
*
* (Source: <a href=
* "https://confluence.atlassian.com/stashkb/integrating-with-custom-jira-issue-key-313460921.html">Atlassian</a>)
* <p>
* The Pattern uses a look-behind in order to avoid matching other identifiers which look similar,
* but aren't used for Jira. Examples would be Crucible Review Keys (such as
* <code>CR-PROJ-1234</code>) or Bamboo build plan IDs which look similar. (Source: <a href=
* "https://community.atlassian.com/t5/Bitbucket-questions/Regex-pattern-to-match-JIRA-issue-key/qaq-p/233319">Atlassian</a>)
* <p>
* We use a slightly modified version which has the following additions:
* <ol>
* <li>It employs a lookahead at the end to ensure that the Issue ID is properly terminated. If
* there are any characters after the Issue ID, they must be whitespace or certain punctuation
* characters.</li>
* <li>It requires the first digit to be non-zero. This has two effects:
* <ol>
* <li>It will not match the invalid issue key that has 0 as its only digit (e.g.
* <code>GLIX-0</code>)</li>
* <li>It will not accept leading zeroes (e.g. <code>GLIX-007</code> as opposed to
* <code>GLIX-7</code>)</li>
* </ol>
* </li>
* </ol>
*/
const defaultPattern = "((?<!([A-Z]{1,10})-?)[A-Z]+-[1-9]\\d*(?![^\\s:.,;!\"/|)}\\]?+&]))";
/**
* Uses the Regex to extract one or more Jira issue key(s) from the given commit messages.
*
* @param commitMessages
* @returns {*[]}
*/
function getJiraIssueKeys(commitMessages) {
const retVal = [];
for (let i = 0; i < commitMessages.length; i++) {
let commitMessage = commitMessages[i];
const re = new RegExp(defaultPattern, "g"); // re-create due to stateful use
while ((match = re.exec(commitMessage)) !== null) {
retVal.push(match[0]);
}
}
return retVal;
}
// just for testing
module.exports = {
getJiraIssueKeys: getJiraIssueKeys
};