diff --git a/.github/workflows/auto-update-base.yaml b/.github/workflows/auto-update-base.yaml new file mode 100644 index 000000000..da32aa55a --- /dev/null +++ b/.github/workflows/auto-update-base.yaml @@ -0,0 +1,60 @@ +name: Auto-Update PR Base + +on: + push: + branches: + - main + +jobs: + auto-update-base: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v7 + with: + script: | + try { + const { data: pulls } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: "open", + }); + + for (const pull of pulls) { + const hasAutoUpdateLabel = pull.labels.some(label => label.name === "auto-update-base"); + + if (hasAutoUpdateLabel) { + try { + const pr = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pull.number, + }); + + if (pr.data.mergeable_state === 'dirty') { + console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Skipping update due to merge conflict.`); + continue; + } + if (pr.data.mergeable_state !== 'behind') { + console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Base branch is up-to-date.`); + continue; + } + + console.log(`[UPDATE] PR #${pull.number}: "${pull.title}" - Updating base branch...`); + try { + await github.rest.pulls.updateBranch({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pull.number, + }); + console.log(`[SUCCESS] PR #${pull.number}: "${pull.title}" - Base branch updated.`); + } catch (updateError) { + console.error(`[ERROR] PR #${pull.number}: Failed to update base branch - ${updateError.message}`); + } + } catch (diffError) { + console.error(`[ERROR] PR #${pull.number}: Failed to compare commits - ${diffError.message}`); + } + } + } + } catch (error) { + console.error(`[ERROR] Failed to list pull requests - ${error.message}`); + }