-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
126 lines (103 loc) · 3.55 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
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
const fs = require("fs");
const path = require("path");
const github = require("@actions/github");
const core = require("@actions/core");
const accessToken = process.env.GITHUB_TOKEN;
const octokit = github.getOctokit(accessToken);
async function run() {
let newLabelsUrl = path.join(
process.env["GITHUB_WORKSPACE"],
".github",
"labels.json"
);
if (!core.getBooleanInput("delete")) {
console.log("[Action] Will not delete any existing labels");
}
let liveLabels = await getCurrentLabels();
let newLabels = JSON.parse(fs.readFileSync(newLabelsUrl).toString());
// If the color of a label has a # sign, remove it
newLabels.forEach((newLabel) => {
if (newLabel.color[0] === "#") {
newLabel.color = newLabel.color.slice(1);
}
});
let labelModList = diffLabels(liveLabels, newLabels);
labelModList.forEach(async (mod) => {
if (mod.type === "create") {
let params = {
...github.context.repo,
name: mod.label.name,
color: mod.label.color,
description: mod.label.description,
};
console.log(`[Action] Creating Label: ${mod.label.name}`);
await octokit.rest.issues.createLabel(params);
} else if (mod.type === "update") {
let params = {
...github.context.repo,
current_name: mod.label.name,
color: mod.label.color,
description: mod.label.description,
};
console.log(`[Action] Updating Label: ${mod.label.name}`);
await octokit.rest.issues.updateLabel(params);
} else if (mod.type === "delete") {
if (core.getBooleanInput("delete")) {
let params = {
...github.context.repo,
name: mod.label.name,
};
console.log(`[Action] Deleting Label: ${mod.label.name}`);
await octokit.rest.issues.deleteLabel(params);
}
}
});
}
async function getCurrentLabels() {
let response = await octokit.rest.issues.listLabelsForRepo({
...github.context.repo,
});
let data = response.data;
return data;
}
function diffLabels(oldLabels, newLabels) {
// Return diff which includes
// 1) New labels to be created
// 2) Labels that exist but have an update
// 3) Labels that no longer exist and should be deleted
// each entry has two values
// { type: 'create' | 'update' | 'delete', label }
let oldLabelsNames = oldLabels.map((label) => label.name.toLowerCase());
let newLabelsNames = newLabels.map((label) => label.name.toLowerCase());
let labelModList = [];
oldLabelsNames.forEach((oLabel) => {
// when using `includes` with strings, the match is case-sensitive
// so we first lowercase both strings when comparing
if (newLabelsNames.includes(oLabel)) {
const oldLabel = oldLabels.filter((l) => l.name === oLabel)[0];
const newLabel = newLabels.filter((l) => l.name === oLabel)[0];
if (
oldLabel.color !== newLabel.color ||
(typeof newLabel.description !== "undefined" &&
oldLabel.description !== newLabel.description)
) {
// UPDATE
labelModList.push({ type: "update", label: newLabel });
}
newLabelsNames = newLabelsNames.filter((element) => {
return element !== oLabel;
});
} else {
// DELETE
const oldLabel = oldLabels.filter((l) => l.name === oLabel)[0];
labelModList.push({ type: "delete", label: oldLabel });
}
});
newLabelsNames.forEach((nLabel) => {
const newLabel = newLabels.filter((l) => l.name === nLabel)[0];
// CREATE
labelModList.push({ type: "create", label: newLabel });
});
return labelModList;
}
run();