Merge pull request #20165 from ivangonzalezacuna/bugfix/gitlab-api-archived-projects

bugfix(catalog-backend-module-gitlab): Make sure archived repos are skipped
This commit is contained in:
Patrik Oldsberg
2023-09-29 11:02:10 +02:00
committed by GitHub
4 changed files with 95 additions and 9 deletions
@@ -237,14 +237,6 @@ describe('GitlabDiscoveryProcessor', () => {
web_url: 'https://gitlab.fake/3',
path_with_namespace: '3',
},
{
id: 4,
archived: true, // ARCHIVED
default_branch: 'master',
last_activity_at: '2021-08-05T11:03:05.774Z',
web_url: 'https://gitlab.fake/4',
path_with_namespace: '4',
},
{
id: 5,
archived: false,
@@ -120,6 +120,14 @@ function setupFakeHasFileEndpoint(srv: SetupServer, apiBaseUrl: string) {
);
}
function setupFakeURLReturnEndpoint(srv: SetupServer, url: string) {
srv.use(
rest.get(url, (req, res, ctx) => {
return res(ctx.json([{ endpoint: req.url.toString() }]));
}),
);
}
describe('GitLabClient', () => {
describe('isSelfManaged', () => {
it('returns true if self managed instance', () => {
@@ -933,3 +941,84 @@ describe('hasFile', () => {
expect(hasFile).toBe(false);
});
});
describe('pagedRequest search params', () => {
beforeEach(() => {
// setup fake paginated endpoint with 4 pages each returning one item
setupFakeURLReturnEndpoint(server, FAKE_PAGED_URL);
});
it('no search params provided', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{ endpoint: 'https://example.com/api/v4/some-endpoint' },
]);
});
it('defined numeric search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ page: 1, per_page: 50 },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint: 'https://example.com/api/v4/some-endpoint?page=1&per_page=50',
},
]);
});
it('defined string search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ test: 'value', empty: '' },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint: 'https://example.com/api/v4/some-endpoint?test=value',
},
]);
});
it('defined boolean search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ active: true, archived: false },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint:
'https://example.com/api/v4/some-endpoint?active=true&archived=false',
},
]);
});
});
@@ -309,7 +309,7 @@ export class GitLabClient {
): Promise<PagedResponse<T>> {
const request = new URL(`${this.config.apiBaseUrl}${endpoint}`);
for (const key in options) {
if (options[key]) {
if (options[key] !== undefined && options[key] !== '') {
request.searchParams.append(key, options[key]!.toString());
}
}