diff --git a/docs/integrations/gerrit/locations.md b/docs/integrations/gerrit/locations.md index 3e76d966ad..747e3f2404 100644 --- a/docs/integrations/gerrit/locations.md +++ b/docs/integrations/gerrit/locations.md @@ -29,8 +29,9 @@ you can list the Gerrit instances you want to fetch data from. Each entry is a structure with up to four elements: - `host`: The host of the Gerrit instance, e.g. `gerrit.company.com`. -- `apiBaseUrl`: The base url of the Gerrit API. This would typically be the address - up to but not including the authentication ("/a/") prefix. +- `apiBaseUrl` (optional): Needed if the Gerrit instance is not reachable at + the base of the `host` option (e.g. `https://gerrit.company.com`). This is + the address that you would open in a browser. - `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/mkdocs.yml b/mkdocs.yml index 4671a10b7a..05c492a077 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -97,6 +97,8 @@ nav: - Discovery: 'integrations/bitbucket/discovery.md' - Datadog: - Installation: 'integrations/datadog-rum/installation.md' + - Gerrit: + - Locations: 'integrations/gerrit/locations.md' - GitHub: - Locations: 'integrations/github/locations.md' - Discovery: 'integrations/github/discovery.md' diff --git a/packages/integration/api-report.md b/packages/integration/api-report.md index 2b3f99fd2c..9a93c30316 100644 --- a/packages/integration/api-report.md +++ b/packages/integration/api-report.md @@ -136,7 +136,7 @@ export class GerritIntegration implements ScmIntegration { // @public export type GerritIntegrationConfig = { host: string; - apiBaseUrl: string; + apiBaseUrl?: string; username?: string; password?: string; }; diff --git a/packages/integration/config.d.ts b/packages/integration/config.d.ts index 06ab359e4a..90b8b9b759 100644 --- a/packages/integration/config.d.ts +++ b/packages/integration/config.d.ts @@ -68,7 +68,7 @@ export interface Config { */ host: string; /** - * The base url for the Gerrit API. + * The base url for the Gerrit instance. * @visibility frontend */ apiBaseUrl?: string; diff --git a/packages/integration/src/gerrit/config.test.ts b/packages/integration/src/gerrit/config.test.ts index 3377a3d0ff..f448b76b0b 100644 --- a/packages/integration/src/gerrit/config.test.ts +++ b/packages/integration/src/gerrit/config.test.ts @@ -68,12 +68,23 @@ describe('readGerritIntegrationConfig', () => { }); }); + it('can create a default value if the API base URL is missing', () => { + const output = readGerritIntegrationConfig( + buildConfig({ + host: 'a.com', + }), + ); + expect(output).toEqual({ + host: 'a.com', + apiBaseUrl: 'https://a.com', + username: undefined, + password: undefined, + }); + }); + it('rejects funky configs', () => { const valid: any = { host: 'a.com', - apiBaseUrl: 'https://a.com/api', - username: 'u', - appPassword: 'p', }; expect(() => readGerritIntegrationConfig(buildConfig({ ...valid, host: 2 })), diff --git a/packages/integration/src/gerrit/config.ts b/packages/integration/src/gerrit/config.ts index d118e22278..c376aef98f 100644 --- a/packages/integration/src/gerrit/config.ts +++ b/packages/integration/src/gerrit/config.ts @@ -30,10 +30,13 @@ export type GerritIntegrationConfig = { host: string; /** - * The base URL of the API of this provider, e.g. "https://gerrit-review.com/gerrit", - * with no trailing slash. + * The optional base URL of the Gerrit instance. It is assumed that https + * is used and that the base path is "/" on the host. If that is not the + * case set the complete base url to the gerrit instance, e.g. + * "https://gerrit-review.com/gerrit". This is the url that you would open + * in a browser. */ - apiBaseUrl: string; + apiBaseUrl?: string; /** * The username to use for requests to gerrit. @@ -57,7 +60,7 @@ export function readGerritIntegrationConfig( config: Config, ): GerritIntegrationConfig { const host = config.getString('host'); - let apiBaseUrl = config.getString('apiBaseUrl'); + let apiBaseUrl = config.getOptionalString('apiBaseUrl'); const username = config.getOptionalString('username'); const password = config.getOptionalString('password'); @@ -65,13 +68,15 @@ export function readGerritIntegrationConfig( throw new Error( `Invalid Gerrit integration config, '${host}' is not a valid host`, ); - } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) { + } else if (apiBaseUrl && !isValidUrl(apiBaseUrl)) { throw new Error( `Invalid Gerrit integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`, ); } if (apiBaseUrl) { apiBaseUrl = trimEnd(apiBaseUrl, '/'); + } else { + apiBaseUrl = `https://${host}`; } return {