Skip to content

Bugfix/hooks yaml baseline #29

Bugfix/hooks yaml baseline

Bugfix/hooks yaml baseline #29

Workflow file for this run

name: "Dependabot Safe Auto-merge for Patch and Minor Updates"
on:
pull_request_target:
branches:
- main
types:
- opened
- synchronize
- reopened
permissions:
contents: write
pull-requests: write
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
jobs:
auto-merge:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
steps:
- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Decide if this PR is eligible
id: decision
env:
PACKAGE_ECOSYSTEM: ${{ steps.metadata.outputs.package-ecosystem }}
UPDATE_TYPE: ${{ steps.metadata.outputs.update-type }}
run: |
should_automerge=false
if [[ "$PACKAGE_ECOSYSTEM" == "github-actions" ]]; then
if [[ "$UPDATE_TYPE" == "version-update:semver-patch" || "$UPDATE_TYPE" == "version-update:semver-minor" ]]; then
should_automerge=true
fi
fi
if [[ "$PACKAGE_ECOSYSTEM" == "pip" ]]; then
if [[ "$UPDATE_TYPE" == "version-update:semver-patch" ]]; then
should_automerge=true
fi
fi
echo "should_automerge=$should_automerge" >> "$GITHUB_OUTPUT"
echo "package_ecosystem=$PACKAGE_ECOSYSTEM" >> "$GITHUB_OUTPUT"
echo "update_type=$UPDATE_TYPE" >> "$GITHUB_OUTPUT"
- name: Approve eligible PR
if: steps.decision.outputs.should_automerge == 'true'
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
event: "APPROVE",
body: "Auto-approved for safe Dependabot update policy."
})
} catch (error) {
const msg = String(error.message || "")
if (
error.status === 422 &&
msg.includes("GitHub Actions is not permitted to approve pull requests")
) {
core.warning(
"Skipping auto-approve: repository setting blocks workflow approvals. " +
"Enable 'Allow GitHub Actions to create and approve pull requests' to restore this step."
)
} else {
core.setFailed("Could not auto-approve eligible Dependabot PR. " + msg)
}
}
- name: Update branch for eligible PR
if: steps.decision.outputs.should_automerge == 'true'
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Intentional strategy:
// - Always request an update for eligible PRs instead of pre-checking merge state.
// - GitHub may report transient/unknown merge state during event handling.
// - updateBranch is idempotent; if already current, GitHub returns a benign 422/no-op.
// This keeps automerge flow deterministic without relying on flaky preconditions.
try {
await github.rest.pulls.updateBranch({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number
})
core.info("Requested branch update from base branch.")
} catch (error) {
const msg = String(error.message || "")
if (error.status === 422 || msg.includes("not behind")) {
// Expected no-op path when branch is already up to date.
core.info("Branch already up to date; continuing.")
} else {
core.setFailed("Could not update branch before automerge. " + msg)
}
}
- name: Enable auto-merge for eligible PR
if: steps.decision.outputs.should_automerge == 'true'
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.graphql(
`mutation($pullRequestId: ID!) {
enablePullRequestAutoMerge(input: {pullRequestId: $pullRequestId, mergeMethod: SQUASH}) {
pullRequest { number }
}
}`,
{ pullRequestId: context.payload.pull_request.node_id }
)
} catch (error) {
core.setFailed(
"Could not enable auto-merge. Ensure repository auto-merge is enabled and branch protections are satisfied.\n" +
error.message
)
}
- name: Log skipped PR
if: steps.decision.outputs.should_automerge != 'true'
run: |
echo "Automerge skipped by policy."
echo "ecosystem=${{ steps.decision.outputs.package_ecosystem }}"
echo "update_type=${{ steps.decision.outputs.update_type }}"