beta.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 shallow = await run(["git", "rev-parse", "--is-shallow-repository"])
  30. if (shallow.exitCode !== 0) {
  31. throw new Error(`Failed to check shallow state: ${shallow.stderr}`)
  32. }
  33. if (shallow.stdout.trim() === "true") {
  34. console.log("Unshallowing repository...")
  35. const unshallow = await run(["git", "fetch", "--unshallow"])
  36. if (unshallow.exitCode !== 0) {
  37. throw new Error(`Failed to unshallow repository: ${unshallow.stderr}`)
  38. }
  39. }
  40. const applied: number[] = []
  41. const skipped: Array<{ number: number; reason: string }> = []
  42. for (const pr of prs) {
  43. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  44. console.log(" Fetching PR head...")
  45. const fetch = await run(["git", "fetch", "origin", `pull/${pr.number}/head:pr/${pr.number}`])
  46. if (fetch.exitCode !== 0) {
  47. console.log(` Failed to fetch PR head: ${fetch.stderr}`)
  48. skipped.push({ number: pr.number, reason: `Fetch failed: ${fetch.stderr}` })
  49. continue
  50. }
  51. console.log(" Merging...")
  52. const merge = await run(["git", "merge", "--no-commit", "--no-ff", `pr/${pr.number}`])
  53. if (merge.exitCode !== 0) {
  54. console.log(" Failed to merge (conflicts)")
  55. await $`git merge --abort`.nothrow()
  56. await $`git checkout -- .`.nothrow()
  57. await $`git clean -fd`.nothrow()
  58. skipped.push({ number: pr.number, reason: "Has conflicts" })
  59. continue
  60. }
  61. const mergeHead = await $`git rev-parse -q --verify MERGE_HEAD`.nothrow()
  62. if (mergeHead.exitCode !== 0) {
  63. console.log(" No changes, skipping")
  64. skipped.push({ number: pr.number, reason: "No changes" })
  65. continue
  66. }
  67. const add = await $`git add -A`.nothrow()
  68. if (add.exitCode !== 0) {
  69. console.log(" Failed to stage")
  70. await $`git checkout -- .`.nothrow()
  71. await $`git clean -fd`.nothrow()
  72. skipped.push({ number: pr.number, reason: "Failed to stage" })
  73. continue
  74. }
  75. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  76. const commit = await run(["git", "commit", "-m", commitMsg])
  77. if (commit.exitCode !== 0) {
  78. console.log(` Failed to commit: ${commit.stderr}`)
  79. await $`git checkout -- .`.nothrow()
  80. await $`git clean -fd`.nothrow()
  81. skipped.push({ number: pr.number, reason: `Commit failed: ${commit.stderr}` })
  82. continue
  83. }
  84. console.log(" Applied successfully")
  85. applied.push(pr.number)
  86. }
  87. console.log("\n--- Summary ---")
  88. console.log(`Applied: ${applied.length} PRs`)
  89. applied.forEach((num) => console.log(` - PR #${num}`))
  90. console.log(`Skipped: ${skipped.length} PRs`)
  91. skipped.forEach((x) => console.log(` - PR #${x.number}: ${x.reason}`))
  92. console.log("\nForce pushing beta branch...")
  93. const push = await $`git push origin beta --force --no-verify`.nothrow()
  94. if (push.exitCode !== 0) {
  95. throw new Error(`Failed to push beta branch: ${push.stderr}`)
  96. }
  97. console.log("Successfully synced beta branch")
  98. }
  99. main().catch((err) => {
  100. console.error("Error:", err)
  101. process.exit(1)
  102. })
  103. async function run(args: string[], stdin?: Uint8Array): Promise<RunResult> {
  104. const proc = Bun.spawn(args, {
  105. stdin: stdin ?? "inherit",
  106. stdout: "pipe",
  107. stderr: "pipe",
  108. })
  109. const exitCode = await proc.exited
  110. const stdout = await new Response(proc.stdout).text()
  111. const stderr = await new Response(proc.stderr).text()
  112. return { exitCode, stdout, stderr }
  113. }
  114. function $(strings: TemplateStringsArray, ...values: unknown[]) {
  115. const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
  116. return {
  117. async nothrow() {
  118. const proc = Bun.spawn(cmd.split(" "), {
  119. stdout: "pipe",
  120. stderr: "pipe",
  121. })
  122. const exitCode = await proc.exited
  123. const stdout = await new Response(proc.stdout).text()
  124. const stderr = await new Response(proc.stderr).text()
  125. return { exitCode, stdout, stderr }
  126. },
  127. }
  128. }