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

Modernize repo and allow choosing of User Groups conditionally #192

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.13.1
46 changes: 46 additions & 0 deletions __tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,52 @@ describe('handlePullRequest', () => {
expect(addAssigneesSpy).not.toBeCalled()
})

test('adds reviewers to pull request from two different groups if review groups are enabled and user is in group', async () => {
// MOCKS
;(github.getOctokit as jest.Mock).mockImplementation(() => ({
rest: {
pulls: {
requestReviewers: async () => {},
},
issues: {
addAssignees: async () => {},
},
},
}))

const client = github.getOctokit('token')

const requestReviewersSpy = jest.spyOn(
client.rest.pulls,
'requestReviewers'
)

const addAssigneesSpy = jest.spyOn(client.rest.issues, 'addAssignees')

// GIVEN
const config = {
addAssignees: false,
addReviewers: true,
useReviewGroups: true,
numberOfReviewers: 1,
reviewGroups: {
groupA: ['pr-creator', 'group1-user1', 'group1-user2', 'group1-user3'],
groupB: ['group2-user1', 'group2-user2', 'group2-user3'],
},
chooseOnlyUserGroups: true,
} as any

// WHEN
await handler.handlePullRequest(client, context, config)

// THEN
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(1)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![0]).toMatch(
/group1/
)
expect(addAssigneesSpy).not.toBeCalled()
})

test('adds all reviewers from a group that has less members than the number of reviews requested', async () => {
// MOCKS
;(github.getOctokit as jest.Mock).mockImplementation(() => ({
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Auto Assign Action'
description: 'Add reviewers to pull requests when pull requests are opened.'
author: 'Kentaro Matsushita'
author: 'Arush Shankar'
inputs:
repo-token:
description: 'A token for the repo'
Expand Down
21 changes: 20 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0",
"js-yaml": "^3.13.1",
"lodash": "^4.17.20"
"lodash": "^4.17.20",
"yarn": "^1.22.22"
},
"devDependencies": {
"@octokit/webhooks-types": "7.5.1",
Expand All @@ -41,5 +42,6 @@
},
"lint-staged": {
"*.ts": "prettier --write"
}
},
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
1 change: 1 addition & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Config {
reviewGroups: { [key: string]: string[] }
assigneeGroups: { [key: string]: string[] }
runOnDraft?: boolean
chooseOnlyUserGroups?: boolean
}

export async function handlePullRequest(
Expand Down
21 changes: 17 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { Config } from './handler'
import { Client } from './types'

export function chooseReviewers(owner: string, config: Config): string[] {
const { useReviewGroups, reviewGroups, numberOfReviewers, reviewers } = config
const {
useReviewGroups,
reviewGroups,
numberOfReviewers,
reviewers,
chooseOnlyUserGroups = false,
} = config
let chosenReviewers: string[] = []
const useGroups: boolean =
useReviewGroups && Object.keys(reviewGroups).length > 0
Expand All @@ -13,7 +19,8 @@ export function chooseReviewers(owner: string, config: Config): string[] {
chosenReviewers = chooseUsersFromGroups(
owner,
reviewGroups,
numberOfReviewers
numberOfReviewers,
chooseOnlyUserGroups
)
} else {
chosenReviewers = chooseUsers(reviewers, numberOfReviewers, owner)
Expand Down Expand Up @@ -95,11 +102,17 @@ export function includesSkipKeywords(
export function chooseUsersFromGroups(
owner: string,
groups: { [key: string]: string[] } | undefined,
desiredNumber: number
desiredNumber: number,
chooseOnlyUserGroups: boolean = false
): string[] {
let users: string[] = []
for (const group in groups) {
users = users.concat(chooseUsers(groups[group], desiredNumber, owner))
if (
!chooseOnlyUserGroups ||
(chooseOnlyUserGroups && groups[group].includes(owner))
) {
users = users.concat(chooseUsers(groups[group], desiredNumber, owner))
}
}
return users
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["es2016"],
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
Expand Down
Loading