This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabel.js
68 lines (58 loc) · 1.93 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
require('dotenv').config({ silent: true });
const GitHubApi = require('@octokit/rest');
const Config = require('./src/Config');
const StudentMapping = require('./src/StudentMapping');
const config = new Config();
const modules = config.modules;
// TODO: How to make it less manual?
const warningLabels = {
FormatCheckRequested: 'ba3940',
GithubUserNameRequested: 'd73a4a'
};
const github = new GitHubApi({
auth: process.env.GITHUB_TOKEN
});
Object.values(modules).forEach(module => {
const { moduleConfig, studentMappingPath } = module;
const { repositories, semesterAccount } = moduleConfig;
const phaseMapping = new StudentMapping(studentMappingPath);
let uniqueLabels = {};
Object.keys(warningLabels).forEach(label => {
uniqueLabels[label] = warningLabels[label];
});
// eslint-disable-next-line
[].concat.apply([], Object.values(phaseMapping.data).map(value => value.labels)).forEach(label => {
uniqueLabels[label] = 'ededed';
});
repositories.forEach(repo => {
let repoPromises = [];
Object.keys(uniqueLabels).forEach(name => {
const color = uniqueLabels[name];
repoPromises.push(new Promise((resolve, reject) => {
github.issues.createLabel({
owner: semesterAccount,
repo: repo,
name: name,
color: color
}).then(() => {
resolve();
}, err => {
if (!err) {
resolve();
return;
}
// Checks whether the error is because the label already exists.
const errObj = JSON.parse(err.message);
if (errObj.errors && errObj.errors[0] && errObj.errors[0].code === 'already_exists') {
resolve();
return;
}
reject(err);
});
}));
});
Promise.all(repoPromises)
.then(() => `Added ${Object.keys(uniqueLabels).length} labels to ${semesterAccount}/${repo}`)
.catch(err => console.log(err));
});
});