From f0f2e4f6b0bfbc699cee1d023c55647491e81218 Mon Sep 17 00:00:00 2001 From: ahmed Date: Wed, 6 Jul 2022 21:55:48 +0200 Subject: [PATCH] move relative path to a function Signed-off-by: ahmed --- .../src/reading/GitlabUrlReader.ts | 8 +++-- packages/integration/api-report.md | 6 +++- .../src/gitlab/GitLabIntegration.test.ts | 1 - .../integration/src/gitlab/config.test.ts | 5 --- packages/integration/src/gitlab/config.ts | 32 +++++++++-------- packages/integration/src/gitlab/core.test.ts | 35 ++++++++++++++----- packages/integration/src/gitlab/core.ts | 13 ++++--- packages/integration/src/gitlab/index.ts | 1 + 8 files changed, 64 insertions(+), 37 deletions(-) diff --git a/packages/backend-common/src/reading/GitlabUrlReader.ts b/packages/backend-common/src/reading/GitlabUrlReader.ts index f681adb4d4..c4cb0fb661 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.ts @@ -16,6 +16,7 @@ import { getGitLabFileFetchUrl, + getGitLabIntegrationRelativePath, getGitLabRequestOptions, GitLabIntegration, ScmIntegrations, @@ -118,11 +119,12 @@ export class GitlabUrlReader implements UrlReader { const { ref, full_name, filepath } = parseGitUrl(url); // Considering self hosted gitlab with relative - const repo_full_name = trimStart( - full_name, - this.integration.config.relativePath ?? '', + const relativePath = getGitLabIntegrationRelativePath( + this.integration.config, ); + const repo_full_name = trimStart(full_name, relativePath); + // Use GitLab API to get the default branch // encodeURIComponent is required for GitLab API // https://docs.gitlab.com/ee/api/README.html#namespaced-path-encoding diff --git a/packages/integration/api-report.md b/packages/integration/api-report.md index 78d170a0e0..8d8d017095 100644 --- a/packages/integration/api-report.md +++ b/packages/integration/api-report.md @@ -342,6 +342,11 @@ export function getGitLabFileFetchUrl( config: GitLabIntegrationConfig, ): Promise; +// @public +export function getGitLabIntegrationRelativePath( + config: GitLabIntegrationConfig, +): string; + // @public export function getGitLabRequestOptions(config: GitLabIntegrationConfig): { headers: Record; @@ -443,7 +448,6 @@ export type GitLabIntegrationConfig = { apiBaseUrl: string; token?: string; baseUrl: string; - relativePath?: string; }; // @public diff --git a/packages/integration/src/gitlab/GitLabIntegration.test.ts b/packages/integration/src/gitlab/GitLabIntegration.test.ts index ca84110438..575ce75308 100644 --- a/packages/integration/src/gitlab/GitLabIntegration.test.ts +++ b/packages/integration/src/gitlab/GitLabIntegration.test.ts @@ -28,7 +28,6 @@ describe('GitLabIntegration', () => { token: 't', apiBaseUrl: 'https://h.com/api/v4', baseUrl: 'https://h.com', - relativePath: '', }, ], }, diff --git a/packages/integration/src/gitlab/config.test.ts b/packages/integration/src/gitlab/config.test.ts index b642b3c7ae..db03285206 100644 --- a/packages/integration/src/gitlab/config.test.ts +++ b/packages/integration/src/gitlab/config.test.ts @@ -66,7 +66,6 @@ describe('readGitLabIntegrationConfig', () => { token: 't', apiBaseUrl: 'https://a.com', baseUrl: 'https://baseurl.for.me/gitlab', - relativePath: '/gitlab', }); }); @@ -78,7 +77,6 @@ describe('readGitLabIntegrationConfig', () => { host: 'gitlab.com', apiBaseUrl: 'https://gitlab.com/api/v4', baseUrl: 'https://gitlab.com', - relativePath: '', }); }); @@ -91,7 +89,6 @@ describe('readGitLabIntegrationConfig', () => { host: 'gitlab.com', baseUrl: 'https://gitlab.com', apiBaseUrl: 'https://gitlab.com/api/v4', - relativePath: '', }); }); @@ -122,7 +119,6 @@ describe('readGitLabIntegrationConfig', () => { host: 'a.com', apiBaseUrl: 'https://a.com/api', baseUrl: 'https://a.com', - relativePath: '/', }); }); }); @@ -148,7 +144,6 @@ describe('readGitLabIntegrationConfigs', () => { token: 't', apiBaseUrl: 'https://a.com/api/v4', baseUrl: 'https://a.com', - relativePath: '/', }); }); diff --git a/packages/integration/src/gitlab/config.ts b/packages/integration/src/gitlab/config.ts index 89f6b6a822..0e96a5e5e9 100644 --- a/packages/integration/src/gitlab/config.ts +++ b/packages/integration/src/gitlab/config.ts @@ -54,14 +54,6 @@ export type GitLabIntegrationConfig = { * If no baseUrl is provided, it will default to `https://${host}` */ baseUrl: string; - - /** - * The relative path for gitlab self hosted installations - * described here https://docs.gitlab.com/ee/install/relative_url.html - * - * If no baseUrl is provided, it will default to `/` - */ - relativePath?: string; }; /** @@ -77,7 +69,6 @@ export function readGitLabIntegrationConfig( let apiBaseUrl = config.getOptionalString('apiBaseUrl'); const token = config.getOptionalString('token'); let baseUrl = config.getOptionalString('baseUrl'); - let relativePath = ''; if (apiBaseUrl) { apiBaseUrl = trimEnd(apiBaseUrl, '/'); } else if (host === GITLAB_HOST) { @@ -104,11 +95,7 @@ export function readGitLabIntegrationConfig( ); } - if (host !== GITLAB_HOST) { - relativePath = new URL(baseUrl).pathname; - } - - return { host, token, apiBaseUrl, baseUrl, relativePath }; + return { host, token, apiBaseUrl, baseUrl }; } /** @@ -136,3 +123,20 @@ export function readGitLabIntegrationConfigs( return result; } + +/** + * Reads a GitLab integration config, and returns + * relative path. + * + * @param config - GitLabIntegrationConfig object + * @public + */ +export function getGitLabIntegrationRelativePath( + config: GitLabIntegrationConfig, +): string { + let relativePath = ''; + if (config.host !== GITLAB_HOST) { + relativePath = new URL(config.baseUrl).pathname; + } + return relativePath; +} diff --git a/packages/integration/src/gitlab/core.test.ts b/packages/integration/src/gitlab/core.test.ts index e923c0ee0c..d0ba87c653 100644 --- a/packages/integration/src/gitlab/core.test.ts +++ b/packages/integration/src/gitlab/core.test.ts @@ -35,26 +35,23 @@ describe('gitlab core', () => { }); const configWithToken: GitLabIntegrationConfig = { - host: 'g.com', + host: 'gitlab.com', token: '0123456789', apiBaseUrl: '', baseUrl: '', - relativePath: '', }; const configWithNoToken: GitLabIntegrationConfig = { - host: 'g.com', + host: 'gitlab.com', apiBaseUrl: '', baseUrl: '', - relativePath: '', }; const configWithSelfHosted: GitLabIntegrationConfig = { - host: 'g.com', + host: 'gitlab.mycompany.com', token: '0123456789', apiBaseUrl: '', - baseUrl: '', - relativePath: '/gitlab', + baseUrl: 'https://gitlab.mycompany.com/gitlab', }; describe('getGitLabFileFetchUrl with .yaml extension', () => { @@ -66,6 +63,12 @@ describe('gitlab core', () => { result: 'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch', }, + { + config: configWithSelfHosted, + url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yaml', + result: + 'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch', + }, { config: configWithNoToken, // Works with non URI encoded link @@ -73,6 +76,13 @@ describe('gitlab core', () => { result: 'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yaml/raw?ref=branch', }, + { + config: configWithSelfHosted, + url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yaml', + result: + 'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yaml/raw?ref=branch', + }, + { config: configWithNoToken, url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yaml', @@ -133,9 +143,9 @@ describe('gitlab core', () => { }, { config: configWithSelfHosted, - url: 'https://gitlab.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yml', + url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yml', result: - 'https://gitlab.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yml/raw?ref=branch', + 'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yml/raw?ref=branch', }, { config: configWithNoToken, @@ -144,6 +154,13 @@ describe('gitlab core', () => { result: 'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yml/raw?ref=branch', }, + { + config: configWithSelfHosted, + // Works with non URI encoded link + url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yml', + result: + 'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yml/raw?ref=branch', + }, { config: configWithNoToken, url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yml', diff --git a/packages/integration/src/gitlab/core.ts b/packages/integration/src/gitlab/core.ts index 5432bd90ee..5f51a514bf 100644 --- a/packages/integration/src/gitlab/core.ts +++ b/packages/integration/src/gitlab/core.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { GitLabIntegrationConfig } from './config'; +import { + getGitLabIntegrationRelativePath, + GitLabIntegrationConfig, +} from './config'; import fetch from 'cross-fetch'; import { InputError } from '@backstage/errors'; @@ -42,6 +45,7 @@ export async function getGitLabFileFetchUrl( // TODO(Rugvip): From the old GitlabReaderProcessor; used // the existence of /-/blob/ to switch the logic. Don't know if this // makes sense and it might require some more work. + if (url.includes('/-/blob/')) { const projectID = await getProjectId(url, config); return buildProjectUrl(url, projectID, config).toString(); @@ -111,9 +115,10 @@ export function buildProjectUrl( const branchAndFilePath = url.pathname.split('/-/blob/')[1]; const [branch, ...filePath] = branchAndFilePath.split('/'); + const relativePath = getGitLabIntegrationRelativePath(config); url.pathname = - [config.relativePath] + + [relativePath] + [ '/api/v4/projects', projectID, @@ -145,8 +150,8 @@ export async function getProjectId( try { let repo = url.pathname.split('/-/blob/')[0]; - // Ignore relative path if it's not set - const relativePath = config.relativePath ?? ''; + // Get gitlab relative path + const relativePath = getGitLabIntegrationRelativePath(config); // Should replace first match only repo = repo.replace(relativePath, ''); diff --git a/packages/integration/src/gitlab/index.ts b/packages/integration/src/gitlab/index.ts index e8d6665a6f..1a5063c51c 100644 --- a/packages/integration/src/gitlab/index.ts +++ b/packages/integration/src/gitlab/index.ts @@ -17,6 +17,7 @@ export { readGitLabIntegrationConfig, readGitLabIntegrationConfigs, + getGitLabIntegrationRelativePath, } from './config'; export type { GitLabIntegrationConfig } from './config'; export { getGitLabFileFetchUrl, getGitLabRequestOptions } from './core';