Skip to content

Dependabot auto-merge #1457

Dependabot auto-merge

Dependabot auto-merge #1457

name: Dependabot auto-merge
on:
pull_request_target:
types: [opened, reopened, synchronize, ready_for_review]
workflow_dispatch:
schedule:
- cron: '17 * * * *'
permissions:
contents: write
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]' && github.event.pull_request.draft == false }}
steps:
- name: Enable auto-merge for Dependabot PRs
uses: peter-evans/enable-pull-request-automerge@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
pull-request-number: ${{ github.event.pull_request.number }}
merge-method: merge
dependabot-backfill:
runs-on: ubuntu-latest
if: ${{ github.event_name != 'pull_request_target' }}
steps:
- name: Enable auto-merge on all open Dependabot PRs
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {owner, repo} = context.repo;
const pulls = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: 'open',
per_page: 100
});
const dependabotPRs = pulls.filter((pr) => pr.user?.login === 'dependabot[bot]' && !pr.draft);
core.info(`Found ${dependabotPRs.length} open Dependabot PR(s).`);
for (const pr of dependabotPRs) {
try {
await github.graphql(
`mutation EnableAutoMerge($pullRequestId: ID!) {
enablePullRequestAutoMerge(input: {pullRequestId: $pullRequestId, mergeMethod: MERGE}) {
pullRequest { number }
}
}`,
{ pullRequestId: pr.node_id }
);
core.info(`Enabled auto-merge for #${pr.number}`);
} catch (error) {
core.warning(`Could not enable auto-merge for #${pr.number}: ${error.message}`);
}
}