Skip to content

Commit

Permalink
feat(reviewersInTeams): using teams to add reviews
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Thomas Truong committed Apr 14, 2020
1 parent 4ed0719 commit a63a68d
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 21 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,40 @@ useAssigneeGroups: false
# - wip
```

#### Add Members in Github Team(s) to Reviewers List

Randomly add members in Github team(s) to the pull request based on number of reviewers listed using the `org/team_slug` syntax.

```yaml
# Set to true to add reviewers to pull requests
addReviewers: true

# Set to true to add assignees to pull requests
addAssignees: false

# A list of team reviewers to be added to pull requests (GitHub team slug)
reviewersInTeams:
- org/teamReviewerA
- org/teamReviewerB

# Number of reviewers has no impact on Github teams
# Set 0 to add all the reviewers (default: 0)
numberOfReviewers: 2

# A list of assignees, overrides reviewers if set
# assignees:
# - assigneeA

# A number of assignees to add to the pull request
# Set to 0 to add all of the assignees.
# Uses numberOfReviewers if unset.
# numberOfAssignees: 2

# A list of keywords to be skipped the process that add reviewers if pull requests include it
# skipKeywords:
# - wip
```

### Assign Author as Assignee
Add the PR creator as assignee to the pull request.

Expand Down
76 changes: 75 additions & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,61 @@ export interface Config {
useAssigneeGroups: boolean
reviewGroups: { [key: string]: string[] }
assigneeGroups: { [key: string]: string[] }
reviewersInTeams: string[]
}

export async function getTeamMembers(
context: Context,
{ org, teamSlug }: { org: string; teamSlug: string }
): Promise<string[]> {
let members: { login: string }[] = []

try {
members = (
await context.github.teams.listMembersInOrg({
org,
// eslint-disable-next-line @typescript-eslint/camelcase
team_slug: teamSlug
})
).data
} catch (error) {
context.log(error)
}

if (members.length === 0) {
let team

try {
team = (
await context.github.teams.getByName({
org,
// eslint-disable-next-line @typescript-eslint/camelcase
team_slug: teamSlug
})
).data
} catch (error) {
context.log(error)
throw new Error(
`Cannot fetch team id for team ${teamSlug} under organisation ${org}.`
)
}

try {
members = (
await context.github.teams.listMembersLegacy({
// eslint-disable-next-line @typescript-eslint/camelcase
team_id: team.id
})
).data
} catch (error) {
context.log(error)
throw new Error(
`Cannot fetch list of members in team ${teamSlug} under organisation ${org}.`
)
}
}

return members.map((member): string => member.login)
}

export async function handlePullRequest(context: Context): Promise<void> {
Expand All @@ -30,7 +85,8 @@ export async function handlePullRequest(context: Context): Promise<void> {
useAssigneeGroups,
assigneeGroups,
addReviewers,
addAssignees
addAssignees,
reviewersInTeams
} = config

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

if (reviewersInTeams && reviewersInTeams.length > 0) {
for (const team of reviewersInTeams) {
const result = /^([\w\-]+)\/([\w\-]+)$/.exec(team)
if (result === null) {
throw new Error(
'Error in configuration file to expand a team reviewersInTeams must be a list of org/team_slug.'
)
}
const reviewers = await getTeamMembers(context, {
org: result[1],
teamSlug: result[2]
})
config.reviewers = Array.from(
new Set((config.reviewers || []).concat(reviewers))
)
}
}

const owner = context.payload.pull_request.user.login

if (addReviewers) {
Expand Down
Loading

0 comments on commit a63a68d

Please sign in to comment.