Skip to content
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
13 changes: 7 additions & 6 deletions scripts/assign-reviewers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getJuniorRequesters, TEAM_SLUG } from './junior-requesters.mjs';

const REQUIRED_REVIEWERS = 3;
// Managers who are members of the SDK teams, but should never be auto-requested for review.
// GitHub's team auto-assignment can still pick them, so their pending requests don't count towards the required reviewers.
// GitHub's team auto-assignment can still pick them, so their requests and reviews don't count towards the required reviewers.
const NEVER_REQUEST = ['mydea', 'stephanie-anderson', 'HazAT'];
const REVIEWERS_PATH = '.github/REVIEWERS';
const REVIEW_LOAD_DAYS = 7;
Expand Down Expand Up @@ -208,16 +208,17 @@ export async function assignReviewers({ github, context, core }) {
// Whoever asked Junior for the PR is its de facto author.
const requesters = await getJuniorRequesters({ github, core, pr: currentPr });

const neverRequest = new Set(NEVER_REQUEST.map(login => login.toLowerCase()));
const requestedReviewers = currentPr.requested_reviewers.filter(user => !neverRequest.has(user.login.toLowerCase()));
// The people who own the PR comment on it while iterating, and that isn't a review.
const notReviewers = [author, ...requesters, ...currentPr.assignees.map(user => user.login), ...NEVER_REQUEST];
const notCounted = new Set(notReviewers.map(login => login.toLowerCase()));

const reviews = await github.paginate(github.rest.pulls.listReviews, { owner, repo, pull_number });
// Requested reviewers drop off `requested_reviewers` once they submit a review, so count both.
// Bots (AI reviewers) don't count towards the required reviewers.
const existing = [
...new Set(
[...requestedReviewers, ...reviews.map(r => r.user)]
.filter(user => user && user.type !== 'Bot' && user.login.toLowerCase() !== author.toLowerCase())
[...currentPr.requested_reviewers, ...reviews.map(r => r.user)]
.filter(user => user && user.type !== 'Bot' && !notCounted.has(user.login.toLowerCase()))
.map(user => user.login),
),
];
Expand Down Expand Up @@ -252,7 +253,7 @@ export async function assignReviewers({ github, context, core }) {
existing,
codeownerPool,
teamPool,
exclude: [author, ...requesters, ...NEVER_REQUEST, ...busy],
exclude: [...notReviewers, ...busy],
load,
});

Expand Down
26 changes: 26 additions & 0 deletions scripts/assign-reviewers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,30 @@ describe('assignReviewers', () => {
expect.objectContaining({ reviewers: expect.not.arrayContaining(['Mydea']) }),
);
});

it('does not count the assignee towards the required reviewers, nor request their review', async () => {
const { github, run } = setup(assignReviewers, {
user: { login: 'author', type: 'User' },
assignees: [{ login: 'A', type: 'User' }],
requested_reviewers: [{ login: 'a', type: 'User' }],
});
await run();
expect(github.rest.pulls.requestReviewers).toHaveBeenCalledWith(
expect.objectContaining({ reviewers: expect.arrayContaining(['b', 'c', 'd']) }),
);
});

it('does not count reviews from the requester of a Junior PR towards the required reviewers', async () => {
const { github, run } = setup(assignReviewers, { user: junior, body: attribution('@a') });
const paginate = github.paginate.getMockImplementation();
github.paginate.mockImplementation(async method =>
method === github.rest.pulls.listReviews
? [{ user: { login: 'a', type: 'User' } }, { user: { login: 'mydea', type: 'User' } }]
: paginate(method),
);
await run();
expect(github.rest.pulls.requestReviewers).toHaveBeenCalledWith(
expect.objectContaining({ reviewers: expect.arrayContaining(['b', 'c', 'd']) }),
);
});
});
Loading