mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-11-09 16:18:34 +01:00
ci/github-script/prepare: init from actions/get-merge-commit
This just moves the code over to ci/github-script to make it easy to test and iterate on locally. The name `prepare` is chosen, because the script will be extended with the other steps from "PR / prepare" next.
This commit is contained in:
parent
2087f1be30
commit
c787c66de6
49
.github/actions/get-merge-commit/action.yml
vendored
49
.github/actions/get-merge-commit/action.yml
vendored
|
|
@ -35,50 +35,11 @@ runs:
|
|||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
|
||||
with:
|
||||
script: |
|
||||
for (const retryInterval of [5, 10, 20, 40, 80]) {
|
||||
console.log("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
|
||||
})).data
|
||||
|
||||
if (prInfo.state != 'open') throw new Error ("PR is not open anymore.")
|
||||
|
||||
if (prInfo.mergeable == null) {
|
||||
console.log(`GitHub is still computing whether this PR can be merged, waiting ${retryInterval} seconds before trying again...`)
|
||||
await new Promise(resolve => setTimeout(resolve, retryInterval * 1000))
|
||||
continue
|
||||
}
|
||||
|
||||
let mergedSha, targetSha
|
||||
|
||||
if (prInfo.mergeable) {
|
||||
console.log("The PR can be merged.")
|
||||
|
||||
mergedSha = prInfo.merge_commit_sha
|
||||
targetSha = (await github.rest.repos.getCommit({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
ref: prInfo.merge_commit_sha
|
||||
})).data.parents[0].sha
|
||||
} else {
|
||||
console.log("The PR has a merge conflict.")
|
||||
|
||||
mergedSha = prInfo.head.sha
|
||||
targetSha = (await github.rest.repos.compareCommitsWithBasehead({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
basehead: `${prInfo.base.sha}...${prInfo.head.sha}`
|
||||
})).data.merge_base_commit.sha
|
||||
}
|
||||
|
||||
console.log(`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`)
|
||||
core.setOutput('mergedSha', mergedSha)
|
||||
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.")
|
||||
require('./ci/github-script/prepare.js')({
|
||||
github,
|
||||
context,
|
||||
core,
|
||||
})
|
||||
|
||||
- if: inputs.merged-as-untrusted && (inputs.mergedSha || steps.commits.outputs.mergedSha)
|
||||
# Would be great to do the checkouts in git worktrees of the existing spare checkout instead,
|
||||
|
|
|
|||
4
.github/workflows/codeowners-v2.yml
vendored
4
.github/workflows/codeowners-v2.yml
vendored
|
|
@ -53,7 +53,9 @@ jobs:
|
|||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
sparse-checkout: .github/actions
|
||||
sparse-checkout: |
|
||||
.github/actions
|
||||
ci/github-script
|
||||
- name: Check if the PR can be merged and checkout the merge and target commits
|
||||
uses: ./.github/actions/get-merge-commit
|
||||
with:
|
||||
|
|
|
|||
1
.github/workflows/pr.yml
vendored
1
.github/workflows/pr.yml
vendored
|
|
@ -34,6 +34,7 @@ jobs:
|
|||
with:
|
||||
sparse-checkout: |
|
||||
.github/actions
|
||||
ci/github-script
|
||||
ci/supportedBranches.js
|
||||
ci/supportedSystems.json
|
||||
- name: Check if the PR can be merged and get the test merge commit
|
||||
|
|
|
|||
46
ci/github-script/prepare.js
Normal file
46
ci/github-script/prepare.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
module.exports = async function ({ github, context, core }) {
|
||||
for (const retryInterval of [5, 10, 20, 40, 80]) {
|
||||
console.log("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
|
||||
})).data
|
||||
|
||||
if (prInfo.state != 'open') throw new Error ("PR is not open anymore.")
|
||||
|
||||
if (prInfo.mergeable == null) {
|
||||
console.log(`GitHub is still computing whether this PR can be merged, waiting ${retryInterval} seconds before trying again...`)
|
||||
await new Promise(resolve => setTimeout(resolve, retryInterval * 1000))
|
||||
continue
|
||||
}
|
||||
|
||||
let mergedSha, targetSha
|
||||
|
||||
if (prInfo.mergeable) {
|
||||
console.log("The PR can be merged.")
|
||||
|
||||
mergedSha = prInfo.merge_commit_sha
|
||||
targetSha = (await github.rest.repos.getCommit({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
ref: prInfo.merge_commit_sha
|
||||
})).data.parents[0].sha
|
||||
} else {
|
||||
console.log("The PR has a merge conflict.")
|
||||
|
||||
mergedSha = prInfo.head.sha
|
||||
targetSha = (await github.rest.repos.compareCommitsWithBasehead({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
basehead: `${prInfo.base.sha}...${prInfo.head.sha}`
|
||||
})).data.merge_base_commit.sha
|
||||
}
|
||||
|
||||
console.log(`Checking the commits:\nmerged:${mergedSha}\ntarget:${targetSha}`)
|
||||
core.setOutput('mergedSha', mergedSha)
|
||||
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.")
|
||||
}
|
||||
|
|
@ -39,6 +39,17 @@ async function run(action, owner, repo, pull_number, dry = true) {
|
|||
})
|
||||
}
|
||||
|
||||
program
|
||||
.command('prepare')
|
||||
.description('Prepare relevant information of a pull request.')
|
||||
.argument('<owner>', 'Owner of the GitHub repository to check (Example: NixOS)')
|
||||
.argument('<repo>', 'Name of the GitHub repository to check (Example: nixpkgs)')
|
||||
.argument('<pr>', 'Number of the Pull Request to check')
|
||||
.action(async (owner, repo, pr) => {
|
||||
const prepare = (await import('./prepare.js')).default
|
||||
run(prepare, owner, repo, pr)
|
||||
})
|
||||
|
||||
program
|
||||
.command('commits')
|
||||
.description('Check commit structure of a pull request.')
|
||||
|
|
|
|||
Loading…
Reference in a new issue