Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reviewersInTeams): using teams to add reviews #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ default_permissions:

# Organization members and teams.
# https://developer.github.com/v3/apps/permissions/#permission-on-members
# members: read
members: read

# View and manage users blocked by the organization.
# https://developer.github.com/v3/apps/permissions/#permission-on-organization-user-blocking
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