Skip to content

Commit f86bc99

Browse files
test(backend): add tests for Bitbucket shouldExcludeRepo functions
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b471209 commit f86bc99

File tree

2 files changed

+175
-2
lines changed

2 files changed

+175
-2
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { expect, test, describe } from 'vitest';
2+
import { cloudShouldExcludeRepo, serverShouldExcludeRepo, BitbucketRepository } from './bitbucket';
3+
import { BitbucketConnectionConfig } from '@sourcebot/schemas/v3/bitbucket.type';
4+
import { SchemaRepository as CloudRepository } from '@coderabbitai/bitbucket/cloud/openapi';
5+
import { SchemaRestRepository as ServerRepository } from '@coderabbitai/bitbucket/server/openapi';
6+
7+
const makeCloudRepo = (overrides: Partial<CloudRepository> = {}): BitbucketRepository => ({
8+
type: 'repository',
9+
full_name: 'myworkspace/my-repo',
10+
project: { type: 'project', key: 'PROJ' },
11+
is_private: false,
12+
...overrides,
13+
} as CloudRepository);
14+
15+
const makeServerRepo = (overrides: Partial<ServerRepository> = {}): BitbucketRepository => ({
16+
slug: 'my-repo',
17+
project: { key: 'PROJ' },
18+
archived: false,
19+
...overrides,
20+
} as ServerRepository);
21+
22+
const baseConfig: BitbucketConnectionConfig = {
23+
type: 'bitbucket',
24+
deploymentType: 'cloud',
25+
};
26+
27+
describe('cloudShouldExcludeRepo', () => {
28+
test('returns false when no exclusions are configured', () => {
29+
expect(cloudShouldExcludeRepo(makeCloudRepo(), baseConfig)).toBe(false);
30+
});
31+
32+
test('returns false when exclude.repos is empty', () => {
33+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
34+
...baseConfig,
35+
exclude: { repos: [] },
36+
})).toBe(false);
37+
});
38+
39+
test('returns true when repo matches exclude.repos exactly', () => {
40+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
41+
...baseConfig,
42+
exclude: { repos: ['myworkspace/PROJ/my-repo'] },
43+
})).toBe(true);
44+
});
45+
46+
test('returns false when exclude.repos does not match', () => {
47+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
48+
...baseConfig,
49+
exclude: { repos: ['myworkspace/PROJ/other-repo'] },
50+
})).toBe(false);
51+
});
52+
53+
test('returns true when repo matches a glob pattern in exclude.repos', () => {
54+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
55+
...baseConfig,
56+
exclude: { repos: ['myworkspace/PROJ/*'] },
57+
})).toBe(true);
58+
});
59+
60+
test('returns true when repo matches a workspace-level glob in exclude.repos', () => {
61+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
62+
...baseConfig,
63+
exclude: { repos: ['myworkspace/**'] },
64+
})).toBe(true);
65+
});
66+
67+
test('returns false when exclude.forks is true but repo is not a fork', () => {
68+
expect(cloudShouldExcludeRepo(makeCloudRepo(), {
69+
...baseConfig,
70+
exclude: { forks: true },
71+
})).toBe(false);
72+
});
73+
74+
test('returns true when exclude.forks is true and repo is a fork', () => {
75+
const forkedRepo = makeCloudRepo({ parent: { type: 'repository' } as CloudRepository });
76+
expect(cloudShouldExcludeRepo(forkedRepo, {
77+
...baseConfig,
78+
exclude: { forks: true },
79+
})).toBe(true);
80+
});
81+
82+
test('returns false when exclude.forks is false and repo is a fork', () => {
83+
const forkedRepo = makeCloudRepo({ parent: { type: 'repository' } as CloudRepository });
84+
expect(cloudShouldExcludeRepo(forkedRepo, {
85+
...baseConfig,
86+
exclude: { forks: false },
87+
})).toBe(false);
88+
});
89+
});
90+
91+
describe('serverShouldExcludeRepo', () => {
92+
const serverConfig: BitbucketConnectionConfig = {
93+
type: 'bitbucket',
94+
deploymentType: 'server',
95+
url: 'https://bitbucket.example.com',
96+
};
97+
98+
test('returns false when no exclusions are configured', () => {
99+
expect(serverShouldExcludeRepo(makeServerRepo(), serverConfig)).toBe(false);
100+
});
101+
102+
test('returns false when exclude.repos is empty', () => {
103+
expect(serverShouldExcludeRepo(makeServerRepo(), {
104+
...serverConfig,
105+
exclude: { repos: [] },
106+
})).toBe(false);
107+
});
108+
109+
test('returns true when repo matches exclude.repos exactly', () => {
110+
expect(serverShouldExcludeRepo(makeServerRepo(), {
111+
...serverConfig,
112+
exclude: { repos: ['PROJ/my-repo'] },
113+
})).toBe(true);
114+
});
115+
116+
test('returns false when exclude.repos does not match', () => {
117+
expect(serverShouldExcludeRepo(makeServerRepo(), {
118+
...serverConfig,
119+
exclude: { repos: ['PROJ/other-repo'] },
120+
})).toBe(false);
121+
});
122+
123+
test('returns true when repo matches a glob pattern in exclude.repos', () => {
124+
expect(serverShouldExcludeRepo(makeServerRepo(), {
125+
...serverConfig,
126+
exclude: { repos: ['PROJ/*'] },
127+
})).toBe(true);
128+
});
129+
130+
test('returns false when exclude.archived is true but repo is not archived', () => {
131+
expect(serverShouldExcludeRepo(makeServerRepo({ archived: false }), {
132+
...serverConfig,
133+
exclude: { archived: true },
134+
})).toBe(false);
135+
});
136+
137+
test('returns true when exclude.archived is true and repo is archived', () => {
138+
expect(serverShouldExcludeRepo(makeServerRepo({ archived: true }), {
139+
...serverConfig,
140+
exclude: { archived: true },
141+
})).toBe(true);
142+
});
143+
144+
test('returns false when exclude.archived is false and repo is archived', () => {
145+
expect(serverShouldExcludeRepo(makeServerRepo({ archived: true }), {
146+
...serverConfig,
147+
exclude: { archived: false },
148+
})).toBe(false);
149+
});
150+
151+
test('returns false when exclude.forks is true but repo is not a fork', () => {
152+
expect(serverShouldExcludeRepo(makeServerRepo(), {
153+
...serverConfig,
154+
exclude: { forks: true },
155+
})).toBe(false);
156+
});
157+
158+
test('returns true when exclude.forks is true and repo is a fork', () => {
159+
const forkedRepo = makeServerRepo({ origin: { slug: 'original-repo' } as ServerRepository });
160+
expect(serverShouldExcludeRepo(forkedRepo, {
161+
...serverConfig,
162+
exclude: { forks: true },
163+
})).toBe(true);
164+
});
165+
166+
test('returns false when exclude.forks is false and repo is a fork', () => {
167+
const forkedRepo = makeServerRepo({ origin: { slug: 'original-repo' } as ServerRepository });
168+
expect(serverShouldExcludeRepo(forkedRepo, {
169+
...serverConfig,
170+
exclude: { forks: false },
171+
})).toBe(false);
172+
});
173+
});

packages/backend/src/bitbucket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ async function cloudGetRepos(client: BitbucketClient, repoList: string[]): Promi
345345
};
346346
}
347347

348-
function cloudShouldExcludeRepo(repo: BitbucketRepository, config: BitbucketConnectionConfig): boolean {
348+
export function cloudShouldExcludeRepo(repo: BitbucketRepository, config: BitbucketConnectionConfig): boolean {
349349
const cloudRepo = repo as CloudRepository;
350350
let reason = '';
351351
const [workspace, repoSlug] = cloudRepo.full_name!.split('/');
@@ -553,7 +553,7 @@ async function serverGetRepos(client: BitbucketClient, repoList: string[]): Prom
553553
};
554554
}
555555

556-
function serverShouldExcludeRepo(repo: BitbucketRepository, config: BitbucketConnectionConfig): boolean {
556+
export function serverShouldExcludeRepo(repo: BitbucketRepository, config: BitbucketConnectionConfig): boolean {
557557
const serverRepo = repo as ServerRepository;
558558

559559
const projectName = serverRepo.project!.key;

0 commit comments

Comments
 (0)