diff --git a/plugins/catalog-backend-module-gitlab/src/__testUtils__/handlers.ts b/plugins/catalog-backend-module-gitlab/src/__testUtils__/handlers.ts index 9af00d2564..241bc86df6 100644 --- a/plugins/catalog-backend-module-gitlab/src/__testUtils__/handlers.ts +++ b/plugins/catalog-backend-module-gitlab/src/__testUtils__/handlers.ts @@ -266,24 +266,34 @@ const httpProjectFindByIdDynamic = all_projects_response.map(project => { return res(ctx.json(all_projects_response.find(p => p.id === project.id))); }); }); -const httpProjectCatalogDynamic = all_projects_response.map(project => { - const path: string = project.path_with_namespace - ? project.path_with_namespace!.replace(/\//g, '%2F') - : `${project.path_with_namespace}%2F${project.name}`; - return rest.head( - `${apiBaseUrl}/projects/${path}/repository/files/catalog-info.yaml`, - (req, res, ctx) => { - const branch = req.url.searchParams.get('ref'); - if ( - branch === project.default_branch || - branch === 'main' || - branch === 'develop' - ) { - return res(ctx.status(200)); - } - return res(ctx.status(404, 'Not Found')); - }, +/** + * Checks for both project id and namespaced path, as these can both be used for the :id segment in Gitlab API: + * https://docs.gitlab.com/api/repository_files/#get-file-from-repository + */ +const httpProjectCatalogDynamic = all_projects_response.flatMap(project => { + const possibleIdSegments: string[] = [ + project.id.toString(), + project.path_with_namespace ?? '', + ]; + + return possibleIdSegments.map(seg => + rest.head( + `${apiBaseUrl}/projects/${encodeURIComponent( + seg, + )}/repository/files/catalog-info.yaml`, + (req, res, ctx) => { + const branch = req.url.searchParams.get('ref'); + if ( + branch === project.default_branch || + branch === 'main' || + branch === 'develop' + ) { + return res(ctx.status(200)); + } + return res(ctx.status(404, 'Not Found')); + }, + ), ); }); diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts index 9b3bac6399..5a60d18493 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts @@ -580,7 +580,12 @@ describe('hasFile', () => { }); }); - it('should find catalog file', async () => { + it('should find catalog file by id', async () => { + const hasFile = await client.hasFile('1', 'main', 'catalog-info.yaml'); + expect(hasFile).toBe(true); + }); + + it('should find catalog file by namespace path', async () => { const hasFile = await client.hasFile( 'group1/test-repo1', 'main',