publish-start.ts 8.1 KB

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