sync-zed.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { tmpdir } from "os"
  4. import { join } from "path"
  5. const FORK_REPO = "sst/zed-extensions"
  6. const UPSTREAM_REPO = "zed-industries/extensions"
  7. const EXTENSION_NAME = "opencode"
  8. const OPENCODE_REPO = "sst/opencode"
  9. async function main() {
  10. const version = process.argv[2]
  11. if (!version) throw new Error("Version argument required: bun script/sync-zed.ts v1.0.52")
  12. const token = process.env.GITHUB_TOKEN
  13. if (!token) throw new Error("GITHUB_TOKEN environment variable required")
  14. const cleanVersion = version.replace(/^v/, "")
  15. console.log(`📦 Syncing Zed extension for version ${cleanVersion}`)
  16. const commitSha = await $`git rev-parse ${version}`.text()
  17. const sha = commitSha.trim()
  18. console.log(`🔍 Found commit SHA: ${sha}`)
  19. const extensionToml = await $`git show ${version}:packages/extensions/zed/extension.toml`.text()
  20. const parsed = Bun.TOML.parse(extensionToml) as { version: string }
  21. const extensionVersion = parsed.version
  22. if (extensionVersion !== cleanVersion) {
  23. throw new Error(`Version mismatch: extension.toml has ${extensionVersion} but tag is ${cleanVersion}`)
  24. }
  25. console.log(`✅ Version ${extensionVersion} matches tag`)
  26. // Clone the fork to a temp directory
  27. const workDir = join(tmpdir(), `zed-extensions-${Date.now()}`)
  28. console.log(`📁 Working in ${workDir}`)
  29. await $`git clone https://x-access-token:${token}@github.com/${FORK_REPO}.git ${workDir}`
  30. process.chdir(workDir)
  31. // Configure git identity
  32. await $`git config user.name "github-actions[bot]"`
  33. await $`git config user.email "github-actions[bot]@users.noreply.github.com"`
  34. // Sync fork with upstream
  35. console.log(`🔄 Syncing fork with upstream...`)
  36. await $`git remote add upstream https://github.com/${UPSTREAM_REPO}.git`
  37. await $`git fetch upstream`
  38. await $`git checkout main`
  39. await $`git merge upstream/main --ff-only`
  40. await $`git push origin main`
  41. console.log(`✅ Fork synced`)
  42. // Create a new branch
  43. const branchName = `update-${EXTENSION_NAME}-${cleanVersion}`
  44. console.log(`🌿 Creating branch ${branchName}`)
  45. await $`git checkout -b ${branchName}`
  46. const submodulePath = `extensions/${EXTENSION_NAME}`
  47. console.log(`📌 Updating submodule to commit ${sha}`)
  48. await $`git submodule update --init ${submodulePath}`
  49. process.chdir(submodulePath)
  50. await $`git fetch`
  51. await $`git checkout ${sha}`
  52. process.chdir(workDir)
  53. await $`git add ${submodulePath}`
  54. console.log(`📝 Updating extensions.toml`)
  55. const extensionsTomlPath = "extensions.toml"
  56. const extensionsToml = await Bun.file(extensionsTomlPath).text()
  57. const versionRegex = new RegExp(`(\\[${EXTENSION_NAME}\\][\\s\\S]*?)version = "[^"]+"`)
  58. const updatedToml = extensionsToml.replace(versionRegex, `$1version = "${cleanVersion}"`)
  59. if (updatedToml === extensionsToml) {
  60. throw new Error(`Failed to update version in extensions.toml - pattern not found`)
  61. }
  62. await Bun.write(extensionsTomlPath, updatedToml)
  63. await $`git add extensions.toml`
  64. const commitMessage = `Update ${EXTENSION_NAME} to v${cleanVersion}`
  65. await $`git commit -m ${commitMessage}`
  66. console.log(`✅ Changes committed`)
  67. // Delete any existing branches for opencode updates
  68. console.log(`🔍 Checking for existing branches...`)
  69. const branches = await $`git ls-remote --heads https://x-access-token:${token}@github.com/${FORK_REPO}.git`.text()
  70. const branchPattern = `refs/heads/update-${EXTENSION_NAME}-`
  71. const oldBranches = branches
  72. .split("\n")
  73. .filter((line) => line.includes(branchPattern))
  74. .map((line) => line.split("refs/heads/")[1])
  75. .filter(Boolean)
  76. if (oldBranches.length > 0) {
  77. console.log(`🗑️ Found ${oldBranches.length} old branch(es), deleting...`)
  78. for (const branch of oldBranches) {
  79. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git --delete ${branch}`
  80. console.log(`✅ Deleted branch ${branch}`)
  81. }
  82. }
  83. console.log(`🚀 Pushing to fork...`)
  84. await $`git push https://x-access-token:${token}@github.com/${FORK_REPO}.git ${branchName}`
  85. console.log(`📬 Creating pull request...`)
  86. const prUrl =
  87. await $`gh pr create --repo ${UPSTREAM_REPO} --base main --head ${FORK_REPO.split("/")[0]}:${branchName} --title "Update ${EXTENSION_NAME} to v${cleanVersion}" --body "Updating OpenCode extension to v${cleanVersion}"`.text()
  88. console.log(`✅ Pull request created: ${prUrl}`)
  89. console.log(`🎉 Done!`)
  90. }
  91. main().catch((err) => {
  92. console.error("❌ Error:", err.message)
  93. process.exit(1)
  94. })