diff --git a/.changeset/ten-spies-explode.md b/.changeset/ten-spies-explode.md new file mode 100644 index 0000000000..ba7d406398 --- /dev/null +++ b/.changeset/ten-spies-explode.md @@ -0,0 +1,11 @@ +--- +'@backstage/integration': minor +--- + +Add Gerrit option to activate the edit mode for version >= 3.9 + +``` + enableEditUrl: true +``` + +The url pattern is `^\/admin\/repos\/edit\/repo\/(.+)\/branch\/(.+)\/file\/(.+)$` diff --git a/docs/integrations/gerrit/locations.md b/docs/integrations/gerrit/locations.md index 3f237fc9ba..79e7771bc6 100644 --- a/docs/integrations/gerrit/locations.md +++ b/docs/integrations/gerrit/locations.md @@ -22,6 +22,7 @@ integrations: gitilesBaseUrl: https://gerrit.company.com/gitiles baseUrl: https://gerrit.company.com/gerrit cloneUrl: https://gerrit.company.com/clone + enableEditUrl: true username: ${GERRIT_USERNAME} password: ${GERRIT_PASSWORD} ``` @@ -37,6 +38,7 @@ a structure with up to six elements: address here. This is the address that you would open in a browser. - `cloneUrl` (optional): The base URL for HTTP clones. Will default to `baseUrl` if not set. The address used to clone a repo is the `cloneUrl` plus the repo name. +- `enableEditUrl` (optional): Activate the edit mode for Gerrit >= 3.9 - `username` (optional): The Gerrit username to use in API requests. If neither a username nor password are supplied, anonymous access will be used. - `password` (optional): The password or http token for the Gerrit user. diff --git a/packages/integration/config.d.ts b/packages/integration/config.d.ts index 1e4c8d373d..fc4559f73a 100644 --- a/packages/integration/config.d.ts +++ b/packages/integration/config.d.ts @@ -161,6 +161,11 @@ export interface Config { * @visibility frontend */ cloneUrl?: string; + /** + * Activate the edit url feature available since Gerrit 3.9 + * @visibility frontend + */ + enableEditUrl?: boolean; /** * The username to use for authenticated requests. * @visibility secret diff --git a/packages/integration/report.api.md b/packages/integration/report.api.md index 6bb48b8185..ecf46b10f1 100644 --- a/packages/integration/report.api.md +++ b/packages/integration/report.api.md @@ -392,6 +392,7 @@ export type GerritIntegrationConfig = { host: string; baseUrl?: string; cloneUrl?: string; + enableEditUrl?: boolean; gitilesBaseUrl: string; username?: string; password?: string; diff --git a/packages/integration/src/gerrit/GerritIntegration.test.ts b/packages/integration/src/gerrit/GerritIntegration.test.ts index 2cfc748550..b415636072 100644 --- a/packages/integration/src/gerrit/GerritIntegration.test.ts +++ b/packages/integration/src/gerrit/GerritIntegration.test.ts @@ -47,6 +47,7 @@ describe('GerritIntegration', () => { it('returns the basics', () => { const integration = new GerritIntegration({ host: 'gerrit-review.example.com', + gitilesBaseUrl: 'https://gerrit-review.example.com/gitiles', } as any); expect(integration.type).toBe('gerrit'); expect(integration.title).toBe('gerrit-review.example.com'); @@ -70,6 +71,7 @@ describe('GerritIntegration', () => { it('handles line numbers', () => { const integration = new GerritIntegration({ host: 'gerrit-review.example.com', + gitilesBaseUrl: 'https://gerrit-review.example.com/gitiles', } as any); expect( @@ -85,6 +87,7 @@ describe('GerritIntegration', () => { describe('resolves with a relative url', () => { const integration = new GerritIntegration({ host: 'gerrit-review.example.com', + gitilesBaseUrl: 'https://gerrit-review.example.com/gitiles', } as any); it('works for valid urls pointing to a branch', () => { expect( @@ -144,8 +147,27 @@ describe('GerritIntegration', () => { // url as is. expect( integration.resolveEditUrl( - 'https://gerrit-review.example.com/catalog-info.yaml', + 'https://gerrit-review.example.com/gitiles/backstage/backstage/+/refs/heads/master/catalog-info.yaml', ), - ).toBe('https://gerrit-review.example.com/catalog-info.yaml'); + ).toBe( + 'https://gerrit-review.example.com/gitiles/backstage/backstage/+/refs/heads/master/catalog-info.yaml', + ); + }); + + it('resolve edit URL with editUrl', () => { + const integration = new GerritIntegration({ + host: 'gerrit-review.example.com', + baseUrl: 'https://gerrit-review.example.com', + enableEditUrl: true, + gitilesBaseUrl: 'https://gerrit-review.example.com/gitiles', + } as any); + + expect( + integration.resolveEditUrl( + 'https://gerrit-review.example.com/gitiles/backstage/backstage/+/refs/heads/master/catalog-info.yaml', + ), + ).toBe( + 'https://gerrit-review.example.com/admin/repos/edit/repo/backstage/backstage/branch/refs/heads/master/file/catalog-info.yaml', + ); }); }); diff --git a/packages/integration/src/gerrit/GerritIntegration.ts b/packages/integration/src/gerrit/GerritIntegration.ts index 7e2e757c6b..adca205ac7 100644 --- a/packages/integration/src/gerrit/GerritIntegration.ts +++ b/packages/integration/src/gerrit/GerritIntegration.ts @@ -20,7 +20,7 @@ import { GerritIntegrationConfig, readGerritIntegrationConfigs, } from './config'; -import { parseGitilesUrlRef } from './core'; +import { buildGerritEditUrl, parseGitilesUrlRef } from './core'; /** * A Gerrit based integration. @@ -75,7 +75,15 @@ export class GerritIntegration implements ScmIntegration { } resolveEditUrl(url: string): string { - // Not applicable for gerrit. + if (this.config.enableEditUrl) { + const parsed = parseGitilesUrlRef(this.config, url); + return buildGerritEditUrl( + this.config, + parsed.project, + parsed.ref, + parsed.path, + ); + } return url; } } diff --git a/packages/integration/src/gerrit/config.test.ts b/packages/integration/src/gerrit/config.test.ts index be31193f2e..8bf1a3d44c 100644 --- a/packages/integration/src/gerrit/config.test.ts +++ b/packages/integration/src/gerrit/config.test.ts @@ -57,6 +57,7 @@ describe('readGerritIntegrationConfig', () => { host: 'a.com', baseUrl: 'https://a.com/api', cloneUrl: 'https:a.com/clone', + enableEditUrl: false, gitilesBaseUrl: 'https://a.com/git', username: 'u', password: ' p ', @@ -66,12 +67,32 @@ describe('readGerritIntegrationConfig', () => { host: 'a.com', baseUrl: 'https://a.com/api', cloneUrl: 'https:a.com/clone', + enableEditUrl: false, gitilesBaseUrl: 'https://a.com/git', username: 'u', password: 'p', }); }); + it('activate Edit Url', () => { + const output = readGerritIntegrationConfig( + buildConfig({ + host: 'a.com', + enableEditUrl: true, + gitilesBaseUrl: 'https://a.com/gerrit/plugins/gitiles', + }), + ); + expect(output).toEqual({ + host: 'a.com', + baseUrl: 'https://a.com', + cloneUrl: 'https://a.com', + enableEditUrl: true, + gitilesBaseUrl: 'https://a.com/gerrit/plugins/gitiles', + username: undefined, + password: undefined, + }); + }); + it('can create a default value if the API base URL is missing', () => { const output = readGerritIntegrationConfig( buildConfig({ @@ -156,6 +177,7 @@ describe('readGerritIntegrationConfigs', () => { host: 'b.com', baseUrl: 'https://b.com/api', cloneUrl: 'https://b.com/api', + enableEditUrl: undefined, gitilesBaseUrl: 'https://b.com/gerrit/plugins/gitiles', username: undefined, password: undefined, diff --git a/packages/integration/src/gerrit/config.ts b/packages/integration/src/gerrit/config.ts index a5fdc8dabf..710e2847de 100644 --- a/packages/integration/src/gerrit/config.ts +++ b/packages/integration/src/gerrit/config.ts @@ -44,6 +44,11 @@ export type GerritIntegrationConfig = { */ cloneUrl?: string; + /** + * Activate the edit url feature available since Gerrit 3.9 + */ + enableEditUrl?: boolean; + /** * Base url for Gitiles. This is needed for creating a valid * user-friendly url that can be used for browsing the content of the @@ -75,6 +80,7 @@ export function readGerritIntegrationConfig( const host = config.getString('host'); let baseUrl = config.getOptionalString('baseUrl'); let cloneUrl = config.getOptionalString('cloneUrl'); + const enableEditUrl = config.getOptionalBoolean('enableEditUrl'); let gitilesBaseUrl = config.getString('gitilesBaseUrl'); const username = config.getOptionalString('username'); const password = config.getOptionalString('password')?.trim(); @@ -101,21 +107,19 @@ export function readGerritIntegrationConfig( } else { baseUrl = `https://${host}`; } - if (gitilesBaseUrl) { - gitilesBaseUrl = trimEnd(gitilesBaseUrl, '/'); - } else { - gitilesBaseUrl = `https://${host}`; - } if (cloneUrl) { cloneUrl = trimEnd(cloneUrl, '/'); } else { cloneUrl = baseUrl; } + gitilesBaseUrl = trimEnd(gitilesBaseUrl, '/'); + return { host, baseUrl, cloneUrl, + enableEditUrl, gitilesBaseUrl, username, password, diff --git a/packages/integration/src/gerrit/core.ts b/packages/integration/src/gerrit/core.ts index d44865881d..6b90bb28ef 100644 --- a/packages/integration/src/gerrit/core.ts +++ b/packages/integration/src/gerrit/core.ts @@ -208,6 +208,29 @@ export function buildGerritGitilesUrl( }/${project}/+/refs/heads/${branch}/${trimStart(filePath, '/')}`; } +/** + * Build a Gerrit Gitiles url that targets a specific path. + * + * @param config - A Gerrit provider config. + * @param project - The name of the git project + * @param branch - The branch we will target. + * @param filePath - The absolute file path. + * @public + */ +export function buildGerritEditUrl( + config: GerritIntegrationConfig, + project: string, + branch: string, + filePath: string, +): string { + return `${ + config.baseUrl + }/admin/repos/edit/repo/${project}/branch/refs/heads/${branch}/file/${trimStart( + filePath, + '/', + )}`; +} + /** * Build a Gerrit Gitiles archive url that targets a specific branch and path *