beta.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. const model = "opencode/gpt-5.3-codex"
  4. interface PR {
  5. number: number
  6. title: string
  7. author: { login: string }
  8. labels: Array<{ name: string }>
  9. }
  10. interface FailedPR {
  11. number: number
  12. title: string
  13. reason: string
  14. }
  15. async function commentOnPR(prNumber: number, reason: string) {
  16. const body = `⚠️ **Blocking Beta Release**
  17. This PR cannot be merged into the beta branch due to: **${reason}**
  18. Please resolve this issue to include this PR in the next beta release.`
  19. try {
  20. await $`gh pr comment ${prNumber} --body ${body}`
  21. console.log(` Posted comment on PR #${prNumber}`)
  22. } catch (err) {
  23. console.log(` Failed to post comment on PR #${prNumber}: ${err}`)
  24. }
  25. }
  26. async function conflicts() {
  27. const out = await $`git diff --name-only --diff-filter=U`.text().catch(() => "")
  28. return out
  29. .split("\n")
  30. .map((x) => x.trim())
  31. .filter(Boolean)
  32. }
  33. async function cleanup() {
  34. try {
  35. await $`git merge --abort`
  36. } catch {}
  37. try {
  38. await $`git checkout -- .`
  39. } catch {}
  40. try {
  41. await $`git clean -fd`
  42. } catch {}
  43. }
  44. function lines(prs: PR[]) {
  45. return prs.map((x) => `- #${x.number}: ${x.title}`).join("\n") || "(none)"
  46. }
  47. async function typecheck() {
  48. try {
  49. await $`bun typecheck`.cwd("packages/opencode")
  50. return true
  51. } catch (err) {
  52. console.log(`Typecheck failed: ${err}`)
  53. return false
  54. }
  55. }
  56. async function build() {
  57. try {
  58. await $`./script/build.ts --single`.cwd("packages/opencode")
  59. return true
  60. } catch (err) {
  61. console.log(`Build failed: ${err}`)
  62. return false
  63. }
  64. }
  65. async function fix(pr: PR, files: string[], prs: PR[], applied: number[], idx: number) {
  66. console.log(` Trying to auto-resolve ${files.length} conflict(s) with opencode...`)
  67. const done = lines(prs.filter((x) => applied.includes(x.number)))
  68. const next = lines(prs.slice(idx + 1))
  69. const prompt = [
  70. `Resolve the current git merge conflicts while merging PR #${pr.number} into the beta branch.`,
  71. `PR #${pr.number}: ${pr.title}`,
  72. `Only touch these files: ${files.join(", ")}.`,
  73. `Merged PRs on HEAD:\n${done}`,
  74. `Pending PRs after this one (context only):\n${next}`,
  75. "IMPORTANT: The conflict resolution must be consistent with already-merged PRs.",
  76. "Pending PRs are context only; do not introduce their changes unless they are already present on HEAD.",
  77. "Prefer already-merged PRs over the base branch when resolving stacked conflicts.",
  78. "If a PR already deleted a file/directory, do not re-add it, instead apply changes in the new semantic location.",
  79. "If a PR already changed an import, keep that change.",
  80. "After resolving the conflicts, run `bun typecheck` in `packages/opencode`.",
  81. "Fix any merge-caused typecheck errors before finishing.",
  82. "Keep the merge in progress, do not abort the merge, and do not create a commit.",
  83. "When done, leave the working tree with no unmerged files and a passing typecheck.",
  84. ].join("\n")
  85. try {
  86. await $`opencode run -m ${model} ${prompt}`
  87. } catch (err) {
  88. console.log(` opencode failed: ${err}`)
  89. return false
  90. }
  91. const left = await conflicts()
  92. if (left.length > 0) {
  93. console.log(` Conflicts remain: ${left.join(", ")}`)
  94. return false
  95. }
  96. if (!(await typecheck())) return false
  97. console.log(" Conflicts resolved with opencode")
  98. return true
  99. }
  100. async function smoke(prs: PR[], applied: number[]) {
  101. console.log("\nRunning final smoke check with opencode...")
  102. const done = lines(prs.filter((x) => applied.includes(x.number)))
  103. const prompt = [
  104. "The beta merge batch is complete.",
  105. `Merged PRs on HEAD:\n${done}`,
  106. "Run `bun typecheck` in `packages/opencode`.",
  107. "Run `./script/build.ts --single` in `packages/opencode`.",
  108. "Fix any merge-caused issues until both commands pass.",
  109. "Do not create a commit.",
  110. ].join("\n")
  111. try {
  112. await $`opencode run -m ${model} ${prompt}`
  113. } catch (err) {
  114. console.log(`Smoke fix failed: ${err}`)
  115. return false
  116. }
  117. if (!(await typecheck())) {
  118. return false
  119. }
  120. if (!(await build())) {
  121. return false
  122. }
  123. const out = await $`git status --porcelain`.text()
  124. if (!out.trim()) {
  125. console.log("Smoke check passed")
  126. return true
  127. }
  128. try {
  129. await $`git add -A`
  130. await $`git commit -m "Fix beta integration"`
  131. } catch (err) {
  132. console.log(`Failed to commit smoke fixes: ${err}`)
  133. return false
  134. }
  135. if (!(await typecheck())) {
  136. return false
  137. }
  138. if (!(await build())) {
  139. return false
  140. }
  141. console.log("Smoke check passed")
  142. return true
  143. }
  144. async function main() {
  145. console.log("Fetching open PRs with beta label...")
  146. const stdout =
  147. await $`gh pr list --state open --draft=false --label beta --json number,title,author,labels --limit 100`.text()
  148. const prs: PR[] = JSON.parse(stdout).sort((a: PR, b: PR) => a.number - b.number)
  149. console.log(`Found ${prs.length} open PRs with beta label`)
  150. if (prs.length === 0) {
  151. console.log("No team PRs to merge")
  152. return
  153. }
  154. console.log("Fetching latest dev branch...")
  155. await $`git fetch origin dev`
  156. console.log("Checking out beta branch...")
  157. await $`git checkout -B beta origin/dev`
  158. const applied: number[] = []
  159. const failed: FailedPR[] = []
  160. for (const [idx, pr] of prs.entries()) {
  161. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  162. console.log(" Fetching PR head...")
  163. try {
  164. await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
  165. } catch (err) {
  166. console.log(` Failed to fetch: ${err}`)
  167. failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" })
  168. await commentOnPR(pr.number, "Fetch failed")
  169. continue
  170. }
  171. console.log(" Merging...")
  172. try {
  173. await $`git merge --no-commit --no-ff pr/${pr.number}`
  174. } catch {
  175. const files = await conflicts()
  176. if (files.length > 0) {
  177. console.log(" Failed to merge (conflicts)")
  178. if (!(await fix(pr, files, prs, applied, idx))) {
  179. await cleanup()
  180. failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" })
  181. await commentOnPR(pr.number, "Merge conflicts with dev branch")
  182. continue
  183. }
  184. } else {
  185. console.log(" Failed to merge")
  186. await cleanup()
  187. failed.push({ number: pr.number, title: pr.title, reason: "Merge failed" })
  188. await commentOnPR(pr.number, "Merge failed")
  189. continue
  190. }
  191. }
  192. try {
  193. await $`git rev-parse -q --verify MERGE_HEAD`.text()
  194. } catch {
  195. console.log(" No changes, skipping")
  196. continue
  197. }
  198. try {
  199. await $`git add -A`
  200. } catch {
  201. console.log(" Failed to stage changes")
  202. failed.push({ number: pr.number, title: pr.title, reason: "Staging failed" })
  203. await commentOnPR(pr.number, "Failed to stage changes")
  204. continue
  205. }
  206. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  207. try {
  208. await $`git commit -m ${commitMsg}`
  209. } catch (err) {
  210. console.log(` Failed to commit: ${err}`)
  211. failed.push({ number: pr.number, title: pr.title, reason: "Commit failed" })
  212. await commentOnPR(pr.number, "Failed to commit changes")
  213. continue
  214. }
  215. console.log(" Applied successfully")
  216. applied.push(pr.number)
  217. }
  218. console.log("\n--- Summary ---")
  219. console.log(`Applied: ${applied.length} PRs`)
  220. applied.forEach((num) => console.log(` - PR #${num}`))
  221. if (failed.length > 0) {
  222. console.log(`Failed: ${failed.length} PRs`)
  223. failed.forEach((f) => console.log(` - PR #${f.number}: ${f.reason}`))
  224. throw new Error(`${failed.length} PR(s) failed to merge`)
  225. }
  226. if (applied.length > 0) {
  227. const ok = await smoke(prs, applied)
  228. if (!ok) {
  229. throw new Error("Final smoke check failed")
  230. }
  231. }
  232. console.log("\nChecking if beta branch has changes...")
  233. await $`git fetch origin beta`
  234. const localTree = await $`git rev-parse beta^{tree}`.text()
  235. const remoteTrees = (await $`git log origin/dev..origin/beta --format=%T`.text()).split("\n")
  236. const matchIdx = remoteTrees.indexOf(localTree.trim())
  237. if (matchIdx !== -1) {
  238. if (matchIdx !== 0) {
  239. console.log(`Beta branch contains this sync, but additional commits exist after it. Leaving beta branch as is.`)
  240. } else {
  241. console.log("Beta branch has identical contents, no push needed")
  242. }
  243. return
  244. }
  245. console.log("Force pushing beta branch...")
  246. await $`git push origin beta --force --no-verify`
  247. console.log("Successfully synced beta branch")
  248. }
  249. main().catch((err) => {
  250. console.error("Error:", err)
  251. process.exit(1)
  252. })