Merge pull request #4120 from backstage/orkohunter/url-reader-readTree-with-sha

This commit is contained in:
Himanshu Mishra
2021-01-20 14:49:47 +01:00
committed by GitHub
33 changed files with 852 additions and 340 deletions
+56
View File
@@ -108,6 +108,62 @@ export function getAzureDownloadUrl(url: string): string {
return `${protocol}://${resource}/${organization}/${project}/_apis/git/repositories/${repoName}/items?recursionLevel=full&download=true&api-version=6.0${scopePath}`;
}
/**
* Given a URL, return the API URL to fetch commits on the branch.
*
* @param url A URL pointing to a repository or a sub-path
*/
export function getAzureCommitsUrl(url: string): string {
try {
const parsedUrl = new URL(url);
const [
empty,
userOrOrg,
project,
srcKeyword,
repoName,
] = parsedUrl.pathname.split('/');
// Remove the "GB" from "GBmain" for example.
const ref = parsedUrl.searchParams.get('version')?.substr(2);
if (
!!empty ||
!userOrOrg ||
!project ||
srcKeyword !== '_git' ||
!repoName
) {
throw new Error('Wrong Azure Devops URL');
}
// transform to commits api
parsedUrl.pathname = [
empty,
userOrOrg,
project,
'_apis',
'git',
'repositories',
repoName,
'commits',
].join('/');
const queryParams = [];
if (ref) {
queryParams.push(`searchCriteria.itemVersion.version=${ref}`);
}
parsedUrl.search = queryParams.join('&');
parsedUrl.protocol = 'https';
return parsedUrl.toString();
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
}
/**
* Gets the request options necessary to make requests to a given provider.
*
+1
View File
@@ -23,4 +23,5 @@ export {
getAzureDownloadUrl,
getAzureFileFetchUrl,
getAzureRequestOptions,
getAzureCommitsUrl,
} from './core';
+13 -1
View File
@@ -43,7 +43,18 @@ describe('readGitLabIntegrationConfig', () => {
const output = readGitLabIntegrationConfig(buildConfig({}));
expect(output).toEqual({
host: 'gitlab.com',
apiBaseUrl: 'gitlab.com/api/v4',
apiBaseUrl: 'https://gitlab.com/api/v4',
});
});
it('injects the correct GitLab API base URL when missing', () => {
const output = readGitLabIntegrationConfig(
buildConfig({ host: 'gitlab.com' }),
);
expect(output).toEqual({
host: 'gitlab.com',
apiBaseUrl: 'https://gitlab.com/api/v4',
});
});
@@ -86,6 +97,7 @@ describe('readGitLabIntegrationConfigs', () => {
expect(output).toEqual([
{
host: 'gitlab.com',
apiBaseUrl: 'https://gitlab.com/api/v4',
},
]);
});
+2 -2
View File
@@ -18,7 +18,7 @@ import { Config } from '@backstage/config';
import { isValidHost } from '../helpers';
const GITLAB_HOST = 'gitlab.com';
const GITLAB_API_BASE_URL = 'gitlab.com/api/v4';
const GITLAB_API_BASE_URL = 'https://gitlab.com/api/v4';
/**
* The configuration parameters for a single GitLab integration.
@@ -89,7 +89,7 @@ export function readGitLabIntegrationConfigs(
// As a convenience we always make sure there's at least an unauthenticated
// reader for public gitlab repos.
if (!result.some(c => c.host === GITLAB_HOST)) {
result.push({ host: GITLAB_HOST });
result.push({ host: GITLAB_HOST, apiBaseUrl: GITLAB_API_BASE_URL });
}
return result;