publish-start.ts 8.2 KB

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