move relative path to a function
Signed-off-by: ahmed <ahmed@planet.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -342,6 +342,11 @@ export function getGitLabFileFetchUrl(
|
||||
config: GitLabIntegrationConfig,
|
||||
): Promise<string>;
|
||||
|
||||
// @public
|
||||
export function getGitLabIntegrationRelativePath(
|
||||
config: GitLabIntegrationConfig,
|
||||
): string;
|
||||
|
||||
// @public
|
||||
export function getGitLabRequestOptions(config: GitLabIntegrationConfig): {
|
||||
headers: Record<string, string>;
|
||||
@@ -443,7 +448,6 @@ export type GitLabIntegrationConfig = {
|
||||
apiBaseUrl: string;
|
||||
token?: string;
|
||||
baseUrl: string;
|
||||
relativePath?: string;
|
||||
};
|
||||
|
||||
// @public
|
||||
|
||||
@@ -28,7 +28,6 @@ describe('GitLabIntegration', () => {
|
||||
token: 't',
|
||||
apiBaseUrl: 'https://h.com/api/v4',
|
||||
baseUrl: 'https://h.com',
|
||||
relativePath: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -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: '/',
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -35,26 +35,23 @@ describe('gitlab core', () => {
|
||||
});
|
||||
|
||||
const configWithToken: GitLabIntegrationConfig = {
|
||||
host: 'g.com',
|
||||
host: 'gitlab.com',
|
||||
token: '0123456789',
|
||||
apiBaseUrl: '<ignored>',
|
||||
baseUrl: '<ignored>',
|
||||
relativePath: '',
|
||||
};
|
||||
|
||||
const configWithNoToken: GitLabIntegrationConfig = {
|
||||
host: 'g.com',
|
||||
host: 'gitlab.com',
|
||||
apiBaseUrl: '<ignored>',
|
||||
baseUrl: '<ignored>',
|
||||
relativePath: '',
|
||||
};
|
||||
|
||||
const configWithSelfHosted: GitLabIntegrationConfig = {
|
||||
host: 'g.com',
|
||||
host: 'gitlab.mycompany.com',
|
||||
token: '0123456789',
|
||||
apiBaseUrl: '<ignored>',
|
||||
baseUrl: '<ignored>',
|
||||
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',
|
||||
|
||||
@@ -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, '');
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
export {
|
||||
readGitLabIntegrationConfig,
|
||||
readGitLabIntegrationConfigs,
|
||||
getGitLabIntegrationRelativePath,
|
||||
} from './config';
|
||||
export type { GitLabIntegrationConfig } from './config';
|
||||
export { getGitLabFileFetchUrl, getGitLabRequestOptions } from './core';
|
||||
|
||||
Reference in New Issue
Block a user