beta.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 main() {
  26. console.log("Fetching open PRs with beta label...")
  27. const stdout = await $`gh pr list --state open --label beta --json number,title,author,labels --limit 100`.text()
  28. const prs: PR[] = JSON.parse(stdout)
  29. console.log(`Found ${prs.length} open PRs with beta label`)
  30. if (prs.length === 0) {
  31. console.log("No team PRs to merge")
  32. return
  33. }
  34. console.log("Fetching latest dev branch...")
  35. await $`git fetch origin dev`
  36. console.log("Checking out beta branch...")
  37. await $`git checkout -B beta origin/dev`
  38. const applied: number[] = []
  39. const failed: FailedPR[] = []
  40. for (const pr of prs) {
  41. console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
  42. console.log(" Fetching PR head...")
  43. try {
  44. await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
  45. } catch (err) {
  46. console.log(` Failed to fetch: ${err}`)
  47. failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" })
  48. await commentOnPR(pr.number, "Fetch failed")
  49. continue
  50. }
  51. console.log(" Merging...")
  52. try {
  53. await $`git merge --no-commit --no-ff pr/${pr.number}`
  54. } catch {
  55. console.log(" Failed to merge (conflicts)")
  56. try {
  57. await $`git merge --abort`
  58. } catch {}
  59. try {
  60. await $`git checkout -- .`
  61. } catch {}
  62. try {
  63. await $`git clean -fd`
  64. } catch {}
  65. failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" })
  66. await commentOnPR(pr.number, "Merge conflicts with dev branch")
  67. continue
  68. }
  69. try {
  70. await $`git rev-parse -q --verify MERGE_HEAD`.text()
  71. } catch {
  72. console.log(" No changes, skipping")
  73. continue
  74. }
  75. try {
  76. await $`git add -A`
  77. } catch {
  78. console.log(" Failed to stage changes")
  79. failed.push({ number: pr.number, title: pr.title, reason: "Staging failed" })
  80. await commentOnPR(pr.number, "Failed to stage changes")
  81. continue
  82. }
  83. const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
  84. try {
  85. await $`git commit -m ${commitMsg}`
  86. } catch (err) {
  87. console.log(` Failed to commit: ${err}`)
  88. failed.push({ number: pr.number, title: pr.title, reason: "Commit failed" })
  89. await commentOnPR(pr.number, "Failed to commit changes")
  90. continue
  91. }
  92. console.log(" Applied successfully")
  93. applied.push(pr.number)
  94. }
  95. console.log("\n--- Summary ---")
  96. console.log(`Applied: ${applied.length} PRs`)
  97. applied.forEach((num) => console.log(` - PR #${num}`))
  98. if (failed.length > 0) {
  99. console.log(`Failed: ${failed.length} PRs`)
  100. failed.forEach((f) => console.log(` - PR #${f.number}: ${f.reason}`))
  101. throw new Error(`${failed.length} PR(s) failed to merge`)
  102. }
  103. console.log("\nForce pushing beta branch...")
  104. await $`git push origin beta --force --no-verify`
  105. console.log("Successfully synced beta branch")
  106. }
  107. main().catch((err) => {
  108. console.error("Error:", err)
  109. process.exit(1)
  110. })