-
Notifications
You must be signed in to change notification settings - Fork 330
Actions | Introduce auto-assign-pr workflow #4275
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
Open
cheenamalhotra
wants to merge
9
commits into
main
Choose a base branch
from
dev/cheena/auto-assign-prs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
059f419
Actions | Introduce auto-assign-pr workflow
cheenamalhotra a63f1ec
Permissions fix
cheenamalhotra 814bd7b
Enable workflow only on 'pull_request_target' event.
cheenamalhotra 43f68b5
Potential fix for pull request finding
cheenamalhotra 34fa483
Apply suggestions from code review
cheenamalhotra 6d4594b
Fix auto-assign workflow permissions and PR-state race handling
Copilot 8c80c88
Make auto-assign pool configurable via repo variables
Copilot 01e5ef0
Use variable for pool
cheenamalhotra 920f08b
Fix auto-assign workflow variable mismatch and duplicate declarations
Copilot 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,156 @@ | ||
| name: Auto Assign PR Load Balancer | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened, ready_for_review, milestoned] | ||
|
|
||
| concurrency: | ||
| group: auto-assign-pr-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| load-balance-assignees: | ||
| # Only run for assignment-eligible PRs in the upstream repository. Use | ||
| # pull_request_target for both same-repo and fork PRs so the workflow | ||
| # only runs once per PR event. | ||
| if: github.repository == 'dotnet/SqlClient' && github.event.pull_request.draft == false && github.event.pull_request.milestone != null | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| issues: write | ||
|
cheenamalhotra marked this conversation as resolved.
|
||
| pull-requests: read | ||
| env: | ||
| AUTO_ASSIGN_PR_POOL: ${{ vars.AUTO_ASSIGN_PR_POOL }} | ||
| AUTO_ASSIGN_PR_SKIP: ${{ vars.AUTO_ASSIGN_PR_SKIP }} | ||
|
|
||
| steps: | ||
| - name: Calculate Workload and Apply | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const owner = context.repo.owner; | ||
| const repo = context.repo.repo; | ||
| const prNumber = context.issue.number; | ||
| const author = context.payload.pull_request.user.login; | ||
| const normalizeLogin = login => login.toLowerCase(); | ||
| const parseCsvLogins = value => (value ?? '') | ||
| .split(',') | ||
| .map(entry => entry.trim()) | ||
| .filter(entry => entry.length > 0); | ||
|
|
||
| // Fallback pool keeps behavior unchanged when no repo variable is configured. | ||
| const defaultPool = ['cheenamalhotra', 'paulmedynski', 'priyankatiwari08', 'benrr101', 'mdaigle', 'apoorvdeshmukh']; | ||
| const configuredPool = parseCsvLogins(process.env.AUTO_ASSIGN_PR_POOL); | ||
| const rawPool = configuredPool.length > 0 ? configuredPool : defaultPool; | ||
| const skipUsers = new Set(parseCsvLogins(process.env.AUTO_ASSIGN_PR_SKIP).map(normalizeLogin)); | ||
| const seenPoolUsers = new Set(); | ||
| const pool = []; | ||
|
cheenamalhotra marked this conversation as resolved.
|
||
| for (const user of rawPool) { | ||
| const lower = normalizeLogin(user); | ||
| if (!seenPoolUsers.has(lower)) { | ||
| seenPoolUsers.add(lower); | ||
| pool.push(user); | ||
| } | ||
| } | ||
|
|
||
| let latestPr; | ||
| try { | ||
| const response = await github.rest.pulls.get({ | ||
| owner, | ||
| repo, | ||
| pull_number: prNumber | ||
| }); | ||
| latestPr = response.data; | ||
| } catch (error) { | ||
| throw new Error(`Failed to fetch latest PR details: ${error.message}`); | ||
| } | ||
|
|
||
| if (latestPr.draft || !latestPr.milestone) { | ||
| console.log('PR is no longer assignment-eligible (draft or missing milestone).'); | ||
| return; | ||
| } | ||
|
|
||
| const currentAssignees = (latestPr.assignees ?? []).map(a => a.login); | ||
|
|
||
| console.log(`PR Author: ${author}`); | ||
|
cheenamalhotra marked this conversation as resolved.
|
||
| console.log(`Event Name: ${context.eventName}; Is Fork PR: ${context.payload.pull_request.head.repo.fork === true}`); | ||
| console.log(`Current Assignees: ${currentAssignees.join(', ')}`); | ||
|
|
||
| if (currentAssignees.length >= 2) { | ||
| console.log('PR already has 2 or more assignees. No action needed.'); | ||
| return; | ||
| } | ||
|
|
||
|
cheenamalhotra marked this conversation as resolved.
|
||
| const neededAssigneesCount = 2 - currentAssignees.length; | ||
|
|
||
| const candidates = pool.filter(user => | ||
| normalizeLogin(user) !== normalizeLogin(author) && | ||
| !skipUsers.has(normalizeLogin(user)) && | ||
| !currentAssignees.some(a => normalizeLogin(a) === normalizeLogin(user)) | ||
| ); | ||
|
|
||
|
cheenamalhotra marked this conversation as resolved.
|
||
| if (candidates.length === 0) { | ||
| console.log('No valid candidates left in the pool.'); | ||
| return; | ||
| } | ||
|
|
||
| const workloads = {}; | ||
| const canonicalCandidateByLower = {}; | ||
| candidates.forEach(user => { | ||
| const lower = normalizeLogin(user); | ||
| workloads[lower] = 0; | ||
| canonicalCandidateByLower[lower] = user; | ||
| }); | ||
|
|
||
| try { | ||
| // Rank candidates by current assignment count across all open PRs. | ||
| const iterator = github.paginate.iterator(github.rest.pulls.list, { | ||
| owner, | ||
| repo, | ||
| state: 'open', | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| for await (const response of iterator) { | ||
| for (const pr of response.data) { | ||
| if (pr.assignees) { | ||
| for (const assignee of pr.assignees) { | ||
| const login = normalizeLogin(assignee.login); | ||
| if (workloads[login] !== undefined) { | ||
| workloads[login]++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (error) { | ||
| throw new Error(`Failed to fetch open PRs for auto-assignment: ${error.message}`); | ||
| } | ||
|
|
||
| const workloadArray = candidates.map(user => { | ||
| const lower = normalizeLogin(user); | ||
| return { user: canonicalCandidateByLower[lower], count: workloads[lower] }; | ||
| }); | ||
| console.log('Current Workloads:', workloadArray); | ||
|
|
||
| // Shuffle before sorting so ties are broken fairly instead of favoring pool order. | ||
| for (let i = workloadArray.length - 1; i > 0; i--) { | ||
| const j = Math.floor(Math.random() * (i + 1)); | ||
| [workloadArray[i], workloadArray[j]] = [workloadArray[j], workloadArray[i]]; | ||
| } | ||
|
|
||
| workloadArray.sort((a, b) => a.count - b.count); | ||
|
|
||
| const selectedAssignees = workloadArray.slice(0, neededAssigneesCount).map(w => w.user); | ||
| console.log(`Selected candidates: ${selectedAssignees.join(', ')}`); | ||
|
|
||
| if (selectedAssignees.length === 0) { | ||
| console.log('No assignees selected. No action needed.'); | ||
| return; | ||
| } | ||
|
|
||
| await github.rest.issues.addAssignees({ | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber, | ||
| assignees: selectedAssignees | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.