Skip to content

Commit dd069a4

Browse files
Auto Assign Reviewers for PRs (#2643)
## Summary TSIA, add myself + 1 random reviewer from our eng org. ## How was it tested? ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request, I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license). --------- Signed-off-by: John Lago <[email protected]> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 411f38e commit dd069a4

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Random Reviewer Assignment
2+
on:
3+
pull_request:
4+
types: [opened]
5+
6+
permissions:
7+
contents: read
8+
pull-requests: write
9+
10+
env:
11+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_FOR_PR_ASSIGNMENT }}
12+
13+
jobs:
14+
assign-reviewer:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Randomly assign reviewer from team
18+
uses: actions/github-script@v6
19+
with:
20+
script: |
21+
const TRIAGE_USERNAME = 'Lagoja';
22+
const EXCLUDE_USERNAMES = ['jetpack-io-bot'];
23+
24+
try {
25+
const authenticatedUser = await github.rest.users.getAuthenticated();
26+
27+
const teamMembers = await github.rest.teams.listMembersInOrg({
28+
org: 'jetify-com',
29+
team_slug: 'eng'
30+
});
31+
32+
const prAuthor = context.payload.pull_request.user.login.toLowerCase();
33+
const prAuthorId = context.payload.pull_request.user.id;
34+
const authenticatedUserLower = authenticatedUser.data.login.toLowerCase();
35+
36+
// If the PR author is already a member of the team, we can skip random assignment
37+
const isPrAuthorInTeam = teamMembers.data.some(member =>
38+
member.login.toLowerCase() === prAuthor && member.id === prAuthorId
39+
);
40+
41+
if (isPrAuthorInTeam) {
42+
console.log(`PR author ${prAuthor} is already a team member, skipping random assignment.`);
43+
return;
44+
}
45+
46+
// Get eligible reviewers (excluding PR author, authenticated user, and lagoja)
47+
const eligibleReviewers = teamMembers.data
48+
.filter(member => {
49+
const loginLower = member.login.toLowerCase();
50+
51+
// Exclude authenticated user
52+
const isNotAuthenticatedUser = member.id !== authenticatedUser.data.id;
53+
const isNotTriage = loginLower !== TRIAGE_USERNAME.toLowerCase();
54+
const isNotExcludedUsername = !EXCLUDE_USERNAMES.includes(loginLower);
55+
56+
return isNotAuthenticatedUser && isNotTriage && isNotExcludedUsername;
57+
})
58+
.map(member => member.login);
59+
60+
console.log(`Eligible reviewers: ${eligibleReviewers.join(', ')}`);
61+
62+
if (eligibleReviewers.length === 0) {
63+
console.log('No eligible reviewers found');
64+
return;
65+
}
66+
67+
const randomReviewer = eligibleReviewers[Math.floor(Math.random() * eligibleReviewers.length)];
68+
const reviewers = [randomReviewer];
69+
70+
// Only add TRIAGE_USERNAME if they're not the PR author and not the authenticated user
71+
if (prAuthor !== TRIAGE_USERNAME.toLowerCase() &&
72+
authenticatedUserLower !== TRIAGE_USERNAME.toLowerCase()) {
73+
reviewers.push(TRIAGE_USERNAME);
74+
}
75+
76+
console.log(`Final reviewers: ${reviewers.join(', ')}`);
77+
78+
console.log(`Assigning reviewers: ${reviewers.join(', ')}`);
79+
80+
await github.rest.pulls.requestReviewers({
81+
owner: context.repo.owner,
82+
repo: context.repo.repo,
83+
pull_number: context.payload.pull_request.number,
84+
reviewers
85+
});
86+
87+
} catch (error) {
88+
console.error('Error assigning reviewer:', error);
89+
}

0 commit comments

Comments
 (0)