Skip to content

ci: enable auto-merging of dependabot PRs #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Auto-Merge Dependabot PRs (only for changes to minor or patch version but not major version)

on:
pull_request:
types:
- labeled # Triggered when a label is added to a pull request

jobs:
auto-merge:
if: github.actor == 'dependabot[bot]' # Ensures this only runs for Dependabot PRs
runs-on: ubuntu-latest
steps:
- name: Check if the PR has the 'dependencies' label
uses: actions/github-script@v6
id: check-label
with:
script: |
const { context } = require('@actions/github');
const { labels } = context.payload.pull_request;

// When dependabot creates PRs, it adds 'dependencies' as a label to the PRs.
// Here we check to see if 'dependencies' label is present
const hasDependenciesLabel = labels.some(label => label.name === 'dependencies');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependencies label gets added to both the roll-up minor/patch PRs and the major version updates. Is there further gating to ensure that this only happens for minor/patch roll-up PRs?
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From these docs, it looks like we could get more granular based on metadata that comes with the PRs. It also looks like we could do it a bit more streamlined if we leverage the gh CLI as part of the process. Might be worth a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khawkins Oh good point.... I forgot to limit it to minor & patch only. ChatGPT is suggesting to simply look at the title of PR to determine what type of update it is. Since we've already enabled grouping of dependabot PRs for minor & patch, we could simply test to see whether the PR subject includes minor-and-patch group and if so then proceed with auto-merge. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think checking for minor-and-patch group in the subject could be sufficient. It looks like there's a lot of interesting metadata that comes back from the https://github.com/dependabot/fetch-metadata GHA, so check out the README there and see if that changes your mind about your approach. But otherwise, I think checking the subject should be fine.


if (!hasDependenciesLabel) {
core.setFailed("The 'dependencies' label is missing.");
}

- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2

- name: Check if update is a minor or patch group or individual minor/patch update
run: |
if [[ "${{ steps.metadata.outputs.update-type }}" != "minor" && \
"${{ steps.metadata.outputs.update-type }}" != "patch" && \
"${{ github.event.pull_request.title }}" != *"minor-and-patch group"* ]]; then
echo "This PR is not a minor or patch update. Auto-merge aborted."
exit 1
fi

- name: Check if CI passed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding that flow as 'on: pull_request: types: - labeled' would be triggered when pr is added a label. I guess dependabot creates the pr with label in it, so would 'Check if CI passed' always be unsuccessful? might having a timing issue here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I understood is that auto-merge job will get cancelled if the label dependencies is not on the PR. If the label is there then we move to the next phase which is verifying the PR check statuses. If all of the checks have passed too, only then we attempt to merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there will be timing issue because these are all defined as workflow steps and afaik the steps are executed in series not parallel.

uses: actions/github-script@v6
with:
script: |
const { context, github } = require('@actions/github');
const { pull_request } = context.payload;

// Ensure that all other PR job statuses have passed (e.g. build, test, lint, etc)
const { data: statuses } = await github.rest.repos.getCombinedStatusForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: pull_request.head.sha,
});

const allStatusesSuccessful = statuses.statuses.every(status => status.state === 'success');

if (!allStatusesSuccessful) {
core.setFailed("Not all CI checks passed.");
}

- name: Auto-Merge PR
uses: actions/github-script@v6
with:
script: |
const { context, github } = require('@actions/github');
const { pull_request } = context.payload;

await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_request.number,
merge_method: "squash", // other options are "merge" or "rebase"
});
Loading