Skip to content

Commit

Permalink
Add second_reviewer.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 17, 2024
1 parent 26b0be8 commit 3dcae19
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions .github/workflows/second_reviewer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Second Reviewer Assignment

on:
pull_request_target:
types: [ labeled ]

permissions:
pull-requests: write

jobs:
assign-reviewer:
if: ${{ contains(github.event.pull_request.labels.*.name, format('PR{0} second reviewer', ':')) }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Get team members
id: get-team-members
uses: actions/github-script@v7
with:
github-token: ${{secrets.PAT_TOKEN_FOR_SECOND_REVIEWER_UNTIL_JAN_2025}}
script: |
/*
* Using PAT_TOKEN_FOR_SECOND_REVIEWER_UNTIL_JAN_2025 because the current workflow
* lacks sufficient permissions to view members of the Tribler organization.
* This token is valid until January 2025. It's issued for a maximum of one year,
* so in January 2025, it will expire and need to be replaced with a new token.
*/
const teamResponse = await github.rest.teams.listMembersInOrg({
org: 'Tribler',
team_slug: 'reviewers'
});
const allReviewers = teamResponse.data.map(member => member.login);
return allReviewers;
- name: Add second reviewer
uses: actions/github-script@v7
env:
ALL_REVIEWERS: ${{ steps.get-team-members.outputs.result }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const pullRequest = context.payload.pull_request;
const author = pullRequest.user.login;
console.log("Author:", author);
const currentReviewers = pullRequest.requested_reviewers.map(reviewer => reviewer.login);
console.log("Current Reviewers:", currentReviewers);
// Get the list of potential reviewers from the previous step
const allReviewers = JSON.parse(process.env.ALL_REVIEWERS)
console.log("Potential Reviewers:", allReviewers);
// Filter out the PR author and current reviewers
const eligibleReviewers = allReviewers.filter(reviewer => reviewer !== author && !currentReviewers.includes(reviewer));
console.log("Eligible Reviewers:", eligibleReviewers);
// Randomly select a reviewer
if (eligibleReviewers.length > 0) {
const randomReviewer = eligibleReviewers[Math.floor(Math.random() * eligibleReviewers.length)];
console.log("Selected Reviewer:", randomReviewer);
// Assign the selected reviewer
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequest.number,
reviewers: [randomReviewer]
});
// Add a comment explaining the selection
const comment = `A 'second reviewer' has been requested for this pull request. @${randomReviewer} has been randomly selected as the second opinion reviewer. This action is part of the Tie Breaker mechanism designed to resolve conflicts. The decision of the 'second reviewer' is considered final in the dispute.`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: comment
});
} else {
// Add a comment indicating no eligible reviewers are left
const noReviewerComment = `All eligible reviewers have already been added to this pull request.`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: noReviewerComment
});
console.log("No eligible reviewers left to add.");
}
// Remove the 'PR: second reviewer' label
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
name: 'PR: second reviewer'
});
console.log("Label 'PR: second reviewer' removed successfully.");

0 comments on commit 3dcae19

Please sign in to comment.