This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
label.js
112 lines (93 loc) · 3.23 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const github = require("@actions/github");
const core = require("@actions/core");
var labelsToAdd = core
.getInput("add-labels")
.split(",")
.map(x => x.trim());
var labelsToRemove = core
.getInput("remove-labels")
.split(",")
.map(x => x.trim());
/**
* Obtain the issue number either from input or from the context
* @param core - the core object
* @param context - the context object
* @returns {*|number} - issue/card/pr number if not provided by user.
*/
function getIssueNumber(core, context) {
let issueNumber = core.getInput("issue-number");
// return what is provided
if (issueNumber) return issueNumber;
// return the one found in issue
issueNumber = context.payload.issue && context.payload.issue.number;
if (issueNumber) return issueNumber;
// return the one found in PR
issueNumber =
context.payload.pull_request && context.payload.pull_request.number;
if (issueNumber) return issueNumber;
let card_url =
context.payload.project_card && context.payload.project_card.content_url;
issueNumber = card_url && card_url.split("/").pop();
return issueNumber;
}
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;
let issueNumber = getIssueNumber(core, context);
if (issueNumber === undefined) {
return "No action being taken. Ignoring because issueNumber was not identified";
}
labelsToAdd = labelsToAdd.filter(value => ![""].includes(value));
labelsToRemove = labelsToRemove.filter(value => ![""].includes(value));
// 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 => !labelsToRemove.includes(value));
await octokit.issues.update({
owner: ownerName,
repo: repoName,
issue_number: issueNumber,
labels: labels
});
return `Updated labels in ${issueNumber}. 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();
});