ci/github-script/prepare: refactor
Using core.info instead of console.log and simplifying the arguments for API calls a bit.
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
module.exports = async ({ github, context, core }) => {
|
||||
module.exports = async function ({ github, context, core }) {
|
||||
const pull_number = context.payload.pull_request.number
|
||||
|
||||
for (const retryInterval of [5, 10, 20, 40, 80]) {
|
||||
console.log('Checking whether the pull request can be merged...')
|
||||
core.info('Checking whether the pull request can be merged...')
|
||||
const prInfo = (
|
||||
await github.rest.pulls.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: context.payload.pull_request.number,
|
||||
...context.repo,
|
||||
pull_number,
|
||||
})
|
||||
).data
|
||||
|
||||
if (prInfo.state !== 'open') throw new Error('PR is not open anymore.')
|
||||
|
||||
if (prInfo.mergeable == null) {
|
||||
console.log(
|
||||
core.info(
|
||||
`GitHub is still computing whether this PR can be merged, waiting ${retryInterval} seconds before trying again...`,
|
||||
)
|
||||
await new Promise((resolve) => setTimeout(resolve, retryInterval * 1000))
|
||||
@@ -22,31 +23,29 @@ module.exports = async ({ github, context, core }) => {
|
||||
let mergedSha, targetSha
|
||||
|
||||
if (prInfo.mergeable) {
|
||||
console.log('The PR can be merged.')
|
||||
core.info('The PR can be merged.')
|
||||
|
||||
mergedSha = prInfo.merge_commit_sha
|
||||
targetSha = (
|
||||
await github.rest.repos.getCommit({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
...context.repo,
|
||||
ref: prInfo.merge_commit_sha,
|
||||
})
|
||||
).data.parents[0].sha
|
||||
} else {
|
||||
console.log('The PR has a merge conflict.')
|
||||
core.warning('The PR has a merge conflict.')
|
||||
|
||||
mergedSha = prInfo.head.sha
|
||||
targetSha = (
|
||||
await github.rest.repos.compareCommitsWithBasehead({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
...context.repo,
|
||||
basehead: `${prInfo.base.sha}...${prInfo.head.sha}`,
|
||||
})
|
||||
).data.merge_base_commit.sha
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`,
|
||||
core.info(
|
||||
`Checking the commits:\nmerged: ${mergedSha}\ntarget: ${targetSha}`,
|
||||
)
|
||||
core.setOutput('mergedSha', mergedSha)
|
||||
core.setOutput('targetSha', targetSha)
|
||||
|
||||
Reference in New Issue
Block a user