-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.ts
109 lines (91 loc) · 4.12 KB
/
index.ts
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
const core = require('@actions/core');
const github = require('@actions/github');
const matchAll = require("match-all");
const Octokit = require("@octokit/rest");
async function extractJiraKeysFromCommit() {
try {
const regex = /((([A-Z]+)|([0-9]+))+-\d+)/g;
const isPullRequest = core.getInput('is-pull-request') == 'true';
// console.log("isPullRequest: " + isPullRequest);
const commitMessage = core.getInput('commit-message');
// console.log("commitMessage: " + commitMessage);
// console.log("core.getInput('parse-all-commits'): " + core.getInput('parse-all-commits'));
const parseAllCommits = core.getInput('parse-all-commits') == 'true';
// console.log("parseAllCommits: " + parseAllCommits);
const payload = github.context.payload;
const token = process.env['GITHUB_TOKEN'];
const octokit = new Octokit({
auth: token,
});
if (isPullRequest) {
let resultArr: any = [];
// console.log("is pull request...");
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
const prNum = payload.number;
const { data } = await octokit.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: prNum
});
data.forEach((item: any) => {
const commit = item.commit;
const matches: any = matchAll(commit.message, regex).toArray();
matches.forEach((match: any) => {
if (resultArr.find((element: any) => element == match)) {
// console.log(match + " is already included in result array");
} else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
});
const result = resultArr.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("not a pull request");
if (commitMessage) {
// console.log("commit-message input val provided...");
const matches = matchAll(commitMessage, regex).toArray();
const result = matches.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("no commit-message input val provided...");
const payload = github.context.payload;
if (parseAllCommits) {
// console.log("parse-all-commits input val is true");
let resultArr: any = [];
payload.commits.forEach((commit: any) => {
const matches = matchAll(commit.message, regex).toArray();
matches.forEach((match: any) => {
if (resultArr.find((element: any) => element == match)) {
// console.log(match + " is already included in result array");
} else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
});
const result = resultArr.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("parse-all-commits input val is false");
// console.log("head_commit: ", payload.head_commit);
const matches = matchAll(payload.head_commit.message, regex).toArray();
const result = matches.join(',');
core.setOutput("jira-keys", result);
}
}
}
} catch (error) {
core.setFailed(error.message);
}
}
(async function () {
await extractJiraKeysFromCommit();
// console.log("finished extracting jira keys from commit message");
})();
export default extractJiraKeysFromCommit