beta.ts 4.1 KB

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