-
Notifications
You must be signed in to change notification settings - Fork 3
/
labelSyncer.ts
64 lines (61 loc) · 2.75 KB
/
labelSyncer.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
import { Octokit } from "octokit";
export class Label {
id: number;
node_id: string;
url: string;
name: string;
description: string | null;
color: string;
default: boolean;
}
export class LabelSyncer {
public static syncLabels(octokit_source: Octokit, octokit_target: Octokit, owner_source: string, repo_source: string, owner_target:string, repo_target:string):Promise<void> {
// Retrieve labels in source repo
let sourceRepoLabels: Label[] = [];
return octokit_source.request('GET /repos/{owner}/{repo}/labels', {
owner: owner_source,
repo: repo_source,
}).then((response) => {
sourceRepoLabels = response.data;
}).catch((err) => {
console.error("Failed to retrieve source repo labels", err);
}).then(() => {
// Retrieve labels in target repo
let targetRepoLabels: Label[] = [];
octokit_target.request('GET /repos/{owner}/{repo}/labels', {
owner: owner_target,
repo: repo_target,
}).then((response) => {
targetRepoLabels = response.data;
}).catch((err) => {
console.error("Failed to retrieve target repo labels", err);
}).then(() => {
// Filter source repo labels: remove all that from list that are already contained in target (= delta)
sourceRepoLabels = sourceRepoLabels
.filter((label) => targetRepoLabels
// Match by name and description, as IDs may vary across repos
.find(targetEntry => targetEntry.name == label.name && targetEntry.description == label.description)
== undefined
);
// Create delta of missing issues in target
Promise.all(
sourceRepoLabels.map(element => {
return octokit_target.request('POST /repos/{owner}/{repo}/labels', {
owner: owner_target,
repo: repo_target,
name: element.name,
description: element.description ? element.description : "",
color: element.color
}).then(() => "Successfully synced label " + element.name)
.catch((err) => "Failed to sync label " + element.name + ": " + err);
})
).then((results) => {
results.forEach((element) => console.log(element));
}).then(() => {
console.log("Done");
return null;
});
});
});
}
}