|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +interface PR { |
| 4 | + number: number |
| 5 | + headRefName: string |
| 6 | + headRefOid: string |
| 7 | + createdAt: string |
| 8 | + isDraft: boolean |
| 9 | + title: string |
| 10 | +} |
| 11 | + |
| 12 | +async function main() { |
| 13 | + console.log("Fetching open contributor PRs...") |
| 14 | + |
| 15 | + const prsResult = |
| 16 | + await $`gh pr list --label contributor --state open --json number,headRefName,headRefOid,createdAt,isDraft,title --limit 100`.nothrow() |
| 17 | + if (prsResult.exitCode !== 0) { |
| 18 | + throw new Error(`Failed to fetch PRs: ${prsResult.stderr}`) |
| 19 | + } |
| 20 | + |
| 21 | + const allPRs: PR[] = JSON.parse(prsResult.stdout) |
| 22 | + const prs = allPRs.filter((pr) => !pr.isDraft) |
| 23 | + |
| 24 | + console.log(`Found ${prs.length} open non-draft contributor PRs`) |
| 25 | + |
| 26 | + console.log("Fetching latest dev branch...") |
| 27 | + const fetchDev = await $`git fetch origin dev`.nothrow() |
| 28 | + if (fetchDev.exitCode !== 0) { |
| 29 | + throw new Error(`Failed to fetch dev branch: ${fetchDev.stderr}`) |
| 30 | + } |
| 31 | + |
| 32 | + console.log("Checking out beta branch...") |
| 33 | + const checkoutBeta = await $`git checkout -B beta origin/dev`.nothrow() |
| 34 | + if (checkoutBeta.exitCode !== 0) { |
| 35 | + throw new Error(`Failed to checkout beta branch: ${checkoutBeta.stderr}`) |
| 36 | + } |
| 37 | + |
| 38 | + const applied: number[] = [] |
| 39 | + const skipped: Array<{ number: number; reason: string }> = [] |
| 40 | + |
| 41 | + for (const pr of prs) { |
| 42 | + console.log(`\nProcessing PR #${pr.number}: ${pr.title}`) |
| 43 | + |
| 44 | + const fetchPR = await $`git fetch origin pull/${pr.number}/head:pr-${pr.number}`.nothrow() |
| 45 | + if (fetchPR.exitCode !== 0) { |
| 46 | + console.log(` Failed to fetch PR #${pr.number}, skipping`) |
| 47 | + skipped.push({ number: pr.number, reason: "Failed to fetch" }) |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + const merge = await $`git merge --squash pr-${pr.number}`.nothrow() |
| 52 | + if (merge.exitCode !== 0) { |
| 53 | + console.log(` Squash merge failed for PR #${pr.number}`) |
| 54 | + console.log(` Error: ${merge.stderr}`) |
| 55 | + await $`git reset --hard HEAD`.nothrow() |
| 56 | + skipped.push({ number: pr.number, reason: `Squash merge failed: ${merge.stderr}` }) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + const add = await $`git add -A`.nothrow() |
| 61 | + if (add.exitCode !== 0) { |
| 62 | + console.log(` Failed to stage changes for PR #${pr.number}`) |
| 63 | + await $`git reset --hard HEAD`.nothrow() |
| 64 | + skipped.push({ number: pr.number, reason: "Failed to stage" }) |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + const status = await $`git status --porcelain`.nothrow() |
| 69 | + if (status.exitCode !== 0 || !status.stdout.trim()) { |
| 70 | + console.log(` No changes to commit for PR #${pr.number}, skipping`) |
| 71 | + await $`git reset --hard HEAD`.nothrow() |
| 72 | + skipped.push({ number: pr.number, reason: "No changes to commit" }) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + const commitMsg = `Apply PR #${pr.number}: ${pr.title}` |
| 77 | + const commit = await Bun.spawn(["git", "commit", "-m", commitMsg], { stdout: "pipe", stderr: "pipe" }) |
| 78 | + const commitExit = await commit.exited |
| 79 | + const commitStderr = await Bun.readableStreamToText(commit.stderr) |
| 80 | + |
| 81 | + if (commitExit !== 0) { |
| 82 | + console.log(` Failed to commit PR #${pr.number}`) |
| 83 | + console.log(` Error: ${commitStderr}`) |
| 84 | + await $`git reset --hard HEAD`.nothrow() |
| 85 | + skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` }) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + console.log(` Successfully applied PR #${pr.number}`) |
| 90 | + applied.push(pr.number) |
| 91 | + } |
| 92 | + |
| 93 | + console.log("\n--- Summary ---") |
| 94 | + console.log(`Applied: ${applied.length} PRs`) |
| 95 | + applied.forEach((num) => console.log(` - PR #${num}`)) |
| 96 | + console.log(`Skipped: ${skipped.length} PRs`) |
| 97 | + skipped.forEach((x) => console.log(` - PR #${x.number}: ${x.reason}`)) |
| 98 | + |
| 99 | + console.log("\nForce pushing beta branch...") |
| 100 | + const push = await $`git push origin beta --force`.nothrow() |
| 101 | + if (push.exitCode !== 0) { |
| 102 | + throw new Error(`Failed to push beta branch: ${push.stderr}`) |
| 103 | + } |
| 104 | + |
| 105 | + console.log("Successfully synced beta branch") |
| 106 | +} |
| 107 | + |
| 108 | +main().catch((err) => { |
| 109 | + console.error("Error:", err) |
| 110 | + process.exit(1) |
| 111 | +}) |
| 112 | + |
| 113 | +function $(strings: TemplateStringsArray, ...values: unknown[]) { |
| 114 | + const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "") |
| 115 | + return { |
| 116 | + async nothrow() { |
| 117 | + const proc = Bun.spawn(cmd.split(" "), { |
| 118 | + stdout: "pipe", |
| 119 | + stderr: "pipe", |
| 120 | + }) |
| 121 | + const exitCode = await proc.exited |
| 122 | + const stdout = await new Response(proc.stdout).text() |
| 123 | + const stderr = await new Response(proc.stderr).text() |
| 124 | + return { exitCode, stdout, stderr } |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
0 commit comments