-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (62 loc) · 2.13 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
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
const { getInput, setFailed, setOutput } = core;
try {
const githubToken = getInput('github-token', { required: true });
const pullNumber = getInput('pull-number', { required: false });
const base = getInput('base', { required: false });
const head = getInput('head', { required: false });
const { context, getOctokit } = github;
const { repo: { owner, repo } } = context;
const { rest } = getOctokit(githubToken);
const jiraMatcher = /\d+-[A-Z]+(?!-?[a-zA-Z]{1,10})/g;
if (!pullNumber && !(base && head)) {
setFailed('You mast set pull-number or base and head branches');
}
let issues = [];
if (pullNumber) {
const { data} = await rest.pulls.listCommits({owner, repo, pull_number: pullNumber});
issues = Array.isArray(data)
? data.reduce((issues, {commit}) => {
const names = commit.message.split('').reverse().join('').match(jiraMatcher);
if (!names) {
return issues;
}
names.forEach((res) => {
const id = res.split('').reverse().join('');
if (issues.indexOf(id) === -1) {
issues.push(id);
}
});
return issues;
},
[],
)
: [];
} else {
const { data } = await rest.repos.compareCommitsWithBasehead({owner, repo, basehead: `${base}...${head}`});
issues = Array.isArray(data.commits)
? data.commits.reduce((issues, { commit}) => {
const names = commit.message.split('').reverse().join('').match(jiraMatcher);
if (!names) {
return issues;
}
names.forEach((res) => {
const id = res.split('').reverse().join('');
if (issues.indexOf(id) === -1) {
issues.push(id);
}
});
return issues;
},
[],
)
: [];
}
setOutput('issues', JSON.stringify(issues));
} catch (err) {
setFailed(err.message);
}
}
run();