forked from WalshyDev/pr-labels
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (56 loc) · 1.75 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
const core = require('@actions/core'),
github = require('@actions/github');
async function run() {
try {
const token = core.getInput('token');
const labels = Object.keys(process.env).filter(e => e !== 'INPUT_TOKEN' && e.startsWith('INPUT_')).map(e => e.replace('INPUT_', '').toLowerCase());
console.log(`Checking for labels: ` + labels.join(', '));
const prNumber = getPrNumber();
if (!prNumber) {
console.log('Could not get pull request number from context, exiting');
return;
}
const client = github.getOctokit(token);
const repo = github.context.payload.repository;
const data = await client.pulls.get({
owner: repo.owner.login,
repo: repo.name,
pull_number: prNumber,
});
const branch = data.data.head.ref;
let labelApplied = null;
for (let prefix of labels) {
let tmp = prefix;
if (!prefix.endsWith('/'))
prefix += '/';
console.log(`If ${branch} startsWith ${prefix}`)
if (branch.startsWith(prefix)) {
const label = process.env['INPUT_' + tmp.toUpperCase()];
console.log(`Adding ${label} to #${prNumber}`);
await addLabels(client, prNumber, [label]);
labelApplied = label;
break;
}
}
core.setOutput('applied', labelApplied);
} catch (error) {
console.error(error);
core.setFailed(error.message);
}
}
async function addLabels(client, prNumber, labels) {
await client.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});
}
function getPrNumber() {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}
return pullRequest.number;
}
run();