@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
ts 126 lines 4.7 KB
Raw
1 import { execFileSync } from 'node:child_process';
2 import { readFileSync, writeFileSync } from 'node:fs';
3
4 const path = 'ts/packages/cli/CHANGELOG.md';
5 const tagPrefix = '@composio/cli@';
6 const stableTag = /^@composio\/cli@(\d+\.\d+\.\d+)$/;
7 const header = '# @composio/cli\n\n## Unreleased';
8
9 type Note = { heading: string; text: string };
10
11 function splitChangelog(markdown: string) {
12 const boundary = markdown.indexOf('\n## ', header.length);
13 return {
14 unreleased: markdown.slice(header.length, boundary < 0 ? undefined : boundary),
15 history: boundary < 0 ? '' : markdown.slice(boundary + 1),
16 };
17 }
18
19 function pendingNotes(markdown: string): Note[] {
20 // Older stable tags predate the Unreleased section.
21 if (!markdown.startsWith(header + '\n')) return [];
22
23 const notes: Note[] = [];
24 let heading = '';
25 const { unreleased } = splitChangelog(markdown);
26 for (const block of unreleased.split(/(?=^### |^- )/m)) {
27 const text = block.trimEnd();
28 if (!text) continue;
29 if (text.startsWith('### ') && !text.includes('\n')) {
30 heading = text;
31 } else if (text.startsWith('- ')) {
32 notes.push({ heading, text });
33 } else {
34 throw new Error(`Unexpected Unreleased content: ${text}`);
35 }
36 }
37 return notes;
38 }
39
40 function withoutNotes(notes: Note[], removed: Note[]): Note[] {
41 const counts = new Map<string, number>();
42 for (const { text } of removed) counts.set(text, (counts.get(text) ?? 0) + 1);
43
44 return notes.filter(({ text }) => {
45 const count = counts.get(text) ?? 0;
46 if (count === 0) return true;
47 counts.set(text, count - 1);
48 return false;
49 });
50 }
51
52 function renderNotes(notes: Note[]): string {
53 const blocks: string[] = [];
54 let previousHeading = '';
55 for (const { heading, text } of notes) {
56 if (heading && heading !== previousHeading) blocks.push(heading);
57 blocks.push(text);
58 previousHeading = heading;
59 }
60 return blocks.join('\n\n');
61 }
62
63 export function rollChangelog(
64 current: string,
65 releases: { tag: string; markdown: string }[],
66 repository: string
67 ): string {
68 if (!current.startsWith(header + '\n')) {
69 throw new Error('CLI changelog is missing its Unreleased section');
70 }
71 if (releases.length < 2) return current;
72
73 let { history } = splitChangelog(current);
74 let pending = pendingNotes(current);
75 // Version sections contain both notes and standalone text such as compare links.
76 const previouslyShipped = new Set(
77 history
78 .split(/(?=^#{2,3} |^- |^\n\S)/m)
79 .filter(block => block.startsWith('- '))
80 .map(block => block.trimEnd())
81 );
82
83 for (let index = 1; index < releases.length; index++) {
84 const previous = releases[index - 1];
85 const release = releases[index];
86 const version = release.tag.slice(tagPrefix.length);
87 for (const { text } of pendingNotes(previous.markdown)) previouslyShipped.add(text);
88 const releaseNotes = pendingNotes(release.markdown);
89 const shipped = releaseNotes.filter(({ text }) => !previouslyShipped.has(text));
90 pending = withoutNotes(pending, releaseNotes);
91 const notes = renderNotes(shipped);
92 const compare = `[Compare with ${previous.tag.slice(tagPrefix.length)}](https://github.com/${repository}/compare/${encodeURIComponent(previous.tag)}...${encodeURIComponent(release.tag)})`;
93 history = [`## ${version}`, notes, compare, history].filter(Boolean).join('\n\n');
94 }
95
96 return [header, renderNotes(pending), history].filter(Boolean).join('\n\n');
97 }
98
99 function git(...args: string[]): string {
100 return execFileSync('git', args, { encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 });
101 }
102
103 if (import.meta.main) {
104 const repository = process.env.GH_REPO ?? 'ComposioHQ/composio';
105 const current = readFileSync(path, 'utf8');
106 const version = splitChangelog(current).history.match(/^## (\d+\.\d+\.\d+)\n/)?.[1];
107 if (!version) throw new Error('CLI changelog is missing its latest stable version');
108
109 // gh supplies published stable releases. Sort the matching, checked-out tags
110 // numerically and use only the releases after the last documented version.
111 const published = new Set(process.argv.slice(2).filter(tag => stableTag.test(tag)));
112 const tags = git('tag', '--list', '--sort=version:refname', '@composio/cli@*')
113 .trim()
114 .split('\n')
115 .filter(tag => published.has(tag));
116 if (tags.length !== published.size)
117 throw new Error('Published CLI release tags are missing locally');
118 const baseline = tags.indexOf(tagPrefix + version);
119 if (baseline === -1) throw new Error(`No published CLI release found for ${version}`);
120 // A manually built beta may come from a release branch outside next.
121 const releases = tags.slice(baseline).map(tag => ({
122 tag,
123 markdown: git('show', `refs/tags/${tag}:${path}`),
124 }));
125 writeFileSync(path, rollChangelog(current, releases, repository));
126 }