beta.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env bun
  2. interface PR {
  3. number: number
  4. headRefName: string
  5. headRefOid: string
  6. createdAt: string
  7. isDraft: boolean
  8. title: string
  9. }
  10. async function main() {
  11. console.log("Fetching open contributor PRs...")
  12. const prsResult =
  13. await $`gh pr list --label contributor --state open --json number,headRefName,headRefOid,createdAt,isDraft,title --limit 100`.nothrow()
  14. if (prsResult.exitCode !== 0) {
  15. throw new Error(`Failed to fetch PRs: ${prsResult.stderr}`)
  16. }
  17. const allPRs: PR[] = JSON.parse(prsResult.stdout)
  18. const prs = allPRs.filter((pr) => !pr.isDraft)
  19. console.log(`Found ${prs.length} open non-draft contributor PRs`)
  20. console.log("Fetching latest dev branch...")
  21. const fetchDev = await $`git fetch origin dev`.nothrow()
  22. if (fetchDev.exitCode !== 0) {
  23. throw new Error(`Failed to fetch dev branch: ${fetchDev.stderr}`)
  24. }
  25. console.log("Checking out beta branch...")
  26. const checkoutBeta = await $`git checkout -B beta origin/dev`.nothrow()
  27. if (checkoutBeta.exitCode !== 0) {
  28. throw new Error(`Failed to checkout beta branch: ${checkoutBeta.stderr}`)
  29. }
  30. const applied: number[] = []
  31. const skipped: Array<{ number: number; reason: string }> = []
  32. for (const pr of prs) {
  33. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  34. // Fetch the PR
  35. const fetchPR = await $`git fetch origin pull/${pr.number}/head:pr-${pr.number}`.nothrow()
  36. if (fetchPR.exitCode !== 0) {
  37. console.log(` Failed to fetch PR #${pr.number}, skipping`)
  38. skipped.push({ number: pr.number, reason: "Failed to fetch" })
  39. continue
  40. }
  41. // Find merge base and get diff from base to PR head (just the PR's changes)
  42. console.log(` Finding merge base for PR #${pr.number}...`)
  43. const mergeBaseResult = await $`git merge-base dev pr-${pr.number}`.nothrow()
  44. if (mergeBaseResult.exitCode !== 0 || !mergeBaseResult.stdout.trim()) {
  45. console.log(` Failed to find merge base for PR #${pr.number}`)
  46. skipped.push({ number: pr.number, reason: "Failed to find merge base" })
  47. continue
  48. }
  49. const mergeBase = mergeBaseResult.stdout.trim()
  50. console.log(` Getting diff for PR #${pr.number}...`)
  51. const diff = await $`git diff ${mergeBase}..pr-${pr.number}`.nothrow()
  52. if (diff.exitCode !== 0) {
  53. console.log(` Failed to get diff for PR #${pr.number}`)
  54. console.log(` Error: ${diff.stderr}`)
  55. skipped.push({ number: pr.number, reason: `Failed to get diff: ${diff.stderr}` })
  56. continue
  57. }
  58. if (!diff.stdout.trim()) {
  59. console.log(` No changes in PR #${pr.number}, skipping`)
  60. skipped.push({ number: pr.number, reason: "No changes" })
  61. continue
  62. }
  63. // Apply the diff
  64. console.log(` Applying diff for PR #${pr.number}...`)
  65. const apply = await Bun.spawn(["git", "apply"], {
  66. stdin: new TextEncoder().encode(diff.stdout),
  67. stdout: "pipe",
  68. stderr: "pipe",
  69. })
  70. const applyExit = await apply.exited
  71. const applyStderr = await Bun.readableStreamToText(apply.stderr)
  72. if (applyExit !== 0) {
  73. console.log(` Failed to apply diff for PR #${pr.number}`)
  74. console.log(` Error: ${applyStderr}`)
  75. await $`git checkout -- .`.nothrow()
  76. skipped.push({ number: pr.number, reason: `Failed to apply diff: ${applyStderr}` })
  77. continue
  78. }
  79. // Stage all changes
  80. const add = await $`git add -A`.nothrow()
  81. if (add.exitCode !== 0) {
  82. console.log(` Failed to stage changes for PR #${pr.number}`)
  83. await $`git checkout -- .`.nothrow()
  84. skipped.push({ number: pr.number, reason: "Failed to stage" })
  85. continue
  86. }
  87. // Commit
  88. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  89. const commit = await Bun.spawn(["git", "commit", "-m", commitMsg], { stdout: "pipe", stderr: "pipe" })
  90. const commitExit = await commit.exited
  91. const commitStderr = await Bun.readableStreamToText(commit.stderr)
  92. if (commitExit !== 0) {
  93. console.log(` Failed to commit PR #${pr.number}`)
  94. console.log(` Error: ${commitStderr}`)
  95. await $`git checkout -- .`.nothrow()
  96. skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` })
  97. continue
  98. }
  99. console.log(` Successfully applied PR #${pr.number}`)
  100. applied.push(pr.number)
  101. }
  102. console.log("\n--- Summary ---")
  103. console.log(`Applied: ${applied.length} PRs`)
  104. applied.forEach((num) => console.log(` - PR #${num}`))
  105. console.log(`Skipped: ${skipped.length} PRs`)
  106. skipped.forEach((x) => console.log(` - PR #${x.number}: ${x.reason}`))
  107. console.log("\nForce pushing beta branch...")
  108. const push = await $`git push origin beta --force --no-verify`.nothrow()
  109. if (push.exitCode !== 0) {
  110. throw new Error(`Failed to push beta branch: ${push.stderr}`)
  111. }
  112. console.log("Successfully synced beta branch")
  113. }
  114. main().catch((err) => {
  115. console.error("Error:", err)
  116. process.exit(1)
  117. })
  118. function $(strings: TemplateStringsArray, ...values: unknown[]) {
  119. const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
  120. return {
  121. async nothrow() {
  122. const proc = Bun.spawn(cmd.split(" "), {
  123. stdout: "pipe",
  124. stderr: "pipe",
  125. })
  126. const exitCode = await proc.exited
  127. const stdout = await new Response(proc.stdout).text()
  128. const stderr = await new Response(proc.stderr).text()
  129. return { exitCode, stdout, stderr }
  130. },
  131. }
  132. }