Merge pull request #26800 from anicke/get-sha

feat(gerrit-urlreader): support fetching content for a commit SHA hash
This commit is contained in:
Ben Lambert
2025-01-14 08:50:11 +01:00
committed by GitHub
8 changed files with 302 additions and 67 deletions
+8 -2
View File
@@ -299,7 +299,7 @@ export type BitbucketServerIntegrationConfig = {
password?: string;
};
// @public
// @public @deprecated
export function buildGerritGitilesArchiveUrl(
config: GerritIntegrationConfig,
project: string,
@@ -307,6 +307,12 @@ export function buildGerritGitilesArchiveUrl(
filePath: string,
): string;
// @public
export function buildGerritGitilesArchiveUrlFromLocation(
config: GerritIntegrationConfig,
url: string,
): string;
// @public
export class DefaultAzureCredentialsManager implements AzureCredentialsManager {
static fromIntegrations(
@@ -802,7 +808,7 @@ export interface IntegrationsByType {
harness: ScmIntegrationsGroup<HarnessIntegration>;
}
// @public
// @public @deprecated
export function parseGerritGitilesUrl(
config: GerritIntegrationConfig,
url: string,
@@ -21,6 +21,7 @@ import { registerMswTestHooks } from '../helpers';
import { GerritIntegrationConfig } from './config';
import {
buildGerritGitilesArchiveUrl,
buildGerritGitilesArchiveUrlFromLocation,
buildGerritGitilesUrl,
getGerritBranchApiUrl,
getGerritCloneRepoUrl,
@@ -115,6 +116,94 @@ describe('gerrit core', () => {
});
});
describe('buildGerritGitilesArchiveUrlFromLocation', () => {
const config: GerritIntegrationConfig = {
host: 'gerrit.com',
baseUrl: 'https://gerrit.com',
gitilesBaseUrl: 'https://gerrit.com/gitiles',
};
const configWithPath: GerritIntegrationConfig = {
host: 'gerrit.com',
baseUrl: 'https://gerrit.com/gerrit',
gitilesBaseUrl: 'https://gerrit.com/gerrit/plugins/gitiles',
};
const configWithDedicatedGitiles: GerritIntegrationConfig = {
host: 'gerrit.com',
baseUrl: 'https://gerrit.com/gerrit',
gitilesBaseUrl: 'https://dedicated-gitiles-server.com/gerrit/gitiles',
};
it('can create an archive url for a branch', () => {
expect(
buildGerritGitilesArchiveUrlFromLocation(
config,
'https://gerrit.com/gitiles/repo/+/refs/heads/dev/',
),
).toEqual(
'https://gerrit.com/gitiles/repo/+archive/refs/heads/dev.tar.gz',
);
});
it('can create an archive url for a sha', () => {
expect(
buildGerritGitilesArchiveUrlFromLocation(
config,
'https://gerrit.com/gitiles/repo/+/2846e8dc327ae2f60249983b1c3b96f42f205bae/',
),
).toEqual(
'https://gerrit.com/gitiles/repo/+archive/2846e8dc327ae2f60249983b1c3b96f42f205bae.tar.gz',
);
});
it('can create an archive url for a sha with a specific directory', () => {
expect(
buildGerritGitilesArchiveUrlFromLocation(
config,
'https://gerrit.com/gitiles/repo/+/2846e8dc327ae2f60249983b1c3b96f42f205bae/docs',
),
).toEqual(
'https://gerrit.com/gitiles/repo/+archive/2846e8dc327ae2f60249983b1c3b96f42f205bae/docs.tar.gz',
);
});
it('can create an archive url for a specific directory', () => {
expect(
buildGerritGitilesArchiveUrlFromLocation(
config,
'https://gerrit.com/gitiles/repo/+/refs/heads/dev/docs/',
),
).toEqual(
'https://gerrit.com/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz',
);
});
it('can create an authenticated url when auth is enabled and an url-path is used', () => {
const authConfig = {
...configWithPath,
username: 'username',
password: 'password',
};
expect(
buildGerritGitilesArchiveUrlFromLocation(
authConfig,
'https://gerrit.com/gerrit/plugins/gitiles/repo/+/refs/heads/dev/docs/',
),
).toEqual(
'https://gerrit.com/gerrit/a/plugins/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz',
);
});
it('Cannot build an authenticated url when a dedicated Gitiles server is used', () => {
const authConfig = {
...configWithDedicatedGitiles,
username: 'username',
password: 'password',
};
expect(() =>
buildGerritGitilesArchiveUrlFromLocation(
authConfig,
'https://gerrit.com/gitiles/repo/+/refs/heads/dev/',
),
).toThrow(
'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.',
);
});
});
describe('buildGerritGitilesUrl', () => {
it('can create an url from arguments', () => {
const config: GerritIntegrationConfig = {
@@ -426,6 +515,39 @@ describe('gerrit core', () => {
'https://gerrit.com/a/projects/web%2Fproject/branches/master/files/README.md/content',
);
});
it('can create an authenticated url for a commit.', () => {
const authConfig: GerritIntegrationConfig = {
host: 'gerrit.com',
baseUrl: 'https://gerrit.com',
gitilesBaseUrl: 'https://gerrit.com',
username: 'u',
password: 'u',
};
const authFileContentUrl = getGerritFileContentsApiUrl(
authConfig,
'https://gerrit.com/web/project/+/157f862803d45b9d269f0e390f88aece1ded51e8/README.md',
);
expect(authFileContentUrl).toEqual(
'https://gerrit.com/a/projects/web%2Fproject/commits/157f862803d45b9d269f0e390f88aece1ded51e8/files/README.md/content',
);
});
it('will throw for unsupported ref types (tag).', () => {
const authConfig: GerritIntegrationConfig = {
host: 'gerrit.com',
baseUrl: 'https://gerrit.com',
gitilesBaseUrl: 'https://gerrit.com',
username: 'u',
password: 'u',
};
expect(() =>
getGerritFileContentsApiUrl(
authConfig,
'https://gerrit.com/modules/events-broker/+/refs/tags/v3.5.6/src/main/java/com/gerritforge/gerrit/eventbroker/BrokerApi.java',
),
).toThrow(/gitiles ref type/);
});
});
describe('parseGerritJsonResponse', () => {
+54 -7
View File
@@ -41,8 +41,9 @@ const GERRIT_BODY_PREFIX = ")]}'";
*
* @param url - An URL pointing to a file stored in git.
* @public
* @deprecated `parseGerritGitilesUrl` is deprecated. Use
* {@link parseGitilesUrlRef} instead.
*/
export function parseGerritGitilesUrl(
config: GerritIntegrationConfig,
url: string,
@@ -215,6 +216,8 @@ export function buildGerritGitilesUrl(
* @param branch - The branch we will target.
* @param filePath - The absolute file path.
* @public
* @deprecated `buildGerritGitilesArchiveUrl` is deprecated. Use
* {@link buildGerritGitilesArchiveUrlFromLocation} instead.
*/
export function buildGerritGitilesArchiveUrl(
config: GerritIntegrationConfig,
@@ -229,6 +232,38 @@ export function buildGerritGitilesArchiveUrl(
)}/${project}/+archive/refs/heads/${branch}${archiveName}`;
}
/**
* Build a Gerrit Gitiles archive url from a Gitiles url.
*
* @param config - A Gerrit provider config.
* @param url - The gitiles url
* @public
*/
export function buildGerritGitilesArchiveUrlFromLocation(
config: GerritIntegrationConfig,
url: string,
): string {
const {
path: filePath,
ref,
project,
refType,
} = parseGitilesUrlRef(config, url);
const archiveName =
filePath === '/' || filePath === '' ? '.tar.gz' : `/${filePath}.tar.gz`;
if (refType === 'branch') {
return `${getGitilesAuthenticationUrl(
config,
)}/${project}/+archive/refs/heads/${ref}${archiveName}`;
}
if (refType === 'sha') {
return `${getGitilesAuthenticationUrl(
config,
)}/${project}/+archive/${ref}${archiveName}`;
}
throw new Error(`Unsupported gitiles ref type: ${refType}`);
}
/**
* Return the authentication prefix.
*
@@ -324,13 +359,25 @@ export function getGerritFileContentsApiUrl(
config: GerritIntegrationConfig,
url: string,
) {
const { branch, filePath, project } = parseGerritGitilesUrl(config, url);
const { ref, refType, path, project } = parseGitilesUrlRef(config, url);
return `${config.baseUrl}${getAuthenticationPrefix(
config,
)}projects/${encodeURIComponent(
project,
)}/branches/${branch}/files/${encodeURIComponent(filePath)}/content`;
// https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content
if (refType === 'branch') {
return `${config.baseUrl}${getAuthenticationPrefix(
config,
)}projects/${encodeURIComponent(
project,
)}/branches/${ref}/files/${encodeURIComponent(path)}/content`;
}
// https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content-from-commit
if (refType === 'sha') {
return `${config.baseUrl}${getAuthenticationPrefix(
config,
)}projects/${encodeURIComponent(
project,
)}/commits/${ref}/files/${encodeURIComponent(path)}/content`;
}
throw new Error(`Unsupported gitiles ref type: ${refType}`);
}
/**
+1
View File
@@ -20,6 +20,7 @@ export {
} from './config';
export {
buildGerritGitilesArchiveUrl,
buildGerritGitilesArchiveUrlFromLocation,
getGerritBranchApiUrl,
getGerritCloneRepoUrl,
getGerritFileContentsApiUrl,