forked from JulienKode/team-labeler-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.ts
277 lines (248 loc) · 7.16 KB
/
github.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as github from '@actions/github'
import * as yaml from 'js-yaml'
import * as core from '@actions/core'
import {ExternalRepo} from './types'
import {filterAtSign, getTeamMembers} from './teams'
type GitHub = ReturnType<typeof github.getOctokit>
export function getPrNumber(): number | undefined {
const pullRequest = github.context.payload.pull_request
if (!pullRequest) {
const issue = github.context.payload.issue
if (!issue) {
return undefined
}
return issue.number
}
return pullRequest.number
}
export function getPrAuthor(): string | undefined {
const pullRequest = github.context.payload.pull_request
if (!pullRequest) {
const issue = github.context.payload.issue
if (!issue) {
return undefined
}
return issue.user.login
}
return pullRequest.user.login
}
export function getAllReviewers(): string[] | undefined {
const pullRequest = github.context.payload.pull_request
if (pullRequest == null) {
return undefined
}
const reviewers =
pullRequest.requested_reviewers?.map(rr => `${rr.login}`) ?? []
const teamReviewers =
pullRequest.requested_teams?.map(rr => `${rr.name}`) ?? []
const allReviewers: string[] = [...teamReviewers, ...reviewers]
return allReviewers
}
export async function listReviews(
client: GitHub,
externalRepo: ExternalRepo | undefined
) {
const pullRequest = github.context.payload.pull_request
if (pullRequest == null) {
throw new Error('No pull request in context')
}
const pullRequestNumber = pullRequest.number
let repo = github.context.repo.repo
let ref = github.context.sha
if (externalRepo?.repo) {
repo = externalRepo?.repo
ref = externalRepo?.ref
}
core.info(
`listReviews: Using repo "${repo}" & ref "${ref}" & owner ${github.context.repo.owner}`
)
const reviews = await client.rest.pulls.listReviews({
pull_number: pullRequestNumber,
owner: github.context.repo.owner,
repo,
ref,
per_page: 9999999
})
const allReviews = reviews.data
.filter(review => review.state === 'APPROVED')
.map(review => review.user?.login ?? '')
.filter(v => v != '')
return allReviews
}
export async function getLabelsConfiguration(
client: GitHub,
configurationPath: string,
externalRepo: ExternalRepo | undefined
): Promise<Map<string, LabelConfig>> {
core.info(`getLabelsConfiguration`)
const configurationContent: string = await fetchContent(
client,
configurationPath,
externalRepo
)
const configObject: any = yaml.load(configurationContent)
return getLabelGlobMapFromObject(configObject)
}
async function fetchContent(
client: GitHub,
path: string,
externalRepo: ExternalRepo | undefined
): Promise<string> {
let repo = github.context.repo.repo
let ref = github.context.sha
if (externalRepo?.repo) {
repo = externalRepo?.repo
ref = externalRepo?.ref
}
core.info(
`fetchContent: Using repo "${repo}" & ref "${ref}" & path "${path}" & owner ${github.context.repo.owner}`
)
const response = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo,
path,
ref
})
// @ts-ignore
if (!Array.isArray(response.data) && response.data.content)
// @ts-ignore
return Buffer.from(response.data.content, 'base64').toString()
throw new Error('Invalid yaml file')
}
export type LabelConfig =
| {
assigned?: {
teams?: string[]
users?: string[]
}
approved?: {
teams?: string[]
users?: string[]
}
}
| string[]
const escapeDetailedLabelConfig = (labelConfig: LabelConfig) =>
labelConfig instanceof Array
? {}
: {
...(labelConfig?.assigned
? {
assigned: {
teams: labelConfig.assigned.teams?.map(entry =>
filterAtSign(entry)
),
users: labelConfig.assigned.users?.map(entry =>
filterAtSign(entry)
)
}
}
: {}),
...(labelConfig?.approved
? {
approved: {
teams: labelConfig.approved.teams?.map(entry =>
filterAtSign(entry)
),
users: labelConfig.approved.users?.map(entry =>
filterAtSign(entry)
)
}
}
: {})
}
export function getLabelGlobMapFromObject(
configObject: any
): Map<string, LabelConfig> {
const labelGlobs: Map<string, LabelConfig> = new Map()
for (const label in configObject) {
if (typeof configObject[label] === 'string') {
const escapedEntry = filterAtSign(configObject[label])
labelGlobs.set(label, [escapedEntry])
} else if (configObject[label] instanceof Array) {
const escapedEntry = configObject[label].map(entry => filterAtSign(entry))
labelGlobs.set(label, escapedEntry)
} else if (
'assigned' in configObject[label] ||
'approved' in configObject[label]
) {
const escapedEntry = escapeDetailedLabelConfig(configObject[label])
labelGlobs.set(label, escapedEntry)
} else {
throw Error(
`found unexpected type for label ${label} (should be string or array of globs)`
)
}
}
return labelGlobs
}
export async function mapLabelConfigTeamsToUsers(
client: GitHub,
labelConfigs: Map<string, LabelConfig>
): Promise<Map<string, string[]>> {
const allTeams: string[] = []
for (const [, labelReviewers] of labelConfigs.entries()) {
if ('assigned' in labelReviewers || 'approved' in labelReviewers) {
const currentLabelTeams = [
...new Set([
...(labelReviewers?.assigned?.teams ?? []),
...(labelReviewers?.approved?.teams ?? [])
])
]
allTeams.push(...currentLabelTeams)
} else if (labelReviewers instanceof Array) {
allTeams.push(...labelReviewers)
} else if (typeof labelReviewers === 'string') {
allTeams.push(labelReviewers)
}
}
const dedupedTeams = [...new Set(allTeams)]
const arr = await Promise.all(
dedupedTeams
.map(team => filterAtSign(team))
.map(async team => {
const members = await getTeamMembers(client, team)
return {
team,
members
}
})
)
return new Map(arr.map(obj => [obj.team, obj.members]))
}
export function createClient(token: string): GitHub {
return github.getOctokit(token)
}
export async function addPrLabels(
client: GitHub,
prNumber: number,
labels: string[]
) {
const result = await client.rest.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels
})
return result
}
export async function getPrLabels(client: GitHub, prNumber: number) {
const result = await client.rest.issues.listLabelsOnIssue({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber
})
return result
}
export async function removePrLabel(
client: GitHub,
prNumber: number,
label: string
) {
const result = await client.rest.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
name: label
})
return result
}