-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
242 lines (210 loc) · 6.02 KB
/
index.ts
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
#!/usr/bin/env node
import { Octokit } from "octokit";
import { writeFileSync } from "fs";
import yargs from "yargs";
import * as config from "./config.json";
interface Label {
name: string;
color: string;
description: string | null;
}
interface Config {
desiredLabels: Label[];
}
type LabelRetriever = (owner: string, repo: string) => Promise<Label[]>;
type SyncPlanAction = (plan: SyncPlan) => Promise<any>;
class SyncPlan {
private _labelsToCreate: Label[] = [];
private _labelsToUpdate: Map<string, Label> = new Map<string, Label>();
constructor(readonly owner: string, readonly repository: string) {}
addLabelToCreate(label: Label) {
this._labelsToCreate.push(Object.assign({}, label));
}
addLabelToUpdate(existingLabel: Label, update: Label) {
this._labelsToUpdate.set(existingLabel.name, update);
}
get labelsToCreate() {
return this._labelsToCreate;
}
get labelsToUpdate() {
return this._labelsToUpdate;
}
}
interface LabelMatcher {
findMatches(labelToMatch: Label, labels: Label[]): Label[];
}
interface CandidateNamesGenerator {
generateCandidateNames(labelName: string): string[];
}
class IdentityCandidateNamesGenerator implements CandidateNamesGenerator {
generateCandidateNames(labelName: string): string[] {
return [labelName];
}
}
class StripPrefixCandidateNamesGenerator implements CandidateNamesGenerator {
generateCandidateNames(labelName: string): string[] {
const prefixSeparatorIndex = labelName.indexOf(":");
if (prefixSeparatorIndex !== -1) {
return [labelName, labelName.substring(prefixSeparatorIndex + 1)];
}
return [labelName];
}
}
class MappingsCandidateNamesGenerator implements CandidateNamesGenerator {
constructor(readonly mappings: Map<string, string[]>) {}
generateCandidateNames(labelName: string): string[] {
const labelMappings = this.mappings.get(labelName);
if (labelMappings?.length ?? 0 > 0) {
return [labelName, ...labelMappings!];
}
return [labelName];
}
}
class LabelNameMatcher implements LabelMatcher {
constructor(private generator: CandidateNamesGenerator) {}
findMatches(labelToMatch: Label, labels: Label[]): Label[] {
const candidateNames = this.generator.generateCandidateNames(
labelToMatch.name
);
return labels.filter(
(label) =>
candidateNames.find((value) => label.name === value) !== undefined
);
}
}
const options = yargs
.usage("Usage: [-t github_token --dry-run] GITHUB_REPO [GITHUB_REPOS...]")
.option("t", {
alias: "token",
describe: "Github token used for authentication",
type: "string",
demandOption: false,
})
.option("dry-run", {
describe:
"Dry-run will output a json file per repo describing the actions that would have been taken",
type: "boolean",
demandOption: false,
default: false,
})
.parseSync();
const octokit = new Octokit({
userAgent: `gh-label-sync/v0.0.1`,
auth: options.t,
});
let repoAction: SyncPlanAction = updateGHLabels;
if (options.dryRun) {
repoAction = (plan: SyncPlan) => {
dump(`${plan.owner}_${plan.repository}_plan.json`, plan);
return Promise.resolve();
};
}
processRepos(
<string[]>options._,
(owner, repo) => {
return getLabelsForRepo(octokit, owner, repo);
},
repoAction
)
.then(() => {
console.log("All good! Done!");
})
.catch((error) => {
console.error(error);
process.exit(-1);
});
function processRepos(
repos: string[],
retriever: LabelRetriever,
action: SyncPlanAction
): Promise<void[]> {
const repoPromises: Promise<void>[] = [];
repos.forEach((repo) => {
const splitPoint = repo.lastIndexOf("/");
const organization = repo.substring(0, splitPoint);
const repository = repo.substring(splitPoint + 1);
const repoPromise = retriever(organization, repository).then((labels) => {
const syncPlan = createSyncPlanForRepo(
organization,
repository,
config.desiredLabels,
labels
);
action(syncPlan);
});
repoPromises.push(repoPromise);
});
return Promise.all(repoPromises);
}
function dump(filename: string, value: any): void {
function replacer(_: string, value: Map<string, any> | any) {
if (value instanceof Map) {
return Object.fromEntries(value.entries());
} else {
return value;
}
}
writeFileSync(filename, JSON.stringify(value, replacer));
}
function getLabelsForRepo(
octokit: Octokit,
organization: string,
repository: string
): Promise<Label[]> {
return octokit.paginate(octokit.rest.issues.listLabelsForRepo, {
owner: organization,
repo: repository,
per_page: 100,
});
}
function createSyncPlanForRepo(
owner: string,
repo: string,
desiredLabels: Label[],
existingLabels: Label[]
): SyncPlan {
const sync = new SyncPlan(owner, repo);
const matcher = new LabelNameMatcher(
new MappingsCandidateNamesGenerator(
new Map(Object.entries(config.mappings))
)
);
desiredLabels.forEach((dl) => {
const el = matcher.findMatches(dl, existingLabels);
if (el.length > 0) {
sync.addLabelToUpdate(el[0], dl);
} else {
sync.addLabelToCreate(dl);
}
});
return sync;
}
function updateGHLabels(plan: SyncPlan) {
const updates: Promise<any>[] = [];
plan.labelsToUpdate.forEach((value: Label, key: string) => {
const { name, description, ...restOfUpdate } = value;
const promise = octokit.rest.issues.updateLabel({
owner: plan.owner,
repo: plan.repository,
name: key,
description: description ?? undefined,
new_name: name,
...restOfUpdate,
});
updates.push(promise);
});
return Promise.all(updates).then(() => {
const creations: Promise<any>[] = [];
plan.labelsToCreate.forEach((label) => {
const { description, ...restOfLabel } = label;
const promise = octokit.rest.issues.createLabel({
owner: plan.owner,
repo: plan.repository,
description: description ?? undefined,
...restOfLabel,
});
updates.push(promise);
});
return Promise.all(creations);
});
};