actions/get-merge-conflict: refactor

Using core.setOutput is much nicer than having to parse the json
"result" on the outside. This also avoids some very odd errors, when the
result can, for unknown reasons, *not* be parsed as JSON later on.

Also avoiding a bit of duplication between the "if mergeable" branches.
This commit is contained in:
Wolfgang Walther 2025-05-25 13:25:24 +02:00
parent cd9a22d753
commit 539e8d4f66
No known key found for this signature in database
GPG key ID: B39893FA5F65CAE1

View file

@ -5,10 +5,10 @@ description: 'Checks whether the Pull Request is mergeable and returns two commi
outputs: outputs:
mergedSha: mergedSha:
description: "The merge commit SHA" description: "The merge commit SHA"
value: ${{ fromJSON(steps.merged.outputs.result).mergedSha }} value: ${{ steps.merged.outputs.mergedSha }}
targetSha: targetSha:
description: "The target commit SHA" description: "The target commit SHA"
value: ${{ fromJSON(steps.merged.outputs.result).targetSha }} value: ${{ steps.merged.outputs.targetSha }}
runs: runs:
using: composite using: composite
@ -17,7 +17,7 @@ runs:
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with: with:
script: | script: |
if (context.eventName == 'push') return { mergedSha: context.sha } if (context.eventName == 'push') return core.setOutput('mergedSha', context.sha)
for (const retryInterval of [5, 10, 20, 40, 80]) { for (const retryInterval of [5, 10, 20, 40, 80]) {
console.log("Checking whether the pull request can be merged...") console.log("Checking whether the pull request can be merged...")
@ -35,32 +35,31 @@ runs:
continue continue
} }
let mergedSha, targetSha
if (prInfo.mergeable) { if (prInfo.mergeable) {
console.log("The PR can be merged.") console.log("The PR can be merged.")
const mergedSha = prInfo.merge_commit_sha mergedSha = prInfo.merge_commit_sha
const targetSha = (await github.rest.repos.getCommit({ targetSha = (await github.rest.repos.getCommit({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
ref: prInfo.merge_commit_sha ref: prInfo.merge_commit_sha
})).data.parents[0].sha })).data.parents[0].sha
console.log(`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`)
return { mergedSha, targetSha }
} else { } else {
console.log("The PR has a merge conflict.") console.log("The PR has a merge conflict.")
const mergedSha = prInfo.head.sha mergedSha = prInfo.head.sha
const targetSha = (await github.rest.repos.compareCommitsWithBasehead({ targetSha = (await github.rest.repos.compareCommitsWithBasehead({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
basehead: `${prInfo.base.sha}...${prInfo.head.sha}` basehead: `${prInfo.base.sha}...${prInfo.head.sha}`
})).data.merge_base_commit.sha })).data.merge_base_commit.sha
}
console.log(`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`) console.log(`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`)
core.setOutput('mergedSha', mergedSha)
return { mergedSha, targetSha } core.setOutput('targetSha', targetSha)
} return
} }
throw new Error("Not retrying anymore. It's likely that GitHub is having internal issues: check https://www.githubstatus.com.") throw new Error("Not retrying anymore. It's likely that GitHub is having internal issues: check https://www.githubstatus.com.")