/
main.js
273 lines (273 loc) · 13.1 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const yaml = __importStar(require("js-yaml"));
const matcher_1 = __importDefault(require("matcher"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
const octokit = new github.GitHub(process.env['GITHUB_TOKEN'] || '');
let liveLabels;
let fileLabels;
let exclusions;
var LabelStatus;
(function (LabelStatus) {
LabelStatus[LabelStatus["Create"] = 0] = "Create";
LabelStatus[LabelStatus["Update"] = 1] = "Update";
LabelStatus[LabelStatus["Rename"] = 2] = "Rename";
LabelStatus[LabelStatus["Delete"] = 3] = "Delete";
LabelStatus[LabelStatus["Skip"] = 4] = "Skip";
LabelStatus[LabelStatus["Exclude"] = 5] = "Exclude";
LabelStatus[LabelStatus["Error"] = 6] = "Error";
})(LabelStatus || (LabelStatus = {}));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const yaml_file = path.join(core.getInput('yaml_file') || '.github/labels.yml');
const skip_delete = /true/i.test(core.getInput('skip_delete'));
const dry_run = /true/i.test(core.getInput('dry_run'));
if (!fs.existsSync(yaml_file)) {
core.setFailed(`Cannot find YAML file ${yaml_file}`);
return;
}
liveLabels = yield getLiveLabels();
fileLabels = yield getFileLabels(yaml_file);
yield displayLiveLabels();
core.info(`🏃 Running GitHub Labeler`);
let actionLabels = yield getActionLabels();
for (const actionLabel of actionLabels) {
switch (actionLabel.ghaction_status) {
case LabelStatus.Exclude: {
core.info(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
break;
}
case LabelStatus.Create: {
if (dry_run) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
try {
core.info(`${actionLabel.ghaction_log}`);
const params = Object.assign(Object.assign({}, github.context.repo), { name: actionLabel.name, color: actionLabel.color, description: actionLabel.description, mediaType: {
previews: ['symmetra']
} });
yield octokit.issues.createLabel(params);
}
catch (err) {
core.error(err.message);
}
break;
}
case LabelStatus.Update: {
if (dry_run) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
try {
core.info(`${actionLabel.ghaction_log}`);
const params = Object.assign(Object.assign({}, github.context.repo), { current_name: actionLabel.name, color: actionLabel.color, description: actionLabel.description, mediaType: {
previews: ['symmetra']
} });
yield octokit.issues.updateLabel(params);
}
catch (err) {
core.error(err.message);
}
break;
}
case LabelStatus.Rename: {
if (dry_run) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
try {
core.info(`${actionLabel.ghaction_log}`);
const params = Object.assign(Object.assign({}, github.context.repo), { current_name: `${actionLabel.from_name}`, name: actionLabel.name, color: actionLabel.color, description: actionLabel.description, mediaType: {
previews: ['symmetra']
} });
yield octokit.issues.updateLabel(params);
}
catch (err) {
core.error(err.message);
}
break;
}
case LabelStatus.Delete: {
if (skip_delete) {
core.info(`${dry_run ? '[dryrun] ' : ''}⛔️ Skipping delete for '${actionLabel.name}' (skip_delete on)`);
break;
}
if (dry_run) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
try {
core.info(`${actionLabel.ghaction_log}`);
const params = Object.assign(Object.assign({}, github.context.repo), { name: actionLabel.name });
yield octokit.issues.deleteLabel(params);
}
catch (err) {
core.error(err.message);
}
break;
}
case LabelStatus.Skip: {
core.info(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
break;
}
case LabelStatus.Error: {
core.error(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
break;
}
default: {
core.error(`${dry_run ? '[dryrun] ' : ''}🚫 '${actionLabel.name}' not processed`);
break;
}
}
}
}
catch (error) {
core.setFailed(error.message);
}
});
}
function getLiveLabels() {
return __awaiter(this, void 0, void 0, function* () {
const res = yield octokit.paginate(octokit.issues.listLabelsForRepo.endpoint.merge(Object.assign({}, github.context.repo)));
return res.map(label => {
return {
name: label.name,
color: label.color,
description: label.description || ''
};
});
});
}
function getFileLabels(yamlFile) {
return __awaiter(this, void 0, void 0, function* () {
return (yield yaml.load(fs.readFileSync(yamlFile, { encoding: 'utf-8' })));
});
}
function displayLiveLabels() {
return __awaiter(this, void 0, void 0, function* () {
let labels = Array();
for (const liveLabel of liveLabels) {
labels.push({
name: liveLabel.name,
color: liveLabel.color,
description: liveLabel.description
});
}
core.info(`👉 Current labels\n${yaml.safeDump(labels).toString()}`);
});
}
function getExclusions() {
return __awaiter(this, void 0, void 0, function* () {
const raw = core.getInput('exclude') || '[]';
let patterns;
if (raw.trimLeft().startsWith('[')) {
patterns = yield yaml.load(raw);
}
else {
patterns = [raw];
}
if (patterns === undefined || patterns.length === 0) {
return new Set();
}
return new Set(matcher_1.default(liveLabels.map(label => label.name), patterns));
});
}
function getActionLabels() {
return __awaiter(this, void 0, void 0, function* () {
let labels = Array();
exclusions = yield getExclusions();
for (const fileLabel of fileLabels) {
const liveLabel = yield getLiveLabel(fileLabel.name);
// Rename
if (fileLabel.from_name !== undefined) {
if ((yield getLiveLabel(fileLabel.name)) !== undefined) {
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Skip, ghaction_log: `⚠️ Skipping rename '${fileLabel.from_name}' label to '${fileLabel.name}'. Already exists` }));
continue;
}
const liveFromLabel = yield getLiveLabel(fileLabel.from_name);
if (liveFromLabel !== undefined) {
if (exclusions.has(liveFromLabel.name)) {
labels.push(Object.assign(Object.assign({}, liveFromLabel), { ghaction_status: LabelStatus.Exclude, ghaction_log: `🚫️ Excluding '${liveFromLabel.name}' from rename.` }));
continue;
}
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Rename, ghaction_log: `✨ Renaming '${liveFromLabel.name}' label to '${fileLabel.name}' and set color '${fileLabel.color}'${fileLabel.description !== undefined ? ` and desc '${fileLabel.description}'` : ''}` }));
continue;
}
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Error, ghaction_log: `❌ Label '${fileLabel.from_name}' not found. Cannot rename` }));
continue;
}
// Update
if (liveLabel !== undefined) {
if (exclusions.has(liveLabel.name)) {
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Exclude, ghaction_log: `🚫️ Excluding '${fileLabel.name}' from update.` }));
continue;
}
if (fileLabel.color == liveLabel.color && fileLabel.description == liveLabel.description) {
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Skip, ghaction_log: `⚠️ Skipping update for '${fileLabel.name}' label. Same color and description` }));
continue;
}
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Update, ghaction_log: `🔨 Updating '${fileLabel.name}' label with color '${fileLabel.color}'${fileLabel.description !== undefined ? ` and desc '${fileLabel.description}'` : ''}` }));
continue;
}
// Create
labels.push(Object.assign(Object.assign({}, fileLabel), { ghaction_status: LabelStatus.Create, ghaction_log: `🎨 Creating '${fileLabel.name}' label with color '${fileLabel.color}'${fileLabel.description !== undefined ? ` and desc '${fileLabel.description}'` : ''}` }));
}
// Delete
for (const liveLabel of liveLabels) {
if ((yield getFileLabel(liveLabel.name)) !== undefined) {
continue;
}
if (exclusions.has(liveLabel.name)) {
labels.push(Object.assign(Object.assign({}, liveLabel), { ghaction_status: LabelStatus.Exclude, ghaction_log: `🚫️ Excluding '${liveLabel.name}' from deletion.` }));
continue;
}
labels.push(Object.assign(Object.assign({}, liveLabel), { ghaction_status: LabelStatus.Delete, ghaction_log: `🔫 Deleting '${liveLabel.name}'` }));
}
return labels;
});
}
function getLiveLabel(name) {
return __awaiter(this, void 0, void 0, function* () {
for (const liveLabel of liveLabels) {
if (name == liveLabel.name) {
return liveLabel;
}
}
return undefined;
});
}
function getFileLabel(nameOrFrom) {
return __awaiter(this, void 0, void 0, function* () {
for (const fileLabel of fileLabels) {
if (nameOrFrom == fileLabel.name || nameOrFrom == fileLabel.from_name) {
return fileLabel;
}
}
return undefined;
});
}
run();