@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 244 lines 9.74 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { Effect, Option } from 'effect';
3 import { eligibleApproval, readReleasePull, trustedIssue } from '../scripts/sdk-release/approval';
4 import { authorizeSourceTag } from '../scripts/sdk-release/batch';
5 import {
6 APP_SLUG,
7 base,
8 fakeController,
9 head,
10 merge,
11 type ControllerOptions,
12 } from './sdk-release-controller-fixture';
13 import { runRelease } from './sdk-release-harness';
14
15 const tag = (fetcher: ReturnType<typeof fakeController>['fetcher']) =>
16 runRelease(
17 Effect.flatMap(readReleasePull(9), ({ pull, batch }) =>
18 authorizeSourceTag(pull, batch, APP_SLUG)
19 ),
20 fetcher
21 );
22 const pull = 'https://github.com/ComposioHQ/composio/pull/9';
23 const tagged = { _tag: 'Tagged', tag: 'sdk-release/8' } as const;
24 const tagWrite = {
25 method: 'POST',
26 path: '/git/refs',
27 body: { ref: 'refs/tags/sdk-release/8', sha: head },
28 };
29 const commentBodies = (writes: ReadonlyArray<{ body: unknown }>) =>
30 writes.map(write => (write.body as { body: string }).body);
31
32 describe('release approval rule', () => {
33 const review = { user: 'reviewer', state: 'APPROVED', commitId: head, permission: 'write' };
34
35 test.each([
36 ['a writer who is neither author nor requester', [review], true],
37 [
38 'a writer, despite a reader requesting changes',
39 [review, { ...review, user: 'observer', state: 'CHANGES_REQUESTED', permission: 'read' }],
40 true,
41 ],
42 ['the requester', [{ ...review, user: 'requester' }], false],
43 ['an admin requester', [{ ...review, user: 'requester', permission: 'admin' }], true],
44 [
45 'a requester with maintain permission',
46 [{ ...review, user: 'requester', permission: 'maintain' }],
47 false,
48 ],
49 [
50 'an admin requester approving an older commit',
51 [{ ...review, user: 'requester', permission: 'admin', commitId: base }],
52 false,
53 ],
54 [
55 'an admin requester whose approval was dismissed',
56 [{ ...review, user: 'requester', permission: 'admin', state: 'DISMISSED' }],
57 false,
58 ],
59 [
60 'an admin requester while another writer requests changes',
61 [
62 { ...review, user: 'requester', permission: 'admin' },
63 { ...review, user: 'maintainer', state: 'CHANGES_REQUESTED' },
64 ],
65 false,
66 ],
67 ['the author', [{ ...review, user: 'author' }], false],
68 ['an admin author', [{ ...review, user: 'author', permission: 'admin' }], false],
69 ['an approval of an older commit', [{ ...review, commitId: base }], false],
70 ['a reader', [{ ...review, permission: 'read' }], false],
71 ['an approval later overridden', [review, { ...review, state: 'CHANGES_REQUESTED' }], false],
72 [
73 'an approval while another writer requests changes',
74 [review, { ...review, user: 'maintainer', state: 'CHANGES_REQUESTED' }],
75 false,
76 ],
77 ])('%s → %p', (_name, reviews, expected) => {
78 expect(eligibleApproval(reviews, 'author', 'requester', head)).toBe(expected);
79 });
80 });
81
82 describe('source tag', () => {
83 test('an admin requester can approve the exact bot-authored head', async () => {
84 const { fetcher, writes } = fakeController({
85 reviewer: 'requester',
86 reviewerPermission: 'admin',
87 });
88 expect(await tag(fetcher)).toEqual(tagged);
89 expect(writes).toEqual([tagWrite]);
90 });
91
92 test.each(['merge', 'squash'] as const)(
93 'a merged, approved release tags the reviewed head (%s merge)',
94 async mergeKind => {
95 const { fetcher, writes } = fakeController({ mergeKind });
96 expect(await tag(fetcher)).toEqual(tagged);
97 expect(writes).toEqual([tagWrite]);
98 }
99 );
100
101 test('required checks on a later page of more than 100 check runs still tag the head', async () => {
102 const { fetcher, writes } = fakeController();
103 const unrelated = Array.from({ length: 100 }, (_, index) => ({
104 id: 100 + index,
105 name: `matrix job ${index}`,
106 head_sha: head,
107 app: { slug: 'github-actions' },
108 conclusion: 'success',
109 }));
110 const paged: typeof fetcher = (input, init) => {
111 const url = new URL(String(input));
112 if (!url.pathname.endsWith('/check-runs')) return fetcher(input, init);
113 return (url.searchParams.get('page') ?? '1') === '1'
114 ? Promise.resolve(Response.json({ total_count: 103, check_runs: unrelated }))
115 : fetcher(input, init);
116 };
117 expect(await tag(paged)).toEqual(tagged);
118 expect(writes).toEqual([tagWrite]);
119 });
120
121 test("a deleted account's review is skipped and the approved head is still tagged", async () => {
122 const { fetcher, writes } = fakeController({ deletedReviewer: true });
123 expect(await tag(fetcher)).toEqual(tagged);
124 expect(writes).toEqual([tagWrite]);
125 });
126
127 test("only the release App's plan may name the request issue the status job writes to", async () => {
128 const issueOf = (options: Parameters<typeof fakeController>[0]) => {
129 const { fetcher } = fakeController(options);
130 return runRelease(
131 Effect.flatMap(readReleasePull(9), ({ pull, batch }) =>
132 trustedIssue(pull, batch, APP_SLUG)
133 ),
134 fetcher
135 );
136 };
137 expect(await issueOf({})).toEqual(Option.some(8));
138 expect(await issueOf({ closedUnmerged: true, headAuthor: 'writer' })).toEqual(Option.none());
139 expect(await issueOf({ branchSetter: 'writer' })).toEqual(Option.none());
140 });
141
142 test('a beta is tagged without either docs check, which never run on a plan-only PR', async () => {
143 const { fetcher, writes } = fakeController({
144 beta: true,
145 docs: 'missing',
146 rerunFailed: 'typescript-check',
147 });
148 expect(await tag(fetcher)).toEqual(tagged);
149 expect(writes).toEqual([tagWrite]);
150 });
151
152 test('a stable release no longer needs the pull-request docs check, which skips release heads', async () => {
153 const { fetcher, writes } = fakeController({ rerunFailed: 'typescript-check' });
154 expect(await tag(fetcher)).toEqual(tagged);
155 expect(writes).toEqual([tagWrite]);
156 });
157
158 test('a release PR closed without merging is abandoned and creates no tag', async () => {
159 const { fetcher, writes } = fakeController({ closedUnmerged: true });
160 expect(await tag(fetcher)).toEqual({ _tag: 'Abandoned', pull });
161 expect(writes).toEqual([]);
162 });
163
164 test.each<[string, ControllerOptions, string]>([
165 ['the requester approves', { reviewer: 'requester' }, 'lacks an approval'],
166 [
167 'the admin requester approves an older head',
168 { reviewer: 'requester', reviewerPermission: 'admin', oldReview: true },
169 'lacks an approval',
170 ],
171 [
172 'the admin requester approves but the candidate build failed',
173 { reviewer: 'requester', reviewerPermission: 'admin', failedCandidate: true },
174 'SDK candidate (typescript)',
175 ],
176 ['the approval is for an older head', { oldReview: true }, 'lacks an approval'],
177 ['a human pushed the head', { headAuthor: 'writer' }, 'release App commit'],
178 [
179 'a human set the branch to a head that names the App as author',
180 { branchSetter: 'writer' },
181 'release App commit',
182 ],
183 ['the head commit is unverified', { unverifiedHead: true }, 'release App commit'],
184 ['the head is not on the planned source', { headParent: merge }, 'release App commit'],
185 ['the candidate build failed', { failedCandidate: true }, 'SDK candidate (typescript)'],
186 ['the smoke test failed', { failedSmoke: true }, 'SDK candidate smoke (typescript)'],
187 ['the docs check failed', { docs: 'failure' }, 'SDK candidate docs'],
188 ['the docs check is pending', { docs: 'pending' }, 'SDK candidate docs'],
189 ['the docs check is missing', { docs: 'missing' }, 'SDK candidate docs'],
190 [
191 'a re-run of a passed check failed',
192 { rerunFailed: 'SDK candidate (typescript)' },
193 'SDK candidate (typescript)',
194 ],
195 ])(
196 'when %s, the release is refused, no tag is created and the PR is told to revert',
197 async (_name, options, reason) => {
198 const { fetcher, writes } = fakeController(options);
199 expect(await tag(fetcher)).toEqual({
200 _tag: 'Refused',
201 pull,
202 reason: expect.stringContaining(reason),
203 });
204 expect(writes.map(write => write.path)).toEqual(['/issues/9/comments']);
205 for (const body of commentBodies(writes)) {
206 expect(body).toContain(reason);
207 expect(body).toContain('Revert it on `next`');
208 }
209 }
210 );
211
212 test('a GitHub error during the gate fails the job for a re-run and refuses nothing', async () => {
213 const { fetcher, writes } = fakeController();
214 const unavailable: typeof fetcher = (input, init) =>
215 new URL(String(input)).pathname.includes('/check-runs')
216 ? Promise.resolve(new Response('Bad Gateway', { status: 502 }))
217 : fetcher(input, init);
218 await expect(tag(unavailable)).rejects.toThrow('(502)');
219 expect(writes).toEqual([]);
220 });
221
222 test('a pull without `merged` is a contract failure, not an abandoned release', async () => {
223 const { fetcher, writes } = fakeController();
224 const withoutMerged: typeof fetcher = async (input, init) => {
225 const response = await fetcher(input, init);
226 if (new URL(String(input)).pathname !== '/repos/ComposioHQ/composio/pulls/9') return response;
227 const { merged: _merged, ...rest } = (await response.json()) as Record<string, unknown>;
228 return Response.json(rest);
229 };
230 await expect(tag(withoutMerged)).rejects.toThrow(
231 /GET \/repos\/ComposioHQ\/composio\/pulls\/9[\s\S]*merged/
232 );
233 expect(writes).toEqual([]);
234 });
235
236 test('an existing matching tag is reused and a tag on another commit is refused', async () => {
237 const matching = fakeController({ existingTag: head });
238 expect(await tag(matching.fetcher)).toEqual(tagged);
239 expect(matching.writes).toEqual([]);
240 const moved = fakeController({ existingTag: merge });
241 await expect(tag(moved.fetcher)).rejects.toThrow('another source');
242 expect(moved.writes).toEqual([]);
243 });
244 });