|
| 1 | +name: PR Auto Assignment and Labeling |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited] |
| 6 | + |
| 7 | +jobs: |
| 8 | + auto-assign-and-label: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + steps: |
| 14 | + - name: Get branch name |
| 15 | + id: branch |
| 16 | + run: echo "branch_name=${GITHUB_HEAD_REF}" >> $GITHUB_OUTPUT |
| 17 | + |
| 18 | + - name: Determine label based on branch prefix or author |
| 19 | + id: label |
| 20 | + run: | |
| 21 | + # Check if it's a Dependabot PR |
| 22 | + if [[ "${{ github.actor }}" == "dependabot[bot]" ]]; then |
| 23 | + echo "label=dependencies" >> $GITHUB_OUTPUT |
| 24 | + # Check branch prefixes |
| 25 | + elif [[ "${{ github.head_ref }}" == feature/* ]] || [[ "${{ github.head_ref }}" == feat/* ]]; then |
| 26 | + echo "label=enhancement" >> $GITHUB_OUTPUT |
| 27 | + elif [[ "${{ github.head_ref }}" == fix/* ]]; then |
| 28 | + echo "label=fix" >> $GITHUB_OUTPUT |
| 29 | + elif [[ "${{ github.head_ref }}" == docs/* ]]; then |
| 30 | + echo "label=documentation" >> $GITHUB_OUTPUT |
| 31 | + else |
| 32 | + echo "label=" >> $GITHUB_OUTPUT |
| 33 | + fi |
| 34 | +
|
| 35 | + - name: Add label to PR |
| 36 | + if: steps.label.outputs.label != '' |
| 37 | + uses: actions/github-script@v7 |
| 38 | + with: |
| 39 | + script: | |
| 40 | + github.rest.issues.addLabels({ |
| 41 | + issue_number: context.issue.number, |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + labels: ['${{ steps.label.outputs.label }}'] |
| 45 | + }) |
| 46 | +
|
| 47 | + - name: Assign reviewer and assignee |
| 48 | + uses: actions/github-script@v7 |
| 49 | + with: |
| 50 | + script: | |
| 51 | + const assignee = 'baspa'; |
| 52 | + |
| 53 | + // Always assign as assignee (you can be assigned to your own PR) |
| 54 | + github.rest.issues.addAssignees({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + issue_number: context.issue.number, |
| 58 | + assignees: [assignee] |
| 59 | + }); |
| 60 | + console.log(`Assigned ${assignee} as assignee`); |
| 61 | + |
| 62 | + // Only assign reviewer if they are not the PR author |
| 63 | + if (context.payload.pull_request.user.login !== assignee) { |
| 64 | + github.rest.pulls.requestReviewers({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + pull_number: context.issue.number, |
| 68 | + reviewers: [assignee] |
| 69 | + }); |
| 70 | + console.log(`Assigned ${assignee} as reviewer`); |
| 71 | + } else { |
| 72 | + console.log(`Skipping reviewer assignment - ${assignee} is the PR author`); |
| 73 | + } |
0 commit comments