Skip to content

Commit

Permalink
Add group-by-label feature
Browse files Browse the repository at this point in the history
The idea of this feature is to limit which groups get selected for
review on a given PR. It adds a match tag to groups with the
effect that if the PR has labels, only groups whose matcher is
present in one of these labels get selected for reviews.

The use case for this is multiplatform or multiteam monorepos
to not get all the groups in an organization reviewing every single
PR.
  • Loading branch information
Gabriel Melo committed Aug 23, 2023
1 parent c291d74 commit 34ec189
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs'

interface Group {
name: string
matchLabel?: string
reviewers?: number
internal_reviewers?: number
usernames: string[]
Expand Down
36 changes: 26 additions & 10 deletions src/lottery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Pull {
user: {login: string} | null
number: number
draft?: boolean
labels: {name: string}[] | null
}
interface Env {
repository: string
Expand Down Expand Up @@ -79,17 +80,22 @@ class Lottery {
for (const {
reviewers,
internal_reviewers: internalReviewers,
usernames
usernames,
matchLabel
} of this.config.groups) {
const reviewersToRequest =
usernames.includes(author) && internalReviewers
? internalReviewers
: reviewers

if (reviewersToRequest) {
selected = selected.concat(
this.pickRandom(usernames, reviewersToRequest, selected.concat(author))
)
const labels = this.getPRLabels()
const shouldRunForGroup = matchLabel === undefined || labels.length === 0 || labels.includes(matchLabel)
if (shouldRunForGroup) {
const reviewersToRequest =
usernames.includes(author) && internalReviewers
? internalReviewers
: reviewers

if (reviewersToRequest) {
selected = selected.concat(
this.pickRandom(usernames, reviewersToRequest, selected.concat(author))
)
}
}
}
} catch (error: any) {
Expand Down Expand Up @@ -138,6 +144,16 @@ class Lottery {
return Number(this.pr?.number)
}

getPRLabels(): string[] {
const labels = this.pr?.labels?.map((label) => label.name)

if (labels) {
return labels
} else {
return []
}
}

async getPR(): Promise<Pull | undefined> {
if (this.pr) return this.pr

Expand Down

0 comments on commit 34ec189

Please sign in to comment.