publish-start.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencode } from "@opencode-ai/sdk"
  4. import { Script } from "@opencode-ai/script"
  5. const notes = [] as string[]
  6. console.log("=== publishing ===\n")
  7. if (!Script.preview) {
  8. const previous = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  9. .then((res) => {
  10. if (!res.ok) throw new Error(res.statusText)
  11. return res.json()
  12. })
  13. .then((data: any) => data.version)
  14. const log =
  15. await $`git log v${previous}..HEAD --oneline --format="%h %s" -- packages/opencode packages/sdk packages/plugin packages/tauri packages/desktop`.text()
  16. const commits = log.split("\n").filter((line) => line && !line.match(/^\w+ (ignore:|test:|chore:|ci:)/i))
  17. const team = [
  18. "actions-user",
  19. "opencode",
  20. "rekram1-node",
  21. "thdxr",
  22. "kommander",
  23. "jayair",
  24. "fwang",
  25. "adamdotdevin",
  26. "iamdavidhill",
  27. "opencode-agent[bot]",
  28. ]
  29. async function generateChangelog() {
  30. const opencode = await createOpencode()
  31. const session = await opencode.client.session.create()
  32. console.log("generating changelog since " + previous)
  33. const raw = await opencode.client.session
  34. .prompt({
  35. path: {
  36. id: session.data!.id,
  37. },
  38. body: {
  39. model: {
  40. providerID: "opencode",
  41. modelID: "gemini-3-flash",
  42. },
  43. parts: [
  44. {
  45. type: "text",
  46. text: `
  47. Analyze these commits and generate a changelog of all notable user facing changes, grouped by area.
  48. Each commit below includes:
  49. - [author: username] showing the GitHub username of the commit author
  50. - [areas: ...] showing which areas of the codebase were modified
  51. Commits between ${previous} and HEAD:
  52. ${commits.join("\n")}
  53. Group the changes into these categories based on the [areas: ...] tags (omit any category with no changes):
  54. - **TUI**: Changes to "opencode" area (the terminal/CLI interface)
  55. - **Desktop**: Changes to "desktop" or "tauri" areas (the desktop application)
  56. - **SDK**: Changes to "sdk" or "plugin" areas (the SDK and plugin system)
  57. - **Extensions**: Changes to "extensions/zed", "extensions/vscode", or "github" areas (editor extensions and GitHub Action)
  58. - **Other**: Any user-facing changes that don't fit the above categories
  59. Excluded areas (omit these entirely unless they contain user-facing changes like refactors that may affect behavior):
  60. - "nix", "infra", "script" - CI/build infrastructure
  61. - "ui", "docs", "web", "console", "enterprise", "function", "util", "identity", "slack" - internal packages
  62. Rules:
  63. - Use the [areas: ...] tags to determine the correct category. If a commit touches multiple areas, put it in the most relevant user-facing category.
  64. - ONLY include commits that have user-facing impact. Omit purely internal changes (CI, build scripts, internal tooling).
  65. - However, DO include refactors that touch user-facing code - refactors can introduce bugs or change behavior.
  66. - Do NOT make general statements about "improvements", be very specific about what was changed.
  67. - For commits that are already well-written and descriptive, avoid rewording them. Simply capitalize the first letter, fix any misspellings, and ensure proper English grammar.
  68. - DO NOT read any other commits than the ones listed above (THIS IS IMPORTANT TO AVOID DUPLICATING THINGS IN OUR CHANGELOG).
  69. - If a commit was made and then reverted do not include it in the changelog. If the commits only include a revert but not the original commit, then include the revert in the changelog.
  70. - Omit categories that have no changes.
  71. - For community contributors: if the [author: username] is NOT in the team list, add (@username) at the end of the changelog entry. This is REQUIRED for all non-team contributors.
  72. - The team members are: ${team.join(", ")}. Do NOT add @ mentions for team members.
  73. IMPORTANT: ONLY return the grouped changelog, do not include any other information. Do not include a preamble like "Based on my analysis..." or "Here is the changelog..."
  74. <example>
  75. ## TUI
  76. - Added experimental support for the Ty language server (@OpeOginni)
  77. - Added /fork slash command for keyboard-friendly session forking (@ariane-emory)
  78. - Increased retry attempts for failed requests
  79. - Fixed model validation before executing slash commands (@devxoul)
  80. ## Desktop
  81. - Added shell mode support
  82. - Fixed prompt history navigation and optimistic prompt duplication
  83. - Disabled pinch-to-zoom on Linux (@Brendonovich)
  84. ## Extensions
  85. - Added OIDC_BASE_URL support for custom GitHub App installations (@elithrar)
  86. </example>
  87. `,
  88. },
  89. ],
  90. },
  91. })
  92. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  93. opencode.server.close()
  94. return raw
  95. }
  96. const timeout = new Promise<null>((resolve) => setTimeout(() => resolve(null), 120_000))
  97. const raw = await Promise.race([generateChangelog(), timeout])
  98. if (raw) {
  99. for (const line of raw.split("\n")) {
  100. if (line.startsWith("- ")) {
  101. notes.push(line)
  102. }
  103. }
  104. console.log("---- Generated Changelog ----")
  105. console.log(notes.join("\n"))
  106. console.log("-----------------------------")
  107. } else {
  108. console.log("Changelog generation timed out, using raw commits")
  109. for (const commit of commits) {
  110. const message = commit.replace(/^\w+ /, "")
  111. notes.push(`- ${message}`)
  112. }
  113. }
  114. const compare =
  115. await $`gh api "/repos/sst/opencode/compare/v${previous}...HEAD" --jq '.commits[] | {login: .author.login, message: .commit.message}'`.text()
  116. const contributors = new Map<string, string[]>()
  117. for (const line of compare.split("\n").filter(Boolean)) {
  118. const { login, message } = JSON.parse(line) as { login: string | null; message: string }
  119. const title = message.split("\n")[0] ?? ""
  120. if (title.match(/^(ignore:|test:|chore:|ci:|release:)/i)) continue
  121. if (login && !team.includes(login)) {
  122. if (!contributors.has(login)) contributors.set(login, [])
  123. contributors.get(login)?.push(title)
  124. }
  125. }
  126. if (contributors.size > 0) {
  127. notes.push("")
  128. notes.push(`**Thank you to ${contributors.size} community contributor${contributors.size > 1 ? "s" : ""}:**`)
  129. for (const [username, userCommits] of contributors) {
  130. notes.push(`- @${username}:`)
  131. for (const commit of userCommits) {
  132. notes.push(` - ${commit}`)
  133. }
  134. }
  135. }
  136. }
  137. const pkgjsons = await Array.fromAsync(
  138. new Bun.Glob("**/package.json").scan({
  139. absolute: true,
  140. }),
  141. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  142. for (const file of pkgjsons) {
  143. let pkg = await Bun.file(file).text()
  144. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  145. console.log("updated:", file)
  146. await Bun.file(file).write(pkg)
  147. }
  148. const extensionToml = new URL("../packages/extensions/zed/extension.toml", import.meta.url).pathname
  149. let toml = await Bun.file(extensionToml).text()
  150. toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
  151. toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
  152. console.log("updated:", extensionToml)
  153. await Bun.file(extensionToml).write(toml)
  154. await $`bun install`
  155. console.log("\n=== opencode ===\n")
  156. await import(`../packages/opencode/script/publish.ts`)
  157. console.log("\n=== sdk ===\n")
  158. await import(`../packages/sdk/js/script/publish.ts`)
  159. console.log("\n=== plugin ===\n")
  160. await import(`../packages/plugin/script/publish.ts`)
  161. const dir = new URL("..", import.meta.url).pathname
  162. process.chdir(dir)
  163. let output = `version=${Script.version}\n`
  164. if (!Script.preview) {
  165. await $`git commit -am "release: v${Script.version}"`
  166. await $`git tag v${Script.version}`
  167. await $`git fetch origin`
  168. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  169. await $`git push origin HEAD --tags --no-verify --force-with-lease`
  170. await new Promise((resolve) => setTimeout(resolve, 5_000))
  171. await $`gh release create v${Script.version} -d --title "v${Script.version}" --notes ${notes.join("\n") || "No notable changes"} ./packages/opencode/dist/*.zip ./packages/opencode/dist/*.tar.gz`
  172. const release = await $`gh release view v${Script.version} --json id,tagName`.json()
  173. output += `release=${release.id}\n`
  174. output += `tag=${release.tagName}\n`
  175. }
  176. if (process.env.GITHUB_OUTPUT) {
  177. await Bun.write(process.env.GITHUB_OUTPUT, output)
  178. }