From 6f9f3b6936c336159904b1eb667a872444c79bd2 Mon Sep 17 00:00:00 2001 From: ahmed Date: Fri, 8 Jul 2022 17:30:17 +0200 Subject: [PATCH] rename variable and add relativePath check Signed-off-by: ahmed --- .../src/reading/GitlabUrlReader.ts | 14 +++++++---- packages/integration/src/gitlab/core.ts | 24 ++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/backend-common/src/reading/GitlabUrlReader.ts b/packages/backend-common/src/reading/GitlabUrlReader.ts index c4cb0fb661..5bc234daef 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.ts @@ -118,12 +118,16 @@ export class GitlabUrlReader implements UrlReader { const { etag, signal } = options ?? {}; const { ref, full_name, filepath } = parseGitUrl(url); - // Considering self hosted gitlab with relative + let repoFullName = full_name; + const relativePath = getGitLabIntegrationRelativePath( this.integration.config, ); - const repo_full_name = trimStart(full_name, relativePath); + // Considering self hosted gitlab with relative + if (relativePath) { + repoFullName = trimStart(full_name, relativePath); + } // Use GitLab API to get the default branch // encodeURIComponent is required for GitLab API @@ -131,7 +135,7 @@ export class GitlabUrlReader implements UrlReader { const projectGitlabResponse = await fetch( new URL( `${this.integration.config.apiBaseUrl}/projects/${encodeURIComponent( - repo_full_name, + repoFullName, )}`, ).toString(), getGitLabRequestOptions(this.integration.config), @@ -158,7 +162,7 @@ export class GitlabUrlReader implements UrlReader { const commitsGitlabResponse = await fetch( new URL( `${this.integration.config.apiBaseUrl}/projects/${encodeURIComponent( - repo_full_name, + repoFullName, )}/repository/commits?${commitsReqParams.toString()}`, ).toString(), { @@ -189,7 +193,7 @@ export class GitlabUrlReader implements UrlReader { // https://docs.gitlab.com/ee/api/repositories.html#get-file-archive const archiveGitLabResponse = await fetch( `${this.integration.config.apiBaseUrl}/projects/${encodeURIComponent( - repo_full_name, + repoFullName, )}/repository/archive?sha=${branch}`, { ...getGitLabRequestOptions(this.integration.config), diff --git a/packages/integration/src/gitlab/core.ts b/packages/integration/src/gitlab/core.ts index 5f51a514bf..1fcf15a78f 100644 --- a/packages/integration/src/gitlab/core.ts +++ b/packages/integration/src/gitlab/core.ts @@ -117,15 +117,15 @@ export function buildProjectUrl( const [branch, ...filePath] = branchAndFilePath.split('/'); const relativePath = getGitLabIntegrationRelativePath(config); - url.pathname = - [relativePath] + - [ - '/api/v4/projects', - projectID, - 'repository/files', - encodeURIComponent(decodeURIComponent(filePath.join('/'))), - 'raw', - ].join('/'); + url.pathname = [ + ...(relativePath ? [relativePath] : []), + 'api/v4/projects', + projectID, + 'repository/files', + encodeURIComponent(decodeURIComponent(filePath.join('/'))), + 'raw', + ].join('/'); + url.search = `?ref=${branch}`; return url; @@ -153,8 +153,10 @@ export async function getProjectId( // Get gitlab relative path const relativePath = getGitLabIntegrationRelativePath(config); - // Should replace first match only - repo = repo.replace(relativePath, ''); + // Check relative path exist and replace it if it's the case. + if (relativePath) { + repo = repo.replace(relativePath, ''); + } // Convert // to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FsubgroupA%2FteamA%2Frepo