-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (50 loc) · 1.58 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
import * as core from '@actions/core';
import * as github from '@actions/github';
import {Octokit} from '@octokit/rest';
import _ from 'lodash';
import {debug} from "@actions/core";
async function getCommits(payload, token) {
if (!payload.commits) {
return [];
}
if (payload.commits.length < 20) {
return payload.commits;
}
const {data} = await new Octokit({auth: token}).repos.compareCommits({
owner: payload.repository.owner.login,
repo: payload.repository?.name,
base: payload.before,
head: payload.after,
});
return data.commits.map(commit=>commit.commit);
}
function parseCommits(commits) {
if (!commits || commits.length === 0) return '';
const jiraIssueKeyRegex = /([A-Za-z]{2,10}-[0-9]{1,5})/g;
return _.uniq(commits.map(commit => {
const message = commit.message;
const matches = message.match(jiraIssueKeyRegex);
if (matches) {
return matches;
}
}).flat().filter(match => match).map(match => match.toUpperCase())).join(',');
}
async function run() {
debug('Starting action');
const token = process.env.GITHUB_TOKEN;
const {payload} = github.context;
if (!payload.repository) return [];
const owner = payload.repository.owner.login;
const repo = payload.repository?.name;
const commits = await getCommits(payload, token);
debug(`owner: ${owner} repo: ${repo}`);
debug(JSON.stringify(payload));
debug(`commit length : ${commits.length}`);
const issueKeys = parseCommits(commits);
core.setOutput('issueKeys', issueKeys);
}
(async () => {
core.setCommandEcho(true);
await run();
})()
export default run;