ci: fix pre-commit automerge #2
Workflow file for this run
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
| --- | |
| name: Delete branch on PR close (bots only) | |
| on: | |
| pull_request: | |
| types: [closed] | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: delete-branch-${{ github.repository }}-${{ github.event.pull_request.head.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| delete-branch: | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event.pull_request.head.repo.full_name == github.repository && | |
| github.event.pull_request.merged == false | |
| steps: | |
| - name: detect bot author | |
| id: bot-author-check | |
| uses: actions/github-script@v8 | |
| with: | |
| result-encoding: string | |
| script: | | |
| let user; | |
| if (context.eventName === 'issues') { | |
| user = context.payload.issue?.user; | |
| } else if (context.eventName === 'pull_request') { | |
| user = context.payload.pull_request?.user; | |
| } | |
| if ( | |
| user && | |
| (user.type === 'Bot' || | |
| (typeof user.login === 'string' && user.login.endsWith('[bot]'))) | |
| ) { | |
| console.log('Bot user detected:', user.login); | |
| core.setOutput('result', 'true'); | |
| return; | |
| } | |
| return 'false'; | |
| - name: Delete PR head branch | |
| if: steps.bot-author-check.outputs.result == 'true' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| BRANCH: ${{ github.event.pull_request.head.ref }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| [[ "$BRANCH" == "$(gh api "repos/${REPO}" --jq .default_branch)" ]] && exit 0 | |
| # Exit if branch is protected | |
| gh api "repos/${REPO}/branches/${BRANCH}" \ | |
| --jq '.protected' 2>/dev/null | grep -qx true && exit 0 | |
| # Delete ref if it still exists | |
| gh api -X DELETE "repos/${REPO}/git/refs/heads/${BRANCH}" >/dev/null 2>&1 || true |