|
| 1 | +name: Auto Assign Owner |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + pull_request_target: |
| 7 | + types: [opened] |
| 8 | + |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + pull-requests: write |
| 12 | + contents: read |
| 13 | + |
| 14 | +jobs: |
| 15 | + check-and-assign: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Check collaborator count and assign owner |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const owner = context.repo.owner; |
| 23 | + const repo = context.repo.repo; |
| 24 | +
|
| 25 | + // Get collaborators with push or admin access |
| 26 | + const { data: collaborators } = await github.rest.repos.listCollaborators({ |
| 27 | + owner, |
| 28 | + repo, |
| 29 | + permission: 'push' |
| 30 | + }); |
| 31 | +
|
| 32 | + // Filter to only users with push or admin (not just read) |
| 33 | + const pushCollaborators = collaborators.filter(c => |
| 34 | + c.permissions?.push || c.permissions?.admin |
| 35 | + ); |
| 36 | +
|
| 37 | + console.log(`Found ${pushCollaborators.length} collaborators with push access`); |
| 38 | +
|
| 39 | + // Only auto-assign if there's 1 or fewer collaborators with push access |
| 40 | + if (pushCollaborators.length > 1) { |
| 41 | + console.log('Multiple collaborators found, skipping auto-assign'); |
| 42 | + return; |
| 43 | + } |
| 44 | +
|
| 45 | + // Determine the assignee (repo owner) |
| 46 | + const assignee = owner; |
| 47 | +
|
| 48 | + if (context.eventName === 'issues') { |
| 49 | + const issue = context.payload.issue; |
| 50 | +
|
| 51 | + // Skip if already assigned |
| 52 | + if (issue.assignees && issue.assignees.length > 0) { |
| 53 | + console.log('Issue already has assignees, skipping'); |
| 54 | + return; |
| 55 | + } |
| 56 | +
|
| 57 | + // Skip if author is a bot |
| 58 | + if (issue.user.type === 'Bot') { |
| 59 | + console.log('Issue author is a bot, skipping'); |
| 60 | + return; |
| 61 | + } |
| 62 | +
|
| 63 | + await github.rest.issues.addAssignees({ |
| 64 | + owner, |
| 65 | + repo, |
| 66 | + issue_number: issue.number, |
| 67 | + assignees: [assignee] |
| 68 | + }); |
| 69 | +
|
| 70 | + console.log(`Assigned issue #${issue.number} to ${assignee}`); |
| 71 | +
|
| 72 | + } else if (context.eventName === 'pull_request_target') { |
| 73 | + const pr = context.payload.pull_request; |
| 74 | +
|
| 75 | + // Skip if already assigned |
| 76 | + if (pr.assignees && pr.assignees.length > 0) { |
| 77 | + console.log('PR already has assignees, skipping'); |
| 78 | + return; |
| 79 | + } |
| 80 | +
|
| 81 | + // Skip if author is a bot |
| 82 | + if (pr.user.type === 'Bot') { |
| 83 | + console.log('PR author is a bot, skipping'); |
| 84 | + return; |
| 85 | + } |
| 86 | +
|
| 87 | + await github.rest.issues.addAssignees({ |
| 88 | + owner, |
| 89 | + repo, |
| 90 | + issue_number: pr.number, |
| 91 | + assignees: [assignee] |
| 92 | + }); |
| 93 | +
|
| 94 | + console.log(`Assigned PR #${pr.number} to ${assignee}`); |
| 95 | + } |
0 commit comments