changelog.ts 9.8 KB

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