pull/8669/merge
parent
7fb22ab681
commit
9a0132e750
|
|
@ -5,17 +5,10 @@ interface PR {
|
||||||
title: string
|
title: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Repo {
|
interface RunResult {
|
||||||
nameWithOwner: string
|
exitCode: number
|
||||||
}
|
stdout: string
|
||||||
|
stderr: string
|
||||||
interface HeadRepo {
|
|
||||||
nameWithOwner: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PRHead {
|
|
||||||
headRefName: string
|
|
||||||
headRepository: HeadRepo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
|
@ -29,12 +22,6 @@ async function main() {
|
||||||
const prs: PR[] = JSON.parse(prsResult.stdout)
|
const prs: PR[] = JSON.parse(prsResult.stdout)
|
||||||
console.log(`Found ${prs.length} open contributor PRs`)
|
console.log(`Found ${prs.length} open contributor PRs`)
|
||||||
|
|
||||||
const repoResult = await $`gh repo view --json nameWithOwner`.nothrow()
|
|
||||||
if (repoResult.exitCode !== 0) {
|
|
||||||
throw new Error(`Failed to fetch repo info: ${repoResult.stderr}`)
|
|
||||||
}
|
|
||||||
const repo: Repo = JSON.parse(repoResult.stdout)
|
|
||||||
|
|
||||||
console.log("Fetching latest dev branch...")
|
console.log("Fetching latest dev branch...")
|
||||||
const fetchDev = await $`git fetch origin dev`.nothrow()
|
const fetchDev = await $`git fetch origin dev`.nothrow()
|
||||||
if (fetchDev.exitCode !== 0) {
|
if (fetchDev.exitCode !== 0) {
|
||||||
|
|
@ -53,53 +40,35 @@ async function main() {
|
||||||
for (const pr of prs) {
|
for (const pr of prs) {
|
||||||
console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
|
console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
|
||||||
|
|
||||||
const headResult = await $`gh pr view ${pr.number} --json headRefName,headRepository`.nothrow()
|
console.log(" Fetching PR head...")
|
||||||
if (headResult.exitCode !== 0) {
|
const fetch = await run(["git", "fetch", "origin", `pull/${pr.number}/head:pr/${pr.number}`])
|
||||||
console.log(` Failed to get head info`)
|
if (fetch.exitCode !== 0) {
|
||||||
skipped.push({ number: pr.number, reason: `Failed to get head info: ${headResult.stderr}` })
|
console.log(` Failed to fetch PR head: ${fetch.stderr}`)
|
||||||
continue
|
skipped.push({ number: pr.number, reason: `Fetch failed: ${fetch.stderr}` })
|
||||||
}
|
|
||||||
const head: PRHead = JSON.parse(headResult.stdout)
|
|
||||||
|
|
||||||
// Get the diff from GitHub compare API
|
|
||||||
console.log(` Getting diff...`)
|
|
||||||
const compare = `${repo.nameWithOwner}/compare/dev...${head.headRepository.nameWithOwner}:${head.headRefName}`
|
|
||||||
const diffResult = await $`gh api -H Accept:application/vnd.github.v3.diff repos/${compare}`.nothrow()
|
|
||||||
if (diffResult.exitCode !== 0) {
|
|
||||||
console.log(` Failed to get diff: ${diffResult.stderr}`)
|
|
||||||
console.log(` Compare: ${compare}`)
|
|
||||||
skipped.push({ number: pr.number, reason: `Failed to get diff: ${diffResult.stderr}` })
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!diffResult.stdout.trim()) {
|
console.log(" Merging...")
|
||||||
console.log(` No changes, skipping`)
|
const merge = await run(["git", "merge", "--no-commit", "--no-ff", `pr/${pr.number}`])
|
||||||
skipped.push({ number: pr.number, reason: "No changes" })
|
if (merge.exitCode !== 0) {
|
||||||
continue
|
console.log(" Failed to merge (conflicts)")
|
||||||
}
|
await $`git merge --abort`.nothrow()
|
||||||
|
|
||||||
// Try to apply the diff
|
|
||||||
console.log(` Applying...`)
|
|
||||||
const apply = await Bun.spawn(["git", "apply", "--3way"], {
|
|
||||||
stdin: new TextEncoder().encode(diffResult.stdout),
|
|
||||||
stdout: "pipe",
|
|
||||||
stderr: "pipe",
|
|
||||||
})
|
|
||||||
const applyExit = await apply.exited
|
|
||||||
const applyStderr = await Bun.readableStreamToText(apply.stderr)
|
|
||||||
|
|
||||||
if (applyExit !== 0) {
|
|
||||||
console.log(` Failed to apply (conflicts)`)
|
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
await $`git clean -fd`.nothrow()
|
await $`git clean -fd`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: "Has conflicts" })
|
skipped.push({ number: pr.number, reason: "Has conflicts" })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stage and commit
|
const mergeHead = await $`git rev-parse -q --verify MERGE_HEAD`.nothrow()
|
||||||
|
if (mergeHead.exitCode !== 0) {
|
||||||
|
console.log(" No changes, skipping")
|
||||||
|
skipped.push({ number: pr.number, reason: "No changes" })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
const add = await $`git add -A`.nothrow()
|
const add = await $`git add -A`.nothrow()
|
||||||
if (add.exitCode !== 0) {
|
if (add.exitCode !== 0) {
|
||||||
console.log(` Failed to stage`)
|
console.log(" Failed to stage")
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
await $`git clean -fd`.nothrow()
|
await $`git clean -fd`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: "Failed to stage" })
|
skipped.push({ number: pr.number, reason: "Failed to stage" })
|
||||||
|
|
@ -107,21 +76,16 @@ async function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
|
const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
|
||||||
const commit = await Bun.spawn(["git", "commit", "-m", commitMsg], {
|
const commit = await run(["git", "commit", "-m", commitMsg])
|
||||||
stdout: "pipe",
|
if (commit.exitCode !== 0) {
|
||||||
stderr: "pipe",
|
console.log(` Failed to commit: ${commit.stderr}`)
|
||||||
})
|
|
||||||
const commitExit = await commit.exited
|
|
||||||
const commitStderr = await Bun.readableStreamToText(commit.stderr)
|
|
||||||
if (commitExit !== 0) {
|
|
||||||
console.log(` Failed to commit: ${commitStderr}`)
|
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
await $`git clean -fd`.nothrow()
|
await $`git clean -fd`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` })
|
skipped.push({ number: pr.number, reason: `Commit failed: ${commit.stderr}` })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(` Applied successfully`)
|
console.log(" Applied successfully")
|
||||||
applied.push(pr.number)
|
applied.push(pr.number)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,6 +109,18 @@ main().catch((err) => {
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
async function run(args: string[], stdin?: Uint8Array): Promise<RunResult> {
|
||||||
|
const proc = Bun.spawn(args, {
|
||||||
|
stdin: stdin ?? "inherit",
|
||||||
|
stdout: "pipe",
|
||||||
|
stderr: "pipe",
|
||||||
|
})
|
||||||
|
const exitCode = await proc.exited
|
||||||
|
const stdout = await new Response(proc.stdout).text()
|
||||||
|
const stderr = await new Response(proc.stderr).text()
|
||||||
|
return { exitCode, stdout, stderr }
|
||||||
|
}
|
||||||
|
|
||||||
function $(strings: TemplateStringsArray, ...values: unknown[]) {
|
function $(strings: TemplateStringsArray, ...values: unknown[]) {
|
||||||
const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
|
const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue