Skip to content

Commit

Permalink
feat(lottery): avoid duplications of the selected reviewers (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
monorilaci authored Jun 2, 2023
1 parent 9618241 commit c291d74
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
55 changes: 55 additions & 0 deletions __tests__/lottery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const prNumber = 123
const ref = 'refs/pull/branch-name'
const basePull = {number: prNumber, head: {ref}}

function hasDuplicates(array: string[]): boolean {
return (new Set(array)).size !== array.length;
}

const mockGetPull = (pull: Pull) =>
nock('https://api.github.com')
.get('/repos/uesteibar/repository/pulls')
Expand Down Expand Up @@ -57,6 +61,57 @@ test('selects reviewers from a pool of users, ignoring author', async () => {
nock.cleanAll()
})

test('selects reviewers from a pool of users, ignoring author and the selected reviewers from another group', async () => {
const pull = {
...basePull,
user: { login: 'author' },
draft: false,
};

const getPullMock = mockGetPull(pull);

const candidatesTeamA = ['A', 'B', 'author'];
const candidatesTeamB = ['C', 'D', 'author'];
const allCandidates = ['A', 'B', 'C', 'D', 'author'];

const postReviewersMock = nock('https://api.github.com')
.post(`/repos/uesteibar/repository/pulls/${prNumber}/requested_reviewers`, (body): boolean => {
body.reviewers.forEach((reviewer: string) => {
expect(allCandidates).toContain(reviewer);
expect(reviewer).not.toEqual('author');
});
expect(body.reviewers.length).toEqual(4);
expect(hasDuplicates(body.reviewers)).toBe(false);
return true;
})
.reply(200, pull);

const config = {
groups: [
{
name: 'Test-A',
reviewers: 2,
usernames: candidatesTeamA,
},
{
name: 'Test-B',
reviewers: 2,
usernames: candidatesTeamB,
},
],
};

await runLottery(octokit, config, {
repository: 'uesteibar/repository',
ref,
});

getPullMock.done();
postReviewersMock.done();

nock.cleanAll();
});

test("doesn't assign reviewers if the PR is in draft state", async () => {
const pull = {
...basePull,
Expand Down
6 changes: 3 additions & 3 deletions src/lottery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Lottery {

if (reviewersToRequest) {
selected = selected.concat(
this.pickRandom(usernames, reviewersToRequest, author)
this.pickRandom(usernames, reviewersToRequest, selected.concat(author))
)
}
}
Expand All @@ -100,10 +100,10 @@ class Lottery {
return selected
}

pickRandom(items: string[], n: number, ignore: string): string[] {
pickRandom(items: string[], n: number, ignore: string[]): string[] {
const picks: string[] = []

const candidates = items.filter(item => item !== ignore)
const candidates = items.filter(item => !ignore.includes(item));

while (picks.length < n) {
const random = Math.floor(Math.random() * candidates.length)
Expand Down

0 comments on commit c291d74

Please sign in to comment.