-
Notifications
You must be signed in to change notification settings - Fork 332
PR review bot #3326
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
Merged
+212
−0
Merged
PR review bot #3326
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5ddd48f
PR review bot
souvikghosh04 5ff5538
updated reviewers
souvikghosh04 d64bb42
draft PR inclusion
souvikghosh04 12105c5
test draft PR for review bot
souvikghosh04 8c11874
copilot review fixes
souvikghosh04 7e2463c
Merge branch 'main' into Usr/sogh/prreviewbot
souvikghosh04 d085f1f
Merge branch 'main' into Usr/sogh/prreviewbot
Aniruddh25 9cfc781
added fixes and PR review suggesstions
souvikghosh04 0ec4dfc
Merge branch 'main' of https://github.com/Azure/data-api-builder into…
souvikghosh04 77733d6
Fixed review comments
souvikghosh04 194e274
Fixes and improvements
souvikghosh04 b169d89
Address review comments: paginate listReviews, strip label after assi…
souvikghosh04 d2fe2f3
Address review comments + add temp push trigger for testing
souvikghosh04 d74bcae
Merge branch 'main' into Usr/sogh/prreviewbot
souvikghosh04 9fb3069
Merge branch 'Usr/sogh/prreviewbot' of https://github.com/Azure/data-…
souvikghosh04 f9d9723
Fix: exclude PR author from assigned count — author cannot review the…
souvikghosh04 f700c93
Fix: remove duplicate author declaration
souvikghosh04 ac432e6
Turn off dry-run for live testing
souvikghosh04 97ec29a
Remove GHCP inst file and TODO section
souvikghosh04 1ca8743
remove GHCP inst file
souvikghosh04 7b206d6
Merge branch 'main' into Usr/sogh/prreviewbot
souvikghosh04 a43a208
Merge branch 'main' into Usr/sogh/prreviewbot
Aniruddh25 7973eef
Set DRY_RUN to false for pull_request_target events
souvikghosh04 2972316
Randomize assignees instead of fixed/alphabetical ordering
souvikghosh04 ca3d956
Merge branch 'main' into Usr/sogh/prreviewbot
aaronburtle 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,182 @@ | ||
| name: PR Review Assignment Bot | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened, labeled] | ||
|
|
||
| workflow_dispatch: | ||
| inputs: | ||
| dry_run: | ||
| description: 'Run in dry-run mode (no actual assignments)' | ||
|
souvikghosh04 marked this conversation as resolved.
|
||
| required: false | ||
| default: 'true' | ||
| type: choice | ||
| options: | ||
| - 'true' | ||
| - 'false' | ||
|
|
||
| # schedule: | ||
| # - cron: "*/10 * * * *" | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: pr-review-assignment | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| assign-reviewers: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
|
souvikghosh04 marked this conversation as resolved.
|
||
| - name: Assign reviewers to eligible PRs | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const reviewers = ["souvikghosh04", "Aniruddh25", "aaronburtle", "anushakolan", "RubenCerna2079", "JerryNixon"]; | ||
| const REQUIRED_REVIEWERS = 2; | ||
| const DRY_RUN = '${{ github.event.inputs.dry_run || 'true' }}' === 'true'; | ||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const { owner, repo } = context.repo; | ||
|
|
||
| // Fetch all open PRs (paginated) | ||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| const allPRs = await github.paginate(github.rest.pulls.list, { | ||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| owner, | ||
| repo, | ||
| state: "open", | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| // Fetch the set of configured reviewers who are requested OR have already submitted a review. | ||
| // GitHub removes reviewers from requested_reviewers once they submit, so we must check both. | ||
| async function getConfiguredReviewerSet(pr) { | ||
| const requested = (pr.requested_reviewers || []) | ||
| .map((r) => r.login) | ||
| .filter((r) => reviewers.includes(r)); | ||
|
|
||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| const { data: reviews } = await github.rest.pulls.listReviews({ | ||
| owner, | ||
| repo, | ||
| pull_number: pr.number, | ||
| }); | ||
| const submitted = reviews | ||
| .map((r) => r.user.login) | ||
| .filter((r) => reviewers.includes(r)); | ||
|
|
||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| return [...new Set([...requested, ...submitted])]; | ||
| } | ||
|
|
||
| // Determine if a PR is eligible by labels and draft status. | ||
| // Reviewer count is checked later after fetching review data. | ||
| function isEligibleByLabels(pr) { | ||
| const labels = pr.labels.map((l) => l.name); | ||
| if (!labels.includes("assign-for-review")) return false; | ||
| if (pr.draft) return false; | ||
| return true; | ||
| } | ||
|
|
||
| // Calculate PR weight from labels | ||
| function getWeight(pr) { | ||
| const labels = pr.labels.map((l) => l.name); | ||
|
souvikghosh04 marked this conversation as resolved.
|
||
| let weight = 1; | ||
| if (labels.includes("size-medium")) weight = 2; | ||
| else if (labels.includes("size-large")) weight = 3; | ||
| if (labels.includes("priority-high")) weight += 1; | ||
| return weight; | ||
| } | ||
|
|
||
| // Build load map from all load-relevant PRs (assigned + unassigned) | ||
| const load = {}; | ||
| for (const r of reviewers) { | ||
| load[r] = 0; | ||
| } | ||
|
|
||
| core.info(`Total open PRs fetched: ${allPRs.length}`); | ||
|
|
||
| const labelEligiblePRs = allPRs.filter(isEligibleByLabels); | ||
| core.info(`Label-eligible PRs (non-draft, has label): ${labelEligiblePRs.length}`); | ||
|
|
||
| // Fetch configured reviewer sets for all label-eligible PRs (used for load + eligibility) | ||
| const prReviewerMap = new Map(); | ||
| for (const pr of labelEligiblePRs) { | ||
| const configuredSet = await getConfiguredReviewerSet(pr); | ||
| prReviewerMap.set(pr.number, configuredSet); | ||
| } | ||
|
|
||
| // Build load from all label-eligible PRs (includes fully-assigned ones) | ||
| for (const pr of labelEligiblePRs) { | ||
| const weight = getWeight(pr); | ||
| const configuredSet = prReviewerMap.get(pr.number); | ||
| for (const reviewer of configuredSet) { | ||
| if (reviewer in load) { | ||
| load[reviewer] += weight; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| core.info(`Current load: ${JSON.stringify(load)}`); | ||
|
|
||
| // Filter to PRs that still need reviewers | ||
| const eligiblePRs = labelEligiblePRs.filter((pr) => { | ||
| return prReviewerMap.get(pr.number).length < REQUIRED_REVIEWERS; | ||
| }); | ||
| core.info(`Total eligible PRs (need reviewers): ${eligiblePRs.length}`); | ||
|
|
||
| // Sort eligible PRs by weight descending (prioritize large PRs) | ||
| eligiblePRs.sort((a, b) => getWeight(b) - getWeight(a)); | ||
|
|
||
| // Assign reviewers to each eligible PR | ||
| for (const pr of eligiblePRs) { | ||
| const weight = getWeight(pr); | ||
| const configuredSet = prReviewerMap.get(pr.number); | ||
| const needed = REQUIRED_REVIEWERS - configuredSet.length; | ||
|
|
||
| core.info(`PR #${pr.number} — weight: ${weight}, configured reviewers (requested + submitted): [${configuredSet.join(", ")}]`); | ||
|
|
||
| if (needed <= 0) { | ||
| core.info(`PR #${pr.number} already has ${REQUIRED_REVIEWERS} configured reviewers, skipping.`); | ||
| continue; | ||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const author = pr.user.login; | ||
|
|
||
| // Build candidates: exclude author and already-assigned/reviewed reviewers | ||
| const candidates = reviewers | ||
| .filter((r) => r !== author && !configuredSet.includes(r)); | ||
|
|
||
| if (candidates.length === 0) { | ||
| core.info(`PR #${pr.number} — no candidates after filtering.`); | ||
| continue; | ||
| } | ||
|
|
||
| candidates.sort((a, b) => { | ||
| if (load[a] !== load[b]) return load[a] - load[b]; | ||
| return a.localeCompare(b); | ||
| }); | ||
|
|
||
| core.info(`PR #${pr.number} candidates: ${JSON.stringify(candidates)}`); | ||
|
|
||
| const selected = candidates.slice(0, needed); | ||
|
|
||
| if (DRY_RUN) { | ||
| core.info(`[DRY RUN] Would assign [${selected.join(", ")}] to PR #${pr.number}`); | ||
| } else { | ||
| await github.rest.pulls.requestReviewers({ | ||
| owner, | ||
| repo, | ||
| pull_number: pr.number, | ||
| reviewers: selected, | ||
|
souvikghosh04 marked this conversation as resolved.
Outdated
|
||
| }); | ||
| core.info(`Assigned [${selected.join(", ")}] to PR #${pr.number}`); | ||
| } | ||
|
|
||
|
souvikghosh04 marked this conversation as resolved.
|
||
| // Update load in-memory | ||
| for (const reviewer of selected) { | ||
| load[reviewer] += weight; | ||
| } | ||
|
|
||
| core.info(`Updated load: ${JSON.stringify(load)}`); | ||
| } | ||
|
|
||
| core.info("Review assignment complete."); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.