changelog.ts 10 KB

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