Skip to content

fix: dependabot run only when checks pass #30

fix: dependabot run only when checks pass

fix: dependabot run only when checks pass #30

name: Dependabot auto-merge
on:
pull_request:
check_suite:
types: [completed]
permissions:
pull-requests: write
contents: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
steps:
- name: Dependabot metadata
id: dependabot-metadata
uses: dependabot/fetch-metadata@v2
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
- name: Approve and /shipit Dependabot PRs for patch and minor versions
if: ${{ steps.dependabot-metadata.outputs.update-type != 'version-update:semver-major' }}
uses: actions/github-script@v7
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
script: |
const getPullRequestIdQuery = `query GetPullRequestId($owner: String!, $repo: String!, $pullRequestNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pullRequestNumber) {
id
}
}
}`
const repoInfo = {
owner: context.repo.owner,
repo: context.repo.repo,
pullRequestNumber: context.issue.number,
}
const response = await github.graphql(getPullRequestIdQuery, repoInfo)
await github.rest.pulls.createReview({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
event: 'APPROVE',
})
const enableAutoMergeQuery = `mutation ($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) {
enablePullRequestAutoMerge(input: {
pullRequestId: $pullRequestId,
mergeMethod: $mergeMethod
}) {
pullRequest {
autoMergeRequest {
enabledAt
enabledBy {
login
}
}
}
}
}`
const data = {
pullRequestId: response.repository.pullRequest.id,
mergeMethod: 'SQUASH',
}
const checkStatusQuery = `query($owner: String!, $repo: String!, $pullRequestNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pullRequestNumber) {
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
state
}
}
}
}
}
}
}`
// Check if status checks are successful
const statusResponse = await github.graphql(checkStatusQuery, repoInfo)
const checkState = statusResponse.repository.pullRequest.commits.nodes[0]?.commit?.statusCheckRollup?.state
if (checkState === 'FAILURE' || checkState === 'ERROR') {
console.log('Status checks failed. Current state:', checkState)
core.setFailed('Status checks must pass before enabling auto-merge')
return
}
try {
await github.graphql(enableAutoMergeQuery, data)
} catch (error) {
console.log('Failed to enable auto-merge:', error.message)
core.setFailed(error.message)
}