-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsync-labels.ts
More file actions
115 lines (98 loc) · 2.79 KB
/
sync-labels.ts
File metadata and controls
115 lines (98 loc) · 2.79 KB
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
import 'reflect-metadata'
import {Octokit} from '@octokit/rest'
import {GitHub} from '../github.js'
import env from '../env.js'
import * as core from '@actions/core'
import type {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
type Endpoints = InstanceType<typeof Octokit>
type Labels = GetResponseDataTypeFromEndpointMethod<
Endpoints['issues']['getLabel']
>[]
async function getLabels(repo: string): Promise<Labels> {
// initialize GitHub client
const github = await GitHub.getGitHub()
// use the GitHub client to fetch the list of labels from js-libp2p
const labels = await github.client.paginate(
github.client.issues.listLabelsForRepo,
{
owner: env.GITHUB_ORG,
repo
}
)
return labels
}
async function addLabel(
repo: string,
name: string,
color: string,
description: string | undefined
): Promise<void> {
// initialize GitHub client
const github = await GitHub.getGitHub()
await github.client.issues.createLabel({
owner: env.GITHUB_ORG,
repo,
name,
color,
description
})
}
async function removeLabel(repo: string, name: string): Promise<void> {
// initialize GitHub client
const github = await GitHub.getGitHub()
await github.client.issues.deleteLabel({
owner: env.GITHUB_ORG,
repo,
name
})
}
async function sync(): Promise<void> {
const sourceRepo = process.env.SOURCE_REPOSITORY
const targetRepos = process.env.TARGET_REPOSITORIES?.split(',')?.map(r =>
r.trim()
)
const addLabels = process.env.ADD_LABELS === 'true'
const removeLabels = process.env.REMOVE_LABELS === 'true'
if (!sourceRepo) {
throw new Error('SOURCE_REPOSITORY environment variable not set')
}
if (!targetRepos) {
throw new Error('TARGET_REPOSITORIES environment variable not set')
}
const sourceLabels = await getLabels(sourceRepo)
core.info(
`Found the following labels in ${sourceRepo}: ${sourceLabels
.map(l => l.name)
.join(', ')}`
)
for (const repo of targetRepos) {
const targetLabels = await getLabels(repo)
core.info(
`Found the following labels in ${repo}: ${targetLabels
.map(l => l.name)
.join(', ')}`
)
if (removeLabels) {
for (const label of targetLabels) {
if (!sourceLabels.find(l => l.name === label.name)) {
core.info(`Removing ${label.name} label from ${repo} repository`)
await removeLabel(repo, label.name)
}
}
}
if (addLabels) {
for (const label of sourceLabels) {
if (!targetLabels.some(l => l.name === label.name)) {
core.info(`Adding ${label.name} label to ${repo} repository`)
await addLabel(
repo,
label.name,
label.color,
label.description || undefined
)
}
}
}
}
}
sync()