beta.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Get diff from dev to PR head (PR's changes)
  42. console.log(` Getting diff for PR #${pr.number}...`)
  43. const diff = await $`git diff dev..pr-${pr.number}`.nothrow()
  44. if (diff.exitCode !== 0) {
  45. console.log(` Failed to get diff for PR #${pr.number}`)
  46. console.log(` Error: ${diff.stderr}`)
  47. skipped.push({ number: pr.number, reason: `Failed to get diff: ${diff.stderr}` })
  48. continue
  49. }
  50. if (!diff.stdout.trim()) {
  51. console.log(` No changes in PR #${pr.number}, skipping`)
  52. skipped.push({ number: pr.number, reason: "No changes" })
  53. continue
  54. }
  55. // Apply the diff
  56. console.log(` Applying diff for PR #${pr.number}...`)
  57. const apply = await Bun.spawn(["git", "apply"], {
  58. stdin: new TextEncoder().encode(diff.stdout),
  59. stdout: "pipe",
  60. stderr: "pipe",
  61. })
  62. const applyExit = await apply.exited
  63. const applyStderr = await Bun.readableStreamToText(apply.stderr)
  64. if (applyExit !== 0) {
  65. console.log(` Failed to apply diff for PR #${pr.number}`)
  66. console.log(` Error: ${applyStderr}`)
  67. await $`git checkout -- .`.nothrow()
  68. skipped.push({ number: pr.number, reason: `Failed to apply diff: ${applyStderr}` })
  69. continue
  70. }
  71. // Stage all changes
  72. const add = await $`git add -A`.nothrow()
  73. if (add.exitCode !== 0) {
  74. console.log(` Failed to stage changes for PR #${pr.number}`)
  75. await $`git checkout -- .`.nothrow()
  76. skipped.push({ number: pr.number, reason: "Failed to stage" })
  77. continue
  78. }
  79. // Commit
  80. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  81. const commit = await Bun.spawn(["git", "commit", "-m", commitMsg], { stdout: "pipe", stderr: "pipe" })
  82. const commitExit = await commit.exited
  83. const commitStderr = await Bun.readableStreamToText(commit.stderr)
  84. if (commitExit !== 0) {
  85. console.log(` Failed to commit PR #${pr.number}`)
  86. console.log(` Error: ${commitStderr}`)
  87. await $`git checkout -- .`.nothrow()
  88. skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` })
  89. continue
  90. }
  91. console.log(` Successfully applied PR #${pr.number}`)
  92. applied.push(pr.number)
  93. }
  94. console.log("\n--- Summary ---")
  95. console.log(`Applied: ${applied.length} PRs`)
  96. applied.forEach((num) => console.log(` - PR #${num}`))
  97. console.log(`Skipped: ${skipped.length} PRs`)
  98. skipped.forEach((x) => console.log(` - PR #${x.number}: ${x.reason}`))
  99. console.log("\nForce pushing beta branch...")
  100. const push = await $`git push origin beta --force --no-verify`.nothrow()
  101. if (push.exitCode !== 0) {
  102. throw new Error(`Failed to push beta branch: ${push.stderr}`)
  103. }
  104. console.log("Successfully synced beta branch")
  105. }
  106. main().catch((err) => {
  107. console.error("Error:", err)
  108. process.exit(1)
  109. })
  110. function $(strings: TemplateStringsArray, ...values: unknown[]) {
  111. const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
  112. return {
  113. async nothrow() {
  114. const proc = Bun.spawn(cmd.split(" "), {
  115. stdout: "pipe",
  116. stderr: "pipe",
  117. })
  118. const exitCode = await proc.exited
  119. const stdout = await new Response(proc.stdout).text()
  120. const stderr = await new Response(proc.stderr).text()
  121. return { exitCode, stdout, stderr }
  122. },
  123. }
  124. }