|
| 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 | +}); |
0 commit comments