beta.ts 4.3 KB

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