mirror of
https://github.com/anomalyco/opencode.git
synced 2026-02-01 22:48:16 +00:00
ci
This commit is contained in:
@@ -2,26 +2,19 @@
|
|||||||
|
|
||||||
interface PR {
|
interface PR {
|
||||||
number: number
|
number: number
|
||||||
headRefName: string
|
|
||||||
headRefOid: string
|
|
||||||
createdAt: string
|
|
||||||
isDraft: boolean
|
|
||||||
title: string
|
title: string
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("Fetching open contributor PRs...")
|
console.log("Fetching open contributor PRs...")
|
||||||
|
|
||||||
const prsResult =
|
const prsResult = await $`gh pr list --label contributor --state open --json number,title --limit 100`.nothrow()
|
||||||
await $`gh pr list --label contributor --state open --json number,headRefName,headRefOid,createdAt,isDraft,title --limit 100`.nothrow()
|
|
||||||
if (prsResult.exitCode !== 0) {
|
if (prsResult.exitCode !== 0) {
|
||||||
throw new Error(`Failed to fetch PRs: ${prsResult.stderr}`)
|
throw new Error(`Failed to fetch PRs: ${prsResult.stderr}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const allPRs: PR[] = JSON.parse(prsResult.stdout)
|
const prs: PR[] = JSON.parse(prsResult.stdout)
|
||||||
const prs = allPRs.filter((pr) => !pr.isDraft)
|
console.log(`Found ${prs.length} open contributor PRs`)
|
||||||
|
|
||||||
console.log(`Found ${prs.length} open non-draft contributor PRs`)
|
|
||||||
|
|
||||||
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()
|
||||||
@@ -41,43 +34,25 @@ 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}`)
|
||||||
|
|
||||||
// Fetch the PR
|
// Get the diff from GitHub
|
||||||
const fetchPR = await $`git fetch origin pull/${pr.number}/head:pr-${pr.number}`.nothrow()
|
console.log(` Getting diff...`)
|
||||||
if (fetchPR.exitCode !== 0) {
|
const diffResult = await $`gh pr diff ${pr.number}`.nothrow()
|
||||||
console.log(` Failed to fetch PR #${pr.number}, skipping`)
|
if (diffResult.exitCode !== 0) {
|
||||||
skipped.push({ number: pr.number, reason: "Failed to fetch" })
|
console.log(` Failed to get diff`)
|
||||||
|
skipped.push({ number: pr.number, reason: `Failed to get diff: ${diffResult.stderr}` })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find merge base and get diff from base to PR head (just the PR's changes)
|
if (!diffResult.stdout.trim()) {
|
||||||
console.log(` Finding merge base for PR #${pr.number}...`)
|
console.log(` No changes, skipping`)
|
||||||
const mergeBaseResult = await $`git merge-base dev pr-${pr.number}`.nothrow()
|
|
||||||
if (mergeBaseResult.exitCode !== 0 || !mergeBaseResult.stdout.trim()) {
|
|
||||||
console.log(` Failed to find merge base for PR #${pr.number}`)
|
|
||||||
skipped.push({ number: pr.number, reason: "Failed to find merge base" })
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
const mergeBase = mergeBaseResult.stdout.trim()
|
|
||||||
|
|
||||||
console.log(` Getting diff for PR #${pr.number}...`)
|
|
||||||
const diff = await $`git diff ${mergeBase}..pr-${pr.number}`.nothrow()
|
|
||||||
if (diff.exitCode !== 0) {
|
|
||||||
console.log(` Failed to get diff for PR #${pr.number}`)
|
|
||||||
console.log(` Error: ${diff.stderr}`)
|
|
||||||
skipped.push({ number: pr.number, reason: `Failed to get diff: ${diff.stderr}` })
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!diff.stdout.trim()) {
|
|
||||||
console.log(` No changes in PR #${pr.number}, skipping`)
|
|
||||||
skipped.push({ number: pr.number, reason: "No changes" })
|
skipped.push({ number: pr.number, reason: "No changes" })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the diff
|
// Try to apply the diff
|
||||||
console.log(` Applying diff for PR #${pr.number}...`)
|
console.log(` Applying...`)
|
||||||
const apply = await Bun.spawn(["git", "apply"], {
|
const apply = await Bun.spawn(["git", "apply", "--3way"], {
|
||||||
stdin: new TextEncoder().encode(diff.stdout),
|
stdin: new TextEncoder().encode(diffResult.stdout),
|
||||||
stdout: "pipe",
|
stdout: "pipe",
|
||||||
stderr: "pipe",
|
stderr: "pipe",
|
||||||
})
|
})
|
||||||
@@ -85,37 +60,34 @@ async function main() {
|
|||||||
const applyStderr = await Bun.readableStreamToText(apply.stderr)
|
const applyStderr = await Bun.readableStreamToText(apply.stderr)
|
||||||
|
|
||||||
if (applyExit !== 0) {
|
if (applyExit !== 0) {
|
||||||
console.log(` Failed to apply diff for PR #${pr.number}`)
|
console.log(` Failed to apply (conflicts)`)
|
||||||
console.log(` Error: ${applyStderr}`)
|
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: `Failed to apply diff: ${applyStderr}` })
|
await $`git clean -fd`.nothrow()
|
||||||
|
skipped.push({ number: pr.number, reason: "Has conflicts" })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stage all changes
|
// Stage and commit
|
||||||
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 changes for PR #${pr.number}`)
|
console.log(` Failed to stage`)
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
|
await $`git clean -fd`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: "Failed to stage" })
|
skipped.push({ number: pr.number, reason: "Failed to stage" })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit
|
|
||||||
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], { stdout: "pipe", stderr: "pipe" })
|
const commit = await $`git commit -m ${commitMsg}`.nothrow()
|
||||||
const commitExit = await commit.exited
|
if (commit.exitCode !== 0) {
|
||||||
const commitStderr = await Bun.readableStreamToText(commit.stderr)
|
console.log(` Failed to commit`)
|
||||||
|
|
||||||
if (commitExit !== 0) {
|
|
||||||
console.log(` Failed to commit PR #${pr.number}`)
|
|
||||||
console.log(` Error: ${commitStderr}`)
|
|
||||||
await $`git checkout -- .`.nothrow()
|
await $`git checkout -- .`.nothrow()
|
||||||
skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` })
|
await $`git clean -fd`.nothrow()
|
||||||
|
skipped.push({ number: pr.number, reason: `Commit failed: ${commit.stderr}` })
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(` Successfully applied PR #${pr.number}`)
|
console.log(` Applied successfully`)
|
||||||
applied.push(pr.number)
|
applied.push(pr.number)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user