changelog.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencode } from "@opencode-ai/sdk"
  4. import { parseArgs } from "util"
  5. export const team = [
  6. "actions-user",
  7. "opencode",
  8. "rekram1-node",
  9. "thdxr",
  10. "kommander",
  11. "jayair",
  12. "fwang",
  13. "adamdotdevin",
  14. "iamdavidhill",
  15. "opencode-agent[bot]",
  16. ]
  17. export async function getLatestRelease() {
  18. return fetch("https://api.github.com/repos/sst/opencode/releases/latest")
  19. .then((res) => {
  20. if (!res.ok) throw new Error(res.statusText)
  21. return res.json()
  22. })
  23. .then((data: any) => data.tag_name.replace(/^v/, ""))
  24. }
  25. type Commit = {
  26. hash: string
  27. author: string | null
  28. message: string
  29. areas: Set<string>
  30. }
  31. export async function getCommits(from: string, to: string): Promise<Commit[]> {
  32. const fromRef = from.startsWith("v") ? from : `v${from}`
  33. const toRef = to === "HEAD" ? to : to.startsWith("v") ? to : `v${to}`
  34. // Get commit data with GitHub usernames from the API
  35. const compare =
  36. await $`gh api "/repos/sst/opencode/compare/${fromRef}...${toRef}" --jq '.commits[] | {sha: .sha, login: .author.login, message: .commit.message}'`.text()
  37. const commitData = new Map<string, { login: string | null; message: string }>()
  38. for (const line of compare.split("\n").filter(Boolean)) {
  39. const data = JSON.parse(line) as { sha: string; login: string | null; message: string }
  40. commitData.set(data.sha, { login: data.login, message: data.message.split("\n")[0] ?? "" })
  41. }
  42. // Get commits that touch the relevant packages
  43. const log =
  44. await $`git log ${fromRef}..${toRef} --oneline --format="%H" -- packages/opencode packages/sdk packages/plugin packages/desktop packages/app sdks/vscode packages/extensions github`.text()
  45. const hashes = log.split("\n").filter(Boolean)
  46. const commits: Commit[] = []
  47. for (const hash of hashes) {
  48. const data = commitData.get(hash)
  49. if (!data) continue
  50. const message = data.message
  51. if (message.match(/^(ignore:|test:|chore:|ci:|release:)/i)) continue
  52. const files = await $`git diff-tree --no-commit-id --name-only -r ${hash}`.text()
  53. const areas = new Set<string>()
  54. for (const file of files.split("\n").filter(Boolean)) {
  55. if (file.startsWith("packages/opencode/src/cli/cmd/tui/")) areas.add("tui")
  56. else if (file.startsWith("packages/opencode/")) areas.add("core")
  57. else if (file.startsWith("packages/desktop/src-tauri/")) areas.add("tauri")
  58. else if (file.startsWith("packages/desktop/")) areas.add("app")
  59. else if (file.startsWith("packages/app/")) areas.add("app")
  60. else if (file.startsWith("packages/sdk/")) areas.add("sdk")
  61. else if (file.startsWith("packages/plugin/")) areas.add("plugin")
  62. else if (file.startsWith("packages/extensions/")) areas.add("extensions/zed")
  63. else if (file.startsWith("sdks/vscode/")) areas.add("extensions/vscode")
  64. else if (file.startsWith("github/")) areas.add("github")
  65. }
  66. if (areas.size === 0) continue
  67. commits.push({
  68. hash: hash.slice(0, 7),
  69. author: data.login,
  70. message,
  71. areas,
  72. })
  73. }
  74. return commits
  75. }
  76. const sections = {
  77. core: "Core",
  78. tui: "TUI",
  79. app: "Desktop",
  80. tauri: "Desktop",
  81. sdk: "SDK",
  82. plugin: "SDK",
  83. "extensions/zed": "Extensions",
  84. "extensions/vscode": "Extensions",
  85. github: "Extensions",
  86. } as const
  87. function getSection(areas: Set<string>): string {
  88. // Priority order for multi-area commits
  89. const priority = ["core", "tui", "app", "tauri", "sdk", "plugin", "extensions/zed", "extensions/vscode", "github"]
  90. for (const area of priority) {
  91. if (areas.has(area)) return sections[area as keyof typeof sections]
  92. }
  93. return "Core"
  94. }
  95. async function summarizeCommit(
  96. opencode: Awaited<ReturnType<typeof createOpencode>>,
  97. sessionId: string,
  98. message: string,
  99. ): Promise<string> {
  100. console.log("summarizing commit:", message)
  101. const result = await opencode.client.session
  102. .prompt({
  103. path: { id: sessionId },
  104. body: {
  105. model: { providerID: "opencode", modelID: "claude-sonnet-4-5" },
  106. tools: {
  107. "*": false,
  108. },
  109. parts: [
  110. {
  111. type: "text",
  112. text: `Summarize this commit message for a changelog entry. Return ONLY a single line summary starting with a capital letter. Be concise but specific. If the commit message is already well-written, just clean it up (capitalize, fix typos, proper grammar). Do not include any prefixes like "fix:" or "feat:".
  113. Commit: ${message}`,
  114. },
  115. ],
  116. },
  117. signal: AbortSignal.timeout(120_000),
  118. })
  119. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text ?? message)
  120. return result.trim()
  121. }
  122. export async function generateChangelog(commits: Commit[], opencode: Awaited<ReturnType<typeof createOpencode>>) {
  123. const session = await opencode.client.session.create()
  124. const grouped = new Map<string, string[]>()
  125. for (const commit of commits) {
  126. const section = getSection(commit.areas)
  127. const summary = await summarizeCommit(opencode, session.data!.id, commit.message)
  128. const attribution = commit.author && !team.includes(commit.author) ? ` (@${commit.author})` : ""
  129. const entry = `- ${summary}${attribution}`
  130. if (!grouped.has(section)) grouped.set(section, [])
  131. grouped.get(section)!.push(entry)
  132. }
  133. const sectionOrder = ["Core", "TUI", "Desktop", "SDK", "Extensions"]
  134. const lines: string[] = []
  135. for (const section of sectionOrder) {
  136. const entries = grouped.get(section)
  137. if (!entries || entries.length === 0) continue
  138. lines.push(`## ${section}`)
  139. lines.push(...entries)
  140. }
  141. return lines
  142. }
  143. export async function getContributors(from: string, to: string) {
  144. const fromRef = from.startsWith("v") ? from : `v${from}`
  145. const toRef = to === "HEAD" ? to : to.startsWith("v") ? to : `v${to}`
  146. const compare =
  147. await $`gh api "/repos/sst/opencode/compare/${fromRef}...${toRef}" --jq '.commits[] | {login: .author.login, message: .commit.message}'`.text()
  148. const contributors = new Map<string, string[]>()
  149. for (const line of compare.split("\n").filter(Boolean)) {
  150. const { login, message } = JSON.parse(line) as { login: string | null; message: string }
  151. const title = message.split("\n")[0] ?? ""
  152. if (title.match(/^(ignore:|test:|chore:|ci:|release:)/i)) continue
  153. if (login && !team.includes(login)) {
  154. if (!contributors.has(login)) contributors.set(login, [])
  155. contributors.get(login)?.push(title)
  156. }
  157. }
  158. return contributors
  159. }
  160. export async function buildNotes(from: string, to: string) {
  161. const commits = await getCommits(from, to)
  162. if (commits.length === 0) {
  163. return []
  164. }
  165. console.log("generating changelog since " + from)
  166. const opencode = await createOpencode({ port: 5044 })
  167. const notes: string[] = []
  168. try {
  169. const lines = await generateChangelog(commits, opencode)
  170. notes.push(...lines)
  171. console.log("---- Generated Changelog ----")
  172. console.log(notes.join("\n"))
  173. console.log("-----------------------------")
  174. } catch (error) {
  175. if (error instanceof Error && error.name === "TimeoutError") {
  176. console.log("Changelog generation timed out, using raw commits")
  177. for (const commit of commits) {
  178. const attribution = commit.author && !team.includes(commit.author) ? ` (@${commit.author})` : ""
  179. notes.push(`- ${commit.message}${attribution}`)
  180. }
  181. } else {
  182. throw error
  183. }
  184. } finally {
  185. opencode.server.close()
  186. }
  187. const contributors = await getContributors(from, to)
  188. if (contributors.size > 0) {
  189. notes.push("")
  190. notes.push(`**Thank you to ${contributors.size} community contributor${contributors.size > 1 ? "s" : ""}:**`)
  191. for (const [username, userCommits] of contributors) {
  192. notes.push(`- @${username}:`)
  193. for (const c of userCommits) {
  194. notes.push(` - ${c}`)
  195. }
  196. }
  197. }
  198. return notes
  199. }
  200. // CLI entrypoint
  201. if (import.meta.main) {
  202. const { values } = parseArgs({
  203. args: Bun.argv.slice(2),
  204. options: {
  205. from: { type: "string", short: "f" },
  206. to: { type: "string", short: "t", default: "HEAD" },
  207. help: { type: "boolean", short: "h", default: false },
  208. },
  209. })
  210. if (values.help) {
  211. console.log(`
  212. Usage: bun script/changelog.ts [options]
  213. Options:
  214. -f, --from <version> Starting version (default: latest GitHub release)
  215. -t, --to <ref> Ending ref (default: HEAD)
  216. -h, --help Show this help message
  217. Examples:
  218. bun script/changelog.ts # Latest release to HEAD
  219. bun script/changelog.ts --from 1.0.200 # v1.0.200 to HEAD
  220. bun script/changelog.ts -f 1.0.200 -t 1.0.205
  221. `)
  222. process.exit(0)
  223. }
  224. const to = values.to!
  225. const from = values.from ?? (await getLatestRelease())
  226. console.log(`Generating changelog: v${from} -> ${to}\n`)
  227. const notes = await buildNotes(from, to)
  228. console.log("\n=== Final Notes ===")
  229. console.log(notes.join("\n"))
  230. }