From ac793faf29fac7b846c8533bf2744063b85f6ef2 Mon Sep 17 00:00:00 2001 From: JJGadgets Date: Wed, 23 Apr 2025 19:50:01 +0800 Subject: [PATCH] feat(GHA): Renovate auto rebase after PR automerge enabled --- .github/workflows/renovate-rebase.yaml | 80 +++++--------------------- 1 file changed, 14 insertions(+), 66 deletions(-) diff --git a/.github/workflows/renovate-rebase.yaml b/.github/workflows/renovate-rebase.yaml index ddbc2a3a..849bbf4d 100644 --- a/.github/workflows/renovate-rebase.yaml +++ b/.github/workflows/renovate-rebase.yaml @@ -6,13 +6,9 @@ name: "Renovate Rebase For Automerge" on: workflow_dispatch: pull_request: - types: ["auto_merge_enabled"] + types: ["auto_merge_enabled", "closed"] branches: ["main"] -# concurrency: -# group: "renovate-rebase-automerge" -# cancel-in-progress: false - jobs: rebase-after: name: Rebase @@ -27,69 +23,21 @@ jobs: private-key: "${{ secrets.BOT_JWT_PRIVATE_KEY }}" # $BOT_JWT_PRIVATE_KEY is generated in GitHub App main settings page, uses the X.509 private key format owner: "${{ github.repository_owner }}" - - name: Update branch + - name: Update branch if merged + if: github.event.pull_request.merged == true env: GITHUB_TOKEN: "${{ steps.oauth-token.outputs.token }}" shell: bash run: | - rebase () { - gh api --method PUT /repos/${{ github.event.pull_request.base.repo.full_name }}/pulls/${{ github.event.pull_request.number }}/update-branch --field expected_head_sha=${{ github.event.pull_request.head.sha }} - } - getRebaseRuns () { - echo "\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=renovate-rebase.yaml --status in_progress --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=renovate-rebase.yaml --status queued --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=renovate-rebase.yaml --status waiting --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=renovate-rebase.yaml --status requested --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=renovate-rebase.yaml --status pending --json headBranch --jq '.[].headBranch')\ - " | grep '\S' || echo -n '' - } - getOtherRuns () { - echo "\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=kube-flux-diff.yaml --status in_progress --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=kube-flux-diff.yaml --status queued --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=kube-flux-diff.yaml --status waiting --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=kube-flux-diff.yaml --status requested --json headBranch --jq '.[].headBranch')\ - $(gh run list --repo ${{ github.event.pull_request.base.repo.full_name }} --workflow=kube-flux-diff.yaml --status pending --json headBranch --jq '.[].headBranch')\ - " | grep '\S' || echo -n '' - } - getOtherRunsCount () { - echo -n "$(getOtherRuns | wc -l)" - } - rebaseRuns="$(getRebaseRuns)" - getOtherRuns - getOtherRunsCount - while true; do - if [[ "${rebaseRuns}" == "1" ]]; then - echo "only run, rebase early and leave"; - rebase; - break; - fi - getOtherRuns - runsCount="$(getOtherRunsCount)" - echo "run 1 $runsCount" - if [[ "${runsCount}" == "0" ]]; then # 1 run because current - sleep 10 - getOtherRuns - runsCount2="$(getOtherRunsCount)" - echo "run 2 $runsCount2" - if [[ "${runsCount2}" == "0" ]]; then - sleep 10 - getOtherRuns - runsCount3="$(getOtherRunsCount)" - echo "run 3 $runsCount3" - if [[ "${runsCount3}" == "0" ]]; then - echo "pass all 3, run"; - rebase; - break; - else - echo "not yet at $(date -I), pass 2" - fi - else - echo "not yet at $(date -I), pass 1" - fi - else - echo "not yet at $(date -I), all fail" - fi - sleep 1 + for i in $(gh pr list --repo ${{ github.event.pull_request.base.repo.full_name }} --author "tinfoild[bot]" --json title,number,autoMergeRequest,headRefName | jq '.[] | select((.autoMergeRequest != null) and (.headRefName | startswith("renovate/"))) | .number'); do + sha=$(gh pr view ${i} --json headRefOid --jq '.headRefOid' --repo ${{ github.event.pull_request.base.repo.full_name }}) + gh api --method PUT /repos/${{ github.event.pull_request.base.repo.full_name }}/pulls/${i}/update-branch --field expected_head_sha=${sha} done + + - name: Update branch if automerge enabled + if: github.event.action == auto_merge_enabled + env: + GITHUB_TOKEN: "${{ steps.oauth-token.outputs.token }}" + shell: bash + run: | + gh api --method PUT /repos/${{ github.event.pull_request.base.repo.full_name }}/pulls/${{ github.event.pull_request.number }}/update-branch --field expected_head_sha=${{ github.event.pull_request.head.sha }}