Add Reviewers to PR #392
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| name: Add Reviewers to PR | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| pr: | |
| description: Pull request number | |
| type: string | |
| required: true | |
| sha: | |
| description: Head sha of the PR | |
| type: string | |
| required: true | |
| ref: | |
| description: Target branch | |
| type: string | |
| required: true | |
| repo: | |
| description: Target repository | |
| type: string | |
| required: true | |
| jobs: | |
| add-reviewers: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Read branch maintainers | |
| id: get_maintainers | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.PAT }} | |
| script: | | |
| const fs = require('fs'); | |
| const maintainers = JSON.parse(fs.readFileSync('./topic_maintainers.json', 'utf8')); | |
| const targetBranch = '${{ github.event.inputs.ref }}'; | |
| if (!maintainers[targetBranch]) { | |
| console.log(`No maintainers found for branch: ${targetBranch}`); | |
| core.setOutput('reviewers', JSON.stringify([])); | |
| core.setOutput('team_reviewers', JSON.stringify([])); | |
| return; | |
| } | |
| const reviewers = maintainers[targetBranch]; | |
| const users = []; | |
| const teams = []; | |
| // Classify reviewers by checking if they are users or teams via API | |
| for (const reviewer of reviewers) { | |
| try { | |
| // Try to get the user first | |
| await github.rest.users.getByUsername({ | |
| username: reviewer | |
| }); | |
| users.push(reviewer); | |
| } catch (error) { | |
| if (error.status === 404) { | |
| // User not found, assume it's a team | |
| teams.push(reviewer); | |
| } else { | |
| console.warn(`Error checking reviewer ${reviewer}: ${error.message}`); | |
| // Default to team if error | |
| teams.push(reviewer); | |
| } | |
| } | |
| } | |
| core.setOutput('reviewers', JSON.stringify(users)); | |
| core.setOutput('team_reviewers', JSON.stringify(teams)); | |
| - name: Add user reviewers to PR | |
| if: fromJson(steps.get_maintainers.outputs.reviewers)[0] != null | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.PAT }} | |
| script: | | |
| const reviewers = JSON.parse('${{ steps.get_maintainers.outputs.reviewers }}'); | |
| if (reviewers.length === 0) { | |
| console.log('No user reviewers to add'); | |
| return; | |
| } | |
| try { | |
| // Get PR details to find the author | |
| const [owner, repo] = '${{ github.event.inputs.repo }}'.split('/'); | |
| const pr = await github.rest.pulls.get({ | |
| owner: owner, | |
| repo: repo, | |
| pull_number: ${{ github.event.inputs.pr }} | |
| }); | |
| const prAuthor = pr.data.user.login; | |
| console.log(`PR author: ${prAuthor}`); | |
| // Filter out the PR author from reviewers | |
| const filteredReviewers = reviewers.filter(r => r !== prAuthor); | |
| if (filteredReviewers.length === 0) { | |
| console.log('All user reviewers are the PR author, skipping'); | |
| return; | |
| } | |
| const result = await github.rest.pulls.requestReviewers({ | |
| owner: owner, | |
| repo: repo, | |
| pull_number: ${{ github.event.inputs.pr }}, | |
| reviewers: filteredReviewers | |
| }); | |
| console.log(`Added user reviewers: ${filteredReviewers.join(', ')}`); | |
| if (reviewers.length !== filteredReviewers.length) { | |
| console.log(`Skipped PR author: ${prAuthor}`); | |
| } | |
| console.log(`Response: ${JSON.stringify(result)}`); | |
| } catch (error) { | |
| console.error(`Failed to add user reviewers: ${error.message}`); | |
| throw error; | |
| } | |
| - name: Add team reviewers to PR | |
| if: fromJson(steps.get_maintainers.outputs.team_reviewers)[0] != null | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.PAT }} | |
| script: | | |
| const teamReviewers = JSON.parse('${{ steps.get_maintainers.outputs.team_reviewers }}'); | |
| if (teamReviewers.length === 0) { | |
| console.log('No team reviewers to add'); | |
| return; | |
| } | |
| try { | |
| // Get PR details to find the author | |
| const [owner, repo] = '${{ github.event.inputs.repo }}'.split('/'); | |
| const pr = await github.rest.pulls.get({ | |
| owner: owner, | |
| repo: repo, | |
| pull_number: ${{ github.event.inputs.pr }} | |
| }); | |
| const prAuthor = pr.data.user.login; | |
| console.log(`PR author: ${prAuthor}`); | |
| // Filter out the PR author from team reviewers (if they happen to be a team slug matching author) | |
| const filteredTeamReviewers = teamReviewers.filter(t => t !== prAuthor); | |
| if (filteredTeamReviewers.length === 0) { | |
| console.log('All team reviewers are the PR author, skipping'); | |
| return; | |
| } | |
| const result = await github.rest.pulls.requestReviewers({ | |
| owner: owner, | |
| repo: repo, | |
| pull_number: ${{ github.event.inputs.pr }}, | |
| team_reviewers: filteredTeamReviewers | |
| }); | |
| console.log(`Added team reviewers: ${filteredTeamReviewers.join(', ')}`); | |
| if (teamReviewers.length !== filteredTeamReviewers.length) { | |
| console.log(`Skipped PR author: ${prAuthor}`); | |
| } | |
| console.log(`Response: ${JSON.stringify(result)}`); | |
| } catch (error) { | |
| console.error(`Failed to add team reviewers: ${error.message}`); | |
| throw error; | |
| } | |
| - name: Summary | |
| if: success() | |
| shell: bash | |
| run: | | |
| echo "### Reviewer Assignment Summary" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**PR Number:** ${{ github.event.inputs.pr }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**Target Branch:** ${{ github.event.inputs.ref }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**Target Repo:** ${{ github.event.inputs.repo }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**Head SHA:** ${{ github.event.inputs.sha }}" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**User Reviewers Added:** $(echo '${{ steps.get_maintainers.outputs.reviewers }}' | jq -r '.[]' | tr '\n' ',' | sed 's/,$//')" >> $GITHUB_STEP_SUMMARY | |
| echo "**Team Reviewers Added:** $(echo '${{ steps.get_maintainers.outputs.team_reviewers }}' | jq -r '.[]' | tr '\n' ',' | sed 's/,$//')" >> $GITHUB_STEP_SUMMARY |