name: 🏷️ Auto Assign PR on: workflow_call: pull_request: types: [opened, reopened, synchronize, ready_for_review] concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true jobs: assign_pr_creator_or_pusher: name: Assign PR Creator or Pusher runs-on: ubuntu-latest timeout-minutes: 1 env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} permissions: pull-requests: write steps: - name: Determine user to assign id: get_assignee_user run: | ASSIGN_USER="" EVENT_ACTOR="" if [[ "${{ github.event.action }}" == "synchronize" ]]; then EVENT_ACTOR="${{ github.event.sender.login }}" else EVENT_ACTOR="${{ github.event.pull_request.user.login }}" fi KNOWN_BOTS=("dependabot[bot]" "github-actions[bot]" "github-copilot[bot]" "copilot-bot[bot]") IS_KNOWN_BOT=false for BOT in "${KNOWN_BOTS[@]}"; do if [[ "$EVENT_ACTOR" == "$BOT" ]]; then IS_KNOWN_BOT=true break fi done if $IS_KNOWN_BOT || [[ "$EVENT_ACTOR" =~ \[bot\]$ ]]; then echo "Skipping bot user: $EVENT_ACTOR" ASSIGN_USER="" else ASSIGN_USER="$EVENT_ACTOR" echo "Determined user: $ASSIGN_USER" fi echo "assigned_user=$ASSIGN_USER" >> "$GITHUB_OUTPUT" - name: Assign PR to determined user if: steps.get_assignee_user.outputs.assigned_user != '' run: | PR_NUMBER="${{ github.event.pull_request.number }}" ASSIGN_USER_LOGIN="${{ steps.get_assignee_user.outputs.assigned_user }}" echo "Attempting to assign PR #$PR_NUMBER to $ASSIGN_USER_LOGIN" # Notes: # - If the user is already assigned, `gh pr edit` succeeds without error. # - External-collaborator PRs may fail outright (token lacks write on that user) — we # log and continue rather than failing the workflow. # - GitHub can also silently ignore assignees without push access while still exiting 0, # so we re-read the PR's assignees after the edit and confirm the user actually landed # before logging success. if gh pr edit "$PR_NUMBER" --add-assignee "$ASSIGN_USER_LOGIN" --repo "${{ github.repository }}"; then CURRENT_ASSIGNEES="$(gh pr view "$PR_NUMBER" --repo "${{ github.repository }}" --json assignees --jq '.assignees[].login' 2>/dev/null || true)" if grep -Fxq "$ASSIGN_USER_LOGIN" <<<"$CURRENT_ASSIGNEES"; then echo "Successfully assigned PR #$PR_NUMBER to $ASSIGN_USER_LOGIN" else echo "⚠ gh pr edit returned success but $ASSIGN_USER_LOGIN is not in the assignees list (likely silently ignored by GitHub — no push access)" fi else echo "❌ Failed to assign PR #$PR_NUMBER to $ASSIGN_USER_LOGIN, likely an external collaborator" fi