forked from andymckay/labeler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabel.js
141 lines (128 loc) · 4.62 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const github = require("@actions/github");
const core = require("@actions/core");
const util = require("util");
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.getOctokit(myToken);
const context = github.context;
const repoName = context.payload.repository.name;
const ownerName = context.payload.repository.owner.login;
const issueNumber = getIssueNumber(core, context);
if (issueNumber === undefined) {
return "No action being taken. Ignoring because issueNumber was not identified";
} else {
labelsToAdd = labelsToAdd.filter(value => ![""].includes(value));
labelsToRemove = labelsToRemove.filter(value => ![""].includes(value));
if (ignoreIfAssigned || ignoreIfLabeled || labelsToRemove.length !== 0) {
// 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
try {
var updatedIssueInformation = await octokit.rest.issues.get({
owner: ownerName,
repo: repoName,
issue_number: issueNumber
});
} catch (err) {
throw new Error(
`Can not get information for issue number ${issueNumber} in ${ownerName}/${repoName} repository. Cause: ${err}`,
{ cause: err }
);
}
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));
try {
await octokit.rest.issues.update({
owner: ownerName,
repo: repoName,
issue_number: issueNumber,
labels: labels
});
} catch (err) {
throw new Error(
`Can not update issue number ${issueNumber} in ${ownerName}/${repoName} repository. Cause: ${err}`,
{ cause: err }
);
}
return `Updated labels in ${issueNumber}. Added: ${labelsToAdd}. Removed: ${labelsToRemove}.`;
} else {
// The action is trying to add new labels. If an empty array is passed as parameter, all labels will be deleted.
// https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#add-labels-to-an-issue
try {
await octokit.rest.issues.addLabels({
owner: ownerName,
repo: repoName,
issue_number: issueNumber,
labels: labelsToAdd
});
} catch (err) {
throw new Error(
`Can not add labels to the issue number ${issueNumber} in ${ownerName}/${repoName} repository. Cause: ${err}`,
{ cause: err }
);
}
return `Updated labels in ${issueNumber}. Added: ${labelsToAdd}.`;
}
}
}
label()
.then(
result => {
core.info(result);
},
err => {
core.debug(util.inspect(err));
core.setFailed(err);
}
)
.catch(err => {
core.debug(util.inspect(err));
core.setFailed(err);
});