forked from andymckay/labeler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
label.js
77 lines (68 loc) · 2.16 KB
/
label.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
const github = require("@actions/github");
const core = require("@actions/core");
const labelsToAdd = core
.getInput("add-labels")
.split(",")
.map(x => x.trim());
const labelsToRemove = core
.getInput("remove-labels")
.split(",")
.map(x => x.trim());
async function label() {
const myToken = core.getInput("repo-token");
const ignoreIfAssigned = core.getInput("ignore-if-assigned");
const ignoreIfLabeled = core.getInput("ignore-if-labeled");
const octokit = new github.GitHub(myToken);
const context = github.context;
const repoName = context.payload.repository.name;
const ownerName = context.payload.repository.owner.login;
const issueNumber = context.payload.issue.number;
// query for the most recent information about the issue. Between the issue being created and
// the action running, labels or asignees could have been added
var updatedIssueInformation = await octokit.issues.get({
owner: ownerName,
repo: repoName,
issue_number: issueNumber
});
if (ignoreIfAssigned) {
// check if the issue has been assigned to anyone
if (updatedIssueInformation.data.assignees.length !== 0) {
return "No action being taken. Ignoring because one or more assignees have been added to the issue";
}
}
let labels = updatedIssueInformation.data.labels.map(label => label.name);
if (ignoreIfLabeled) {
if (labels.length !== 0) {
return "No action being taken. Ignoring because one or labels have been added to the issue";
}
}
for (let labelToAdd of labelsToAdd) {
if (!labels.includes(labelToAdd)) {
labels.push(labelToAdd);
}
}
labels = labels.filter(value => {
return !labelsToRemove.includes(value);
});
await octokit.issues.update({
owner: ownerName,
repo: repoName,
issue_number: issueNumber,
labels: labels
});
return `Updated labels in ${context.payload.issue.number}. Added: ${labelsToAdd}. Removed: ${labelsToRemove}.`;
}
label()
.then(
result => {
// eslint-disable-next-line no-console
console.log(result);
},
err => {
// eslint-disable-next-line no-console
console.log(err);
}
)
.then(() => {
process.exit();
});