-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
96 lines (96 loc) · 4.19 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 = [];
// 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) => {
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(',');
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 = [];
payload.commits.forEach((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(',');
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");
})();
exports.default = extractJiraKeysFromCommit;