diff --git a/packages/backend-common/src/reading/GitlabUrlReader.test.ts b/packages/backend-common/src/reading/GitlabUrlReader.test.ts index caf82f2f9e..4c612b4350 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.test.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.test.ts @@ -585,32 +585,22 @@ describe('GitlabUrlReader', () => { }); it('should fall back to getGitLabFileFetchUrl for blob urls', async () => { await expect( - (gitlabProcessor as any).getGitlabFetchUrl( - 'https://gitlab.com/group/subgroup/project/-/blob/branch/my/path/to/file.yaml', - ), - ).resolves.toEqual( - 'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch', - ); + (gitlabProcessor as any).getGitlabFetchUrl('https://gitlab.com/group/subgroup/project/-/blob/branch/my/path/to/file.yaml'), + ).resolves.toEqual('https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch'); }); it('should work for job artifact urls', async () => { await expect( - (gitlabProcessor as any).getGitlabFetchUrl( - 'https://gitlab.com/group/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob', - ), - ).resolves.toEqual( - 'https://gitlab.com/api/v4/projects/12345/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob', - ); + (gitlabProcessor as any).getGitlabFetchUrl('https://gitlab.com/group/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob'), + ).resolves.toEqual('https://gitlab.com/api/v4/projects/12345/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob'); }); it('should fail on unfamiliar or non-Gitlab urls', async () => { await expect( - (gitlabProcessor as any).getGitlabFetchUrl( - 'https://gitlab.com/some/random/endpoint', - ), + (gitlabProcessor as any).getGitlabFetchUrl('https://gitlab.com/some/random/endpoint'), ).rejects.toThrow('Please provide full path to yaml file from GitLab'); }); }); - describe('getGitlabArtfiactFetchUrl', () => { + describe('getGitlabArtifactFetchUrl', () => { beforeEach(() => { worker.use( rest.get( @@ -627,25 +617,17 @@ describe('GitlabUrlReader', () => { }); it('should reject urls that are not for the job artifacts API', async () => { await expect( - (gitlabProcessor as any).getGitlabArtifactFetchUrl( - 'https://gitlab.com/some/url', - ), + (gitlabProcessor as any).getGitlabArtifactFetchUrl(new URL('https://gitlab.com/some/url')) ).rejects.toThrow('Unable to process url as an GitLab artifact'); }); it('should work for job artifact urls', async () => { await expect( - (gitlabProcessor as any).getGitlabFetchUrl( - 'https://gitlab.com/group/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob', - ), - ).resolves.toEqual( - 'https://gitlab.com/api/v4/projects/12345/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob', - ); + (gitlabProcessor as any).getGitlabArtifactFetchUrl(new URL('https://gitlab.com/group/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob')) + ).resolves.toEqual(new URL('https://gitlab.com/api/v4/projects/12345/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob')); }); it('errors in mapping the project ID should be captured', async () => { await expect( - (gitlabProcessor as any).getGitlabFetchUrl( - 'https://gitlab.com/groupA/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob', - ), + (gitlabProcessor as any).getGitlabArtifactFetchUrl(new URL('https://gitlab.com/groupA/subgroup/project/-/jobs/artifacts/branch/raw/my/path/to/file.yaml?job=myJob')) ).rejects.toThrow(/^Unable to translate GitLab artifact URL:/); }); }); diff --git a/packages/backend-common/src/reading/GitlabUrlReader.ts b/packages/backend-common/src/reading/GitlabUrlReader.ts index f54e045857..7b89f00f47 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.ts @@ -259,8 +259,9 @@ export class GitlabUrlReader implements UrlReader { private async getGitlabFetchUrl(target: string): Promise { // If the target is for a job artifact then go down that path - if (target.includes('/-/jobs/artifacts/')) { - return this.getGitlabArtifactFetchUrl(target).then(value => + const targetUrl = new URL(target); + if (targetUrl.pathname.includes('/-/jobs/artifacts/')) { + return this.getGitlabArtifactFetchUrl(targetUrl).then(value => value.toString(), ); } @@ -272,22 +273,19 @@ export class GitlabUrlReader implements UrlReader { // https://example.com///-/jobs/artifacts//raw/?job= // to urls of the form: // https://example.com/api/v4/projects/:id/jobs/artifacts/:ref_name/raw/*artifact_path?job= - private async getGitlabArtifactFetchUrl(target: string): Promise { - const url = new URL(target); - if (!url.pathname.includes('/-/jobs/artifacts/')) { + private async getGitlabArtifactFetchUrl(target: URL): Promise { + if (!target.pathname.includes('/-/jobs/artifacts/')) { throw new Error('Unable to process url as an GitLab artifact'); } try { - const [namespaceAndProject, ref] = - url.pathname.split('/-/jobs/artifacts/'); - const projectPath = new URL(url); + const [namespaceAndProject, ref] = target.pathname.split('/-/jobs/artifacts/'); + const projectPath = new URL(target); projectPath.pathname = namespaceAndProject; const projectId = await this.resolveProjectToId(projectPath); - const relativePath = getGitLabIntegrationRelativePath( - this.integration.config, - ); - url.pathname = `${relativePath}/api/v4/projects/${projectId}/jobs/artifacts/${ref}`; - return url; + const relativePath = getGitLabIntegrationRelativePath(this.integration.config); + const newUrl = new URL(target); + newUrl.pathname = `${relativePath}/api/v4/projects/${projectId}/jobs/artifacts/${ref}`; + return newUrl; } catch (e) { throw new Error( `Unable to translate GitLab artifact URL: ${target}, ${e}`,