Skip to content

Commit a63a68d

Browse files
author
Thomas Truong
committed
feat(reviewersInTeams): using teams to add reviews
* Using GitHub teams you can specify reviewers to be added to Pull Request * GitHub team's slug name is used under the label reviewersInTeams to extract the members in the team Fixes kentaro-m#123
1 parent 4ed0719 commit a63a68d

File tree

3 files changed

+353
-21
lines changed

3 files changed

+353
-21
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,40 @@ useAssigneeGroups: false
142142
# - wip
143143
```
144144

145+
#### Add Members in Github Team(s) to Reviewers List
146+
147+
Randomly add members in Github team(s) to the pull request based on number of reviewers listed using the `org/team_slug` syntax.
148+
149+
```yaml
150+
# Set to true to add reviewers to pull requests
151+
addReviewers: true
152+
153+
# Set to true to add assignees to pull requests
154+
addAssignees: false
155+
156+
# A list of team reviewers to be added to pull requests (GitHub team slug)
157+
reviewersInTeams:
158+
- org/teamReviewerA
159+
- org/teamReviewerB
160+
161+
# Number of reviewers has no impact on Github teams
162+
# Set 0 to add all the reviewers (default: 0)
163+
numberOfReviewers: 2
164+
165+
# A list of assignees, overrides reviewers if set
166+
# assignees:
167+
# - assigneeA
168+
169+
# A number of assignees to add to the pull request
170+
# Set to 0 to add all of the assignees.
171+
# Uses numberOfReviewers if unset.
172+
# numberOfAssignees: 2
173+
174+
# A list of keywords to be skipped the process that add reviewers if pull requests include it
175+
# skipKeywords:
176+
# - wip
177+
```
178+
145179
### Assign Author as Assignee
146180
Add the PR creator as assignee to the pull request.
147181

src/handler.ts

+75-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,61 @@ export interface Config {
1313
useAssigneeGroups: boolean
1414
reviewGroups: { [key: string]: string[] }
1515
assigneeGroups: { [key: string]: string[] }
16+
reviewersInTeams: string[]
17+
}
18+
19+
export async function getTeamMembers(
20+
context: Context,
21+
{ org, teamSlug }: { org: string; teamSlug: string }
22+
): Promise<string[]> {
23+
let members: { login: string }[] = []
24+
25+
try {
26+
members = (
27+
await context.github.teams.listMembersInOrg({
28+
org,
29+
// eslint-disable-next-line @typescript-eslint/camelcase
30+
team_slug: teamSlug
31+
})
32+
).data
33+
} catch (error) {
34+
context.log(error)
35+
}
36+
37+
if (members.length === 0) {
38+
let team
39+
40+
try {
41+
team = (
42+
await context.github.teams.getByName({
43+
org,
44+
// eslint-disable-next-line @typescript-eslint/camelcase
45+
team_slug: teamSlug
46+
})
47+
).data
48+
} catch (error) {
49+
context.log(error)
50+
throw new Error(
51+
`Cannot fetch team id for team ${teamSlug} under organisation ${org}.`
52+
)
53+
}
54+
55+
try {
56+
members = (
57+
await context.github.teams.listMembersLegacy({
58+
// eslint-disable-next-line @typescript-eslint/camelcase
59+
team_id: team.id
60+
})
61+
).data
62+
} catch (error) {
63+
context.log(error)
64+
throw new Error(
65+
`Cannot fetch list of members in team ${teamSlug} under organisation ${org}.`
66+
)
67+
}
68+
}
69+
70+
return members.map((member): string => member.login)
1671
}
1772

1873
export async function handlePullRequest(context: Context): Promise<void> {
@@ -30,7 +85,8 @@ export async function handlePullRequest(context: Context): Promise<void> {
3085
useAssigneeGroups,
3186
assigneeGroups,
3287
addReviewers,
33-
addAssignees
88+
addAssignees,
89+
reviewersInTeams
3490
} = config
3591

3692
if (skipKeywords && includesSkipKeywords(title, skipKeywords)) {
@@ -54,6 +110,24 @@ export async function handlePullRequest(context: Context): Promise<void> {
54110
)
55111
}
56112

113+
if (reviewersInTeams && reviewersInTeams.length > 0) {
114+
for (const team of reviewersInTeams) {
115+
const result = /^([\w\-]+)\/([\w\-]+)$/.exec(team)
116+
if (result === null) {
117+
throw new Error(
118+
'Error in configuration file to expand a team reviewersInTeams must be a list of org/team_slug.'
119+
)
120+
}
121+
const reviewers = await getTeamMembers(context, {
122+
org: result[1],
123+
teamSlug: result[2]
124+
})
125+
config.reviewers = Array.from(
126+
new Set((config.reviewers || []).concat(reviewers))
127+
)
128+
}
129+
}
130+
57131
const owner = context.payload.pull_request.user.login
58132

59133
if (addReviewers) {

0 commit comments

Comments
 (0)