forked from Maximisch/issue-sync-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelSyncer.ts
59 lines (57 loc) · 2.63 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
import { GitHub } from './github'
import { Label } from './issue'
export class LabelSyncer {
public static syncLabels(gitHubSource: GitHub, gitHubTarget: GitHub): Promise<void> {
// Retrieve labels in source repo
let sourceRepoLabels: Label[] = []
return gitHubSource
.getLabels()
.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[] = []
gitHubTarget
.getLabels()
.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 gitHubTarget
.createLabel(element.name, element.description || '', 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((): null => {
console.log('Done')
return null
})
})
})
}
}