-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
124 lines (107 loc) · 5.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
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
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]+-\d+)/g;
// console.log("core.getInput('is-pull-request'): " + core.getInput('is-pull-request'));
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 jsonPayload = JSON.stringify(github.context.payload, undefined, 2);
// console.log("github context json payload: ", jsonPayload);
const payload = github.context.payload;
console.log("github: ", github);
console.log("GITHUB_RUN_ID: ", process.env['GITHUB_RUN_ID']);
const token = process.env['GITHUB_TOKEN'];
console.log("github token: " + token);
const octokit = new Octokit({
auth: token,
});
if(isPullRequest) {
let resultArr = [];
console.log("is pull request...");
// console.log("payload.repository.owner.login: " + payload.repository.owner.login);
// console.log("payload.repository.name: " + payload.repository.name);
// console.log("payload.number: " + payload.number);
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
});
// console.log("commits: ", data);
data.forEach(item => {
// console.log("commit: ", item.commit.message);
const commit = item.commit;
const matches = matchAll(commit.message, regex).toArray();
matches.forEach(match => {
if(resultArr.find(element => 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(',');
console.log("result: ", result);
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(',');
console.log("result: ", result);
core.setOutput("jira-keys", result);
}
else {
console.log("no commit-message input val provided...");
const jsonPayload = JSON.stringify(github.context.payload, undefined, 2);
// console.log("github context json payload: ", jsonPayload);
const payload = github.context.payload;
if(parseAllCommits) {
console.log("parse-all-commits input val is true");
let resultArr = [];
payload.commits.forEach(commit => {
// console.log("commit: ", commit);
const matches = matchAll(commit.message, regex).toArray();
matches.forEach(match => {
if(resultArr.find(element => 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(',');
console.log("result: ", result);
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");
})();