-
Notifications
You must be signed in to change notification settings - Fork 51
ci: add assign random reviewer workflow #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| name: Assign Random Reviewer | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, ready_for_review, reopened] | ||
|
|
||
| jobs: | ||
| assign-reviewer: | ||
| runs-on: ubuntu-latest | ||
| # Skip draft PRs | ||
| if: github.event.pull_request.draft == false | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const fs = require('fs'); | ||
|
|
||
| // Parse CODEOWNERS into [{pattern, owners}] entries | ||
| function parseCODEOWNERS(content) { | ||
| return content | ||
| .split('\n') | ||
| .map(line => line.trim()) | ||
| .filter(line => line && !line.startsWith('#')) | ||
| .map(line => { | ||
| const parts = line.split(/\s+/); | ||
| return { | ||
| pattern: parts[0], | ||
| owners: parts.slice(1).map(o => o.replace(/^@/, '').toLowerCase()) | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| // Check if a file path matches a CODEOWNERS pattern | ||
| function matches(filePath, pattern) { | ||
| // Normalise — strip leading slash from pattern | ||
| const p = pattern.replace(/^\//, ''); | ||
| if (p === '*') return true; | ||
| if (p.endsWith('/')) return filePath.startsWith(p); | ||
| if (p.includes('*')) { | ||
| const re = new RegExp('^' + p.replace(/\*/g, '.*') + '$'); | ||
| return re.test(filePath); | ||
| } | ||
| return filePath === p || filePath.startsWith(p + '/'); | ||
| } | ||
|
|
||
| // Read CODEOWNERS (try both locations) | ||
| let codeownersContent = ''; | ||
| for (const path of ['.github/CODEOWNERS', 'CODEOWNERS']) { | ||
| if (fs.existsSync(path)) { | ||
| codeownersContent = fs.readFileSync(path, 'utf8'); | ||
| console.log(`Found CODEOWNERS at ${path}`); | ||
| break; | ||
| } | ||
| } | ||
| if (!codeownersContent) { | ||
| console.log('No CODEOWNERS file found — skipping'); | ||
| return; | ||
| } | ||
|
|
||
| const rules = parseCODEOWNERS(codeownersContent); | ||
| console.log(`Parsed ${rules.length} CODEOWNERS rules`); | ||
|
|
||
| // Get all files changed in this PR | ||
| const { data: files } = await github.rest.pulls.listFiles({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: context.payload.pull_request.number, | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| console.log(`PR has ${files.length} changed files`); | ||
|
|
||
| // For each changed file, find the last matching CODEOWNERS rule | ||
| // (GitHub uses last-match-wins semantics) | ||
| const ownerSet = new Set(); | ||
| for (const file of files) { | ||
| for (let i = rules.length - 1; i >= 0; i--) { | ||
| if (matches(file.filename, rules[i].pattern)) { | ||
| rules[i].owners.forEach(o => ownerSet.add(o)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Remove the PR author from candidates | ||
| const author = context.payload.pull_request.user.login.toLowerCase(); | ||
| const candidates = [...ownerSet].filter(o => o !== author); | ||
|
|
||
| console.log(`Candidates after excluding author (${author}): ${candidates.join(', ') || 'none'}`); | ||
|
|
||
| if (candidates.length === 0) { | ||
| console.log('No eligible reviewers found after excluding PR author'); | ||
| return; | ||
| } | ||
|
|
||
| // Pick one at random | ||
| const chosen = candidates[Math.floor(Math.random() * candidates.length)]; | ||
| console.log(`Randomly chose: ${chosen}`); | ||
|
|
||
| await github.rest.pulls.requestReviewers({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: context.payload.pull_request.number, | ||
| reviewers: [chosen] | ||
| }); | ||
|
|
||
| console.log(`Successfully assigned ${chosen} as reviewer`); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Copilot Autofix
AI 12 days ago
To fix this, add an explicit
permissions:block that grants only the minimal scopes required. This job needs to (1) read pull request details and files, and (2) request reviewers. That implies at leastcontents: read(to safely read repository data if required) andpull-requests: writeto assign reviewers. Declarepermissions:at the job level (assign-reviewer) so it applies only to this job and does not unintentionally affect other workflows.Concretely, in
.github/workflows/assign-random-reviewer.yml, underjobs: assign-reviewer:and at the same indentation level asruns-on, insert:No imports or additional methods are needed because this is a YAML configuration change only; existing functionality (assigning a random reviewer) will continue working, but with a GITHUB_TOKEN limited to the specific capabilities it uses.