beta.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. interface PR {
  4. number: number
  5. title: string
  6. author: { login: string }
  7. labels: Array<{ name: string }>
  8. }
  9. interface FailedPR {
  10. number: number
  11. title: string
  12. reason: string
  13. }
  14. async function commentOnPR(prNumber: number, reason: string) {
  15. const body = `⚠️ **Blocking Beta Release**
  16. This PR cannot be merged into the beta branch due to: **${reason}**
  17. Please resolve this issue to include this PR in the next beta release.`
  18. try {
  19. await $`gh pr comment ${prNumber} --body ${body}`
  20. console.log(` Posted comment on PR #${prNumber}`)
  21. } catch (err) {
  22. console.log(` Failed to post comment on PR #${prNumber}: ${err}`)
  23. }
  24. }
  25. async function conflicts() {
  26. const out = await $`git diff --name-only --diff-filter=U`.text().catch(() => "")
  27. return out
  28. .split("\n")
  29. .map((x) => x.trim())
  30. .filter(Boolean)
  31. }
  32. async function cleanup() {
  33. try {
  34. await $`git merge --abort`
  35. } catch {}
  36. try {
  37. await $`git checkout -- .`
  38. } catch {}
  39. try {
  40. await $`git clean -fd`
  41. } catch {}
  42. }
  43. async function fix(pr: PR, files: string[]) {
  44. console.log(` Trying to auto-resolve ${files.length} conflict(s) with opencode...`)
  45. const prompt = [
  46. `Resolve the current git merge conflicts while merging PR #${pr.number} into the beta branch.`,
  47. `Only touch these files: ${files.join(", ")}.`,
  48. "Keep the merge in progress, do not abort the merge, and do not create a commit.",
  49. "When done, leave the working tree with no unmerged files.",
  50. ].join("\n")
  51. try {
  52. await $`opencode run -m opencode/gpt-5.3-codex ${prompt}`
  53. } catch (err) {
  54. console.log(` opencode failed: ${err}`)
  55. return false
  56. }
  57. const left = await conflicts()
  58. if (left.length > 0) {
  59. console.log(` Conflicts remain: ${left.join(", ")}`)
  60. return false
  61. }
  62. console.log(" Conflicts resolved with opencode")
  63. return true
  64. }
  65. async function main() {
  66. console.log("Fetching open PRs with beta label...")
  67. const stdout =
  68. await $`gh pr list --state open --draft=false --label beta --json number,title,author,labels --limit 100`.text()
  69. const prs: PR[] = JSON.parse(stdout).sort((a: PR, b: PR) => a.number - b.number)
  70. console.log(`Found ${prs.length} open PRs with beta label`)
  71. if (prs.length === 0) {
  72. console.log("No team PRs to merge")
  73. return
  74. }
  75. console.log("Fetching latest dev branch...")
  76. await $`git fetch origin dev`
  77. console.log("Checking out beta branch...")
  78. await $`git checkout -B beta origin/dev`
  79. const applied: number[] = []
  80. const failed: FailedPR[] = []
  81. for (const pr of prs) {
  82. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  83. console.log(" Fetching PR head...")
  84. try {
  85. await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
  86. } catch (err) {
  87. console.log(` Failed to fetch: ${err}`)
  88. failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" })
  89. await commentOnPR(pr.number, "Fetch failed")
  90. continue
  91. }
  92. console.log(" Merging...")
  93. try {
  94. await $`git merge --no-commit --no-ff pr/${pr.number}`
  95. } catch {
  96. const files = await conflicts()
  97. if (files.length > 0) {
  98. console.log(" Failed to merge (conflicts)")
  99. if (!(await fix(pr, files))) {
  100. await cleanup()
  101. failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" })
  102. await commentOnPR(pr.number, "Merge conflicts with dev branch")
  103. continue
  104. }
  105. } else {
  106. console.log(" Failed to merge")
  107. await cleanup()
  108. failed.push({ number: pr.number, title: pr.title, reason: "Merge failed" })
  109. await commentOnPR(pr.number, "Merge failed")
  110. continue
  111. }
  112. }
  113. try {
  114. await $`git rev-parse -q --verify MERGE_HEAD`.text()
  115. } catch {
  116. console.log(" No changes, skipping")
  117. continue
  118. }
  119. try {
  120. await $`git add -A`
  121. } catch {
  122. console.log(" Failed to stage changes")
  123. failed.push({ number: pr.number, title: pr.title, reason: "Staging failed" })
  124. await commentOnPR(pr.number, "Failed to stage changes")
  125. continue
  126. }
  127. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  128. try {
  129. await $`git commit -m ${commitMsg}`
  130. } catch (err) {
  131. console.log(` Failed to commit: ${err}`)
  132. failed.push({ number: pr.number, title: pr.title, reason: "Commit failed" })
  133. await commentOnPR(pr.number, "Failed to commit changes")
  134. continue
  135. }
  136. console.log(" Applied successfully")
  137. applied.push(pr.number)
  138. }
  139. console.log("\n--- Summary ---")
  140. console.log(`Applied: ${applied.length} PRs`)
  141. applied.forEach((num) => console.log(` - PR #${num}`))
  142. if (failed.length > 0) {
  143. console.log(`Failed: ${failed.length} PRs`)
  144. failed.forEach((f) => console.log(` - PR #${f.number}: ${f.reason}`))
  145. throw new Error(`${failed.length} PR(s) failed to merge`)
  146. }
  147. console.log("\nChecking if beta branch has changes...")
  148. await $`git fetch origin beta`
  149. const localTree = await $`git rev-parse beta^{tree}`.text()
  150. const remoteTrees = (await $`git log origin/dev..origin/beta --format=%T`.text()).split("\n")
  151. const matchIdx = remoteTrees.indexOf(localTree.trim())
  152. if (matchIdx !== -1) {
  153. if (matchIdx !== 0) {
  154. console.log(`Beta branch contains this sync, but additional commits exist after it. Leaving beta branch as is.`)
  155. } else {
  156. console.log("Beta branch has identical contents, no push needed")
  157. }
  158. return
  159. }
  160. console.log("Force pushing beta branch...")
  161. await $`git push origin beta --force --no-verify`
  162. console.log("Successfully synced beta branch")
  163. }
  164. main().catch((err) => {
  165. console.error("Error:", err)
  166. process.exit(1)
  167. })