beta.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = await $`gh pr list --state open --label beta --json number,title,author,labels --limit 100`.text()
  68. const prs: PR[] = JSON.parse(stdout).sort((a: PR, b: PR) => a.number - b.number)
  69. console.log(`Found ${prs.length} open PRs with beta label`)
  70. if (prs.length === 0) {
  71. console.log("No team PRs to merge")
  72. return
  73. }
  74. console.log("Fetching latest dev branch...")
  75. await $`git fetch origin dev`
  76. console.log("Checking out beta branch...")
  77. await $`git checkout -B beta origin/dev`
  78. const applied: number[] = []
  79. const failed: FailedPR[] = []
  80. for (const pr of prs) {
  81. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  82. console.log(" Fetching PR head...")
  83. try {
  84. await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
  85. } catch (err) {
  86. console.log(` Failed to fetch: ${err}`)
  87. failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" })
  88. await commentOnPR(pr.number, "Fetch failed")
  89. continue
  90. }
  91. console.log(" Merging...")
  92. try {
  93. await $`git merge --no-commit --no-ff pr/${pr.number}`
  94. } catch {
  95. const files = await conflicts()
  96. if (files.length > 0) {
  97. console.log(" Failed to merge (conflicts)")
  98. if (!(await fix(pr, files))) {
  99. await cleanup()
  100. failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" })
  101. await commentOnPR(pr.number, "Merge conflicts with dev branch")
  102. continue
  103. }
  104. } else {
  105. console.log(" Failed to merge")
  106. await cleanup()
  107. failed.push({ number: pr.number, title: pr.title, reason: "Merge failed" })
  108. await commentOnPR(pr.number, "Merge failed")
  109. continue
  110. }
  111. }
  112. try {
  113. await $`git rev-parse -q --verify MERGE_HEAD`.text()
  114. } catch {
  115. console.log(" No changes, skipping")
  116. continue
  117. }
  118. try {
  119. await $`git add -A`
  120. } catch {
  121. console.log(" Failed to stage changes")
  122. failed.push({ number: pr.number, title: pr.title, reason: "Staging failed" })
  123. await commentOnPR(pr.number, "Failed to stage changes")
  124. continue
  125. }
  126. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  127. try {
  128. await $`git commit -m ${commitMsg}`
  129. } catch (err) {
  130. console.log(` Failed to commit: ${err}`)
  131. failed.push({ number: pr.number, title: pr.title, reason: "Commit failed" })
  132. await commentOnPR(pr.number, "Failed to commit changes")
  133. continue
  134. }
  135. console.log(" Applied successfully")
  136. applied.push(pr.number)
  137. }
  138. console.log("\n--- Summary ---")
  139. console.log(`Applied: ${applied.length} PRs`)
  140. applied.forEach((num) => console.log(` - PR #${num}`))
  141. if (failed.length > 0) {
  142. console.log(`Failed: ${failed.length} PRs`)
  143. failed.forEach((f) => console.log(` - PR #${f.number}: ${f.reason}`))
  144. throw new Error(`${failed.length} PR(s) failed to merge`)
  145. }
  146. console.log("\nChecking if beta branch has changes...")
  147. await $`git fetch origin beta`
  148. const localTree = await $`git rev-parse beta^{tree}`.text()
  149. const remoteTrees = (await $`git log origin/dev..origin/beta --format=%T`.text()).split("\n")
  150. const matchIdx = remoteTrees.indexOf(localTree.trim())
  151. if (matchIdx !== -1) {
  152. if (matchIdx !== 0) {
  153. console.log(`Beta branch contains this sync, but additional commits exist after it. Leaving beta branch as is.`)
  154. } else {
  155. console.log("Beta branch has identical contents, no push needed")
  156. }
  157. return
  158. }
  159. console.log("Force pushing beta branch...")
  160. await $`git push origin beta --force --no-verify`
  161. console.log("Successfully synced beta branch")
  162. }
  163. main().catch((err) => {
  164. console.error("Error:", err)
  165. process.exit(1)
  166. })