diff options
author | Ben Rohlfs <brohlfs@google.com> | 2023-10-23 16:31:41 +0200 |
---|---|---|
committer | Luca Milanesio <luca.milanesio@gmail.com> | 2023-10-26 21:12:14 +0000 |
commit | 9264d8803f1882ff6595f5261248899455ab29c1 (patch) | |
tree | b00f4c5fb720e3e3527fcca798a28c8780c2146d | |
parent | f950ec2579b31221012e8abe305d082480321ca3 (diff) |
Refactor `docsBaseUrl` related code
We are planning to add a docs link to `gr-revisions-parents` and the
existing code for creating Documentation links was in such a bad shape
that we are refactoring it here.
`getDocsBaseUrl` was moved from `gr-rest-api` to `config-model`, which
is a much better place for it.
`getDocsBaseUrl` generally falls back to `gerrit-review`, not just in
some places.
The construction of urls from base and relative URL was moved to
url-util.
Release-Notes: skip
Change-Id: Ic991426b60cffa62e5c0dd27d38b4fecba3e8409
(cherry picked from commit 2d6ad54a532487333fda2b8921ecff041cb1c21e)
12 files changed, 238 insertions, 323 deletions
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.ts b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.ts index a55173cd69..34950b013f 100644 --- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.ts +++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.ts @@ -8,7 +8,7 @@ import '../../shared/gr-dropdown/gr-dropdown'; import '../../shared/gr-icon/gr-icon'; import '../gr-account-dropdown/gr-account-dropdown'; import '../gr-smart-search/gr-smart-search'; -import {getBaseUrl} from '../../../utils/url-util'; +import {getBaseUrl, getDocUrl} from '../../../utils/url-util'; import {getAdminLinks, NavLink} from '../../../models/views/admin'; import { AccountDetailInfo, @@ -85,6 +85,18 @@ const DOCUMENTATION_LINKS: MainHeaderLink[] = [ }, ]; +// visible for testing +export function getDocLinks(docBaseUrl: string, docLinks: MainHeaderLink[]) { + if (!docBaseUrl) return []; + return docLinks.map(link => { + return { + url: getDocUrl(docBaseUrl, link.url), + name: link.name, + target: '_blank', + }; + }); +} + // Set of authentication methods that can provide custom registration page. const AUTH_TYPES_WITH_REGISTER_URL: Set<AuthType> = new Set([ AuthType.LDAP, @@ -121,7 +133,7 @@ export class GrMainHeader extends LitElement { @state() private adminLinks: NavLink[] = []; - @state() private docBaseUrl: string | null = null; + @state() private docsBaseUrl = ''; @state() private userLinks: MainHeaderLink[] = []; @@ -165,7 +177,7 @@ export class GrMainHeader extends LitElement { subscribe( this, () => this.getConfigModel().docsBaseUrl$, - docsBaseUrl => (this.docBaseUrl = docsBaseUrl) + docsBaseUrl => (this.docsBaseUrl = docsBaseUrl) ); subscribe( this, @@ -359,12 +371,9 @@ export class GrMainHeader extends LitElement { </gr-endpoint-decorator> </a> <ul class="links"> - ${this.computeLinks( - this.userLinks, - this.adminLinks, - this.topMenus, - this.docBaseUrl - ).map(linkGroup => this.renderLinkGroup(linkGroup))} + ${this.computeLinks(this.userLinks, this.adminLinks, this.topMenus).map( + linkGroup => this.renderLinkGroup(linkGroup) + )} </ul> <div class="rightItems"> <gr-endpoint-decorator @@ -488,15 +497,13 @@ export class GrMainHeader extends LitElement { userLinks?: MainHeaderLink[], adminLinks?: NavLink[], topMenus?: TopMenuEntryInfo[], - docBaseUrl?: string | null, // defaultLinks parameter is used in tests only defaultLinks = DEFAULT_LINKS ) { if ( userLinks === undefined || adminLinks === undefined || - topMenus === undefined || - docBaseUrl === undefined + topMenus === undefined ) { return []; } @@ -513,7 +520,7 @@ export class GrMainHeader extends LitElement { links: userLinks.slice(), }); } - const docLinks = this.getDocLinks(docBaseUrl, DOCUMENTATION_LINKS); + const docLinks = getDocLinks(this.docsBaseUrl, DOCUMENTATION_LINKS); if (docLinks.length) { links.push({ title: 'Documentation', @@ -550,24 +557,6 @@ export class GrMainHeader extends LitElement { } // private but used in test - getDocLinks(docBaseUrl: string | null, docLinks: MainHeaderLink[]) { - if (!docBaseUrl) { - return []; - } - return docLinks.map(link => { - let url = docBaseUrl; - if (url && url[url.length - 1] === '/') { - url = url.substring(0, url.length - 1); - } - return { - url: url + link.url, - name: link.name, - target: '_blank', - }; - }); - } - - // private but used in test loadAccount() { this.loading = true; diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.ts b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.ts index 4b9c313d23..dfb44b70e1 100644 --- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.ts +++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.ts @@ -11,7 +11,7 @@ import { stubRestApi, } from '../../../test/test-utils'; import './gr-main-header'; -import {GrMainHeader} from './gr-main-header'; +import {GrMainHeader, getDocLinks} from './gr-main-header'; import { createAccountDetailWithId, createGerritInfo, @@ -51,6 +51,11 @@ suite('gr-main-header tests', () => { <span class="linksTitle" id="Changes"> Changes </span> </gr-dropdown> </li> + <li class="hideOnMobile"> + <gr-dropdown down-arrow="" horizontal-align="left" link=""> + <span class="linksTitle" id="Documentation">Documentation</span> + </gr-dropdown> + </li> <li> <gr-dropdown down-arrow="" horizontal-align="left" link=""> <span class="linksTitle" id="Browse"> Browse </span> @@ -168,36 +173,24 @@ suite('gr-main-header tests', () => { // When no admin links are passed, it should use the default. assert.deepEqual( - element.computeLinks( - /* userLinks= */ [], - adminLinks, - /* topMenus= */ [], - /* docBaseUrl= */ '', - defaultLinks - ), - defaultLinks.concat({ - title: 'Browse', - links: adminLinks, - }) + element + .computeLinks( + /* userLinks= */ [], + adminLinks, + /* topMenus= */ [], + defaultLinks + ) + .find(i => i.title === 'Faves'), + defaultLinks[0] ); assert.deepEqual( - element.computeLinks( - userLinks, - adminLinks, - /* topMenus= */ [], - /* docBaseUrl= */ '', - defaultLinks - ), - defaultLinks.concat([ - { - title: 'Your', - links: userLinks, - }, - { - title: 'Browse', - links: adminLinks, - }, - ]) + element + .computeLinks(userLinks, adminLinks, /* topMenus= */ [], defaultLinks) + .find(i => i.title === 'Your'), + { + title: 'Your', + links: userLinks, + } ); }); @@ -209,11 +202,10 @@ suite('gr-main-header tests', () => { }, ]; - assert.deepEqual(element.getDocLinks(null, docLinks), []); - assert.deepEqual(element.getDocLinks('', docLinks), []); - assert.deepEqual(element.getDocLinks('base', []), []); + assert.deepEqual(getDocLinks('', docLinks), []); + assert.deepEqual(getDocLinks('base', []), []); - assert.deepEqual(element.getDocLinks('base', docLinks), [ + assert.deepEqual(getDocLinks('base', docLinks), [ { name: 'Table of Contents', target: '_blank', @@ -221,7 +213,7 @@ suite('gr-main-header tests', () => { }, ]); - assert.deepEqual(element.getDocLinks('base/', docLinks), [ + assert.deepEqual(getDocLinks('base/', docLinks), [ { name: 'Table of Contents', target: '_blank', @@ -255,24 +247,17 @@ suite('gr-main-header tests', () => { /* userLinks= */ [], adminLinks, topMenus, - /* baseDocUrl= */ '', /* defaultLinks= */ [] - ), - [ - { - title: 'Browse', - links: adminLinks, - }, - { - title: 'Plugins', - links: [ - { - name: 'Manage', - url: 'https://gerrit/plugins/plugin-manager/static/index.html', - }, - ], - }, - ] + )[2], + { + title: 'Plugins', + links: [ + { + name: 'Manage', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, + ], + } ); }); @@ -306,24 +291,17 @@ suite('gr-main-header tests', () => { /* userLinks= */ [], adminLinks, topMenus, - /* baseDocUrl= */ '', /* defaultLinks= */ [] - ), - [ - { - title: 'Browse', - links: adminLinks, - }, - { - title: 'Projects', - links: [ - { - name: 'Project List', - url: '/plugins/myplugin/index.html', - }, - ], - }, - ] + )[2], + { + title: 'Projects', + links: [ + { + name: 'Project List', + url: '/plugins/myplugin/index.html', + }, + ], + } ); }); @@ -362,28 +340,21 @@ suite('gr-main-header tests', () => { /* userLinks= */ [], adminLinks, topMenus, - /* baseDocUrl= */ '', /* defaultLinks= */ [] - ), - [ - { - title: 'Browse', - links: adminLinks, - }, - { - title: 'Plugins', - links: [ - { - name: 'Manage', - url: 'https://gerrit/plugins/plugin-manager/static/index.html', - }, - { - name: 'Create', - url: 'https://gerrit/plugins/plugin-manager/static/create.html', - }, - ], - }, - ] + )[2], + { + title: 'Plugins', + links: [ + { + name: 'Manage', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, + { + name: 'Create', + url: 'https://gerrit/plugins/plugin-manager/static/create.html', + }, + ], + } ); }); @@ -416,24 +387,17 @@ suite('gr-main-header tests', () => { /* userLinks= */ [], /* adminLinks= */ [], topMenus, - /* baseDocUrl= */ '', defaultLinks - ), - [ - { - title: 'Faves', - links: defaultLinks[0].links.concat([ - { - name: 'Manage', - url: 'https://gerrit/plugins/plugin-manager/static/index.html', - }, - ]), - }, - { - title: 'Browse', - links: [], - }, - ] + )[0], + { + title: 'Faves', + links: defaultLinks[0].links.concat([ + { + name: 'Manage', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, + ]), + } ); }); @@ -462,29 +426,22 @@ suite('gr-main-header tests', () => { userLinks, /* adminLinks= */ [], topMenus, - /* baseDocUrl= */ '', /* defaultLinks= */ [] - ), - [ - { - title: 'Your', - links: [ - { - name: 'Facebook', - url: 'https://facebook.com', - target: '', - }, - { - name: 'Manage', - url: 'https://gerrit/plugins/plugin-manager/static/index.html', - }, - ], - }, - { - title: 'Browse', - links: [], - }, - ] + )[0], + { + title: 'Your', + links: [ + { + name: 'Facebook', + url: 'https://facebook.com', + target: '', + }, + { + name: 'Manage', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, + ], + } ); }); @@ -513,21 +470,18 @@ suite('gr-main-header tests', () => { /* userLinks= */ [], adminLinks, topMenus, - /* baseDocUrl= */ '', /* defaultLinks= */ [] - ), - [ - { - title: 'Browse', - links: [ - adminLinks[0], - { - name: 'Manage', - url: 'https://gerrit/plugins/plugin-manager/static/index.html', - }, - ], - }, - ] + )[1], + { + title: 'Browse', + links: [ + adminLinks[0], + { + name: 'Manage', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, + ], + } ); }); diff --git a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts index a0187f9a8d..98e9eba8c1 100644 --- a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts +++ b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts @@ -30,6 +30,7 @@ import { ValueChangedEvent, } from '../../../types/events'; import {fireNoBubbleNoCompose} from '../../../utils/event-util'; +import {getDocUrl} from '../../../utils/url-util'; // Possible static search options for auto complete, without negations. const SEARCH_OPERATORS: ReadonlyArray<string> = [ @@ -168,7 +169,7 @@ export class GrSearchBar extends LitElement { @state() inputVal = ''; // private but used in test - @state() docsBaseUrl: string | null = null; + @state() docsBaseUrl = ''; @state() private query: AutocompleteQuery; @@ -240,7 +241,7 @@ export class GrSearchBar extends LitElement { <a class="help" slot="suffix" - href=${this.computeHelpDocLink()} + href=${getDocUrl(this.docsBaseUrl, 'user-search.html')} target="_blank" rel="noopener noreferrer" tabindex="-1" @@ -276,18 +277,6 @@ export class GrSearchBar extends LitElement { return set; } - // private but used in test - computeHelpDocLink() { - // fallback to gerrit's official doc - let baseUrl = - this.docsBaseUrl || - 'https://gerrit-review.googlesource.com/Documentation/'; - if (baseUrl.endsWith('/')) { - baseUrl = baseUrl.substring(0, baseUrl.length - 1); - } - return `${baseUrl}/user-search.html`; - } - private handleInputCommit(e: AutocompleteCommitEvent) { this.preventDefaultAndNavigateToInputVal(e); } diff --git a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar_test.ts b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar_test.ts index 2f955de896..f67024fffc 100644 --- a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar_test.ts +++ b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar_test.ts @@ -91,23 +91,6 @@ suite('gr-search-bar tests', () => { ); }); - test('falls back to gerrit docs url', async () => { - const configWithoutDocsUrl = createServerInfo(); - configWithoutDocsUrl.gerrit.doc_url = undefined; - - configModel.updateServerConfig(configWithoutDocsUrl); - await waitUntilObserved( - configModel.docsBaseUrl$, - docsBaseUrl => docsBaseUrl === 'https://mydocumentationurl.google.com/' - ); - await element.updateComplete; - - assert.equal( - queryAndAssert<HTMLAnchorElement>(element, 'a')!.href, - 'https://mydocumentationurl.google.com/user-search.html' - ); - }); - test('value is propagated to inputVal', async () => { element.value = 'foo'; await element.updateComplete; @@ -303,29 +286,4 @@ suite('gr-search-bar tests', () => { }); }); }); - - suite('doc url', () => { - setup(async () => { - element = await fixture(html`<gr-search-bar></gr-search-bar>`); - }); - - test('compute help doc url with correct path', async () => { - element.docsBaseUrl = 'https://doc.com/'; - await element.updateComplete; - assert.equal( - element.computeHelpDocLink(), - 'https://doc.com/user-search.html' - ); - }); - - test('compute help doc url fallback to gerrit url', async () => { - element.docsBaseUrl = null; - await element.updateComplete; - assert.equal( - element.computeHelpDocLink(), - 'https://gerrit-review.googlesource.com/Documentation/' + - 'user-search.html' - ); - }); - }); }); diff --git a/polygerrit-ui/app/models/config/config-model.ts b/polygerrit-ui/app/models/config/config-model.ts index 66ee2e8eba..dd7828b640 100644 --- a/polygerrit-ui/app/models/config/config-model.ts +++ b/polygerrit-ui/app/models/config/config-model.ts @@ -11,7 +11,10 @@ import {ChangeModel} from '../change/change-model'; import {select} from '../../utils/observable-util'; import {Model} from '../base/model'; import {define} from '../dependency'; -import {loginUrl} from '../../utils/url-util'; +import {getBaseUrl, loginUrl} from '../../utils/url-util'; + +export const PROBE_PATH = '/Documentation/index.html'; +export const DOCS_BASE_PATH = '/Documentation'; export interface ConfigState { repoConfig?: ConfigInfo; @@ -56,9 +59,7 @@ export class ConfigModel extends Model<ConfigState> { public docsBaseUrl$ = select( this.serverConfig$.pipe( - switchMap(serverConfig => - from(this.restApiService.getDocsBaseUrl(serverConfig)) - ) + switchMap(serverConfig => from(this.getDocsBaseUrl(serverConfig))) ), url => url ); @@ -86,6 +87,16 @@ export class ConfigModel extends Model<ConfigState> { } // visible for testing + async getDocsBaseUrl(config: ServerInfo | undefined): Promise<string> { + if (config?.gerrit?.doc_url) return config.gerrit.doc_url; + + const ok = await this.restApiService.probePath(getBaseUrl() + PROBE_PATH); + if (ok) return getBaseUrl() + DOCS_BASE_PATH; + + return 'https://gerrit-review.googlesource.com/Documentation'; + } + + // visible for testing updateRepoConfig(repoConfig?: ConfigInfo) { this.updateState({repoConfig}); } diff --git a/polygerrit-ui/app/models/config/config-model_test.ts b/polygerrit-ui/app/models/config/config-model_test.ts new file mode 100644 index 0000000000..b78a93362f --- /dev/null +++ b/polygerrit-ui/app/models/config/config-model_test.ts @@ -0,0 +1,84 @@ +/** + * @license + * Copyright 2023 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ +import '../../test/common-test-setup'; +import {assert} from '@open-wc/testing'; +import {getBaseUrl} from '../../utils/url-util'; +import { + createGerritInfo, + createServerInfo, +} from '../../test/test-data-generators'; +import {ConfigModel} from './config-model'; +import {testResolver} from '../../test/common-test-setup'; +import {getAppContext} from '../../services/app-context'; +import {changeModelToken} from '../change/change-model'; +import {ServerInfo} from '../../api/rest-api'; + +suite('getDocsBaseUrl tests', () => { + let model: ConfigModel; + + setup(async () => { + model = new ConfigModel( + testResolver(changeModelToken), + getAppContext().restApiService + ); + }); + + test('null config', async () => { + const probePathMock = sinon + .stub(model.restApiService, 'probePath') + .resolves(true); + const docsBaseUrl = await model.getDocsBaseUrl(undefined); + assert.equal( + probePathMock.lastCall.args[0], + `${getBaseUrl()}/Documentation/index.html` + ); + assert.equal(docsBaseUrl, `${getBaseUrl()}/Documentation`); + }); + + test('no doc config', async () => { + const probePathMock = sinon + .stub(model.restApiService, 'probePath') + .resolves(true); + const config: ServerInfo = { + ...createServerInfo(), + gerrit: createGerritInfo(), + }; + const docsBaseUrl = await model.getDocsBaseUrl(config); + assert.equal( + probePathMock.lastCall.args[0], + `${getBaseUrl()}/Documentation/index.html` + ); + assert.equal(docsBaseUrl, `${getBaseUrl()}/Documentation`); + }); + + test('has doc config', async () => { + const probePathMock = sinon + .stub(model.restApiService, 'probePath') + .resolves(true); + const config: ServerInfo = { + ...createServerInfo(), + gerrit: {...createGerritInfo(), doc_url: 'foobar'}, + }; + const docsBaseUrl = await model.getDocsBaseUrl(config); + assert.isFalse(probePathMock.called); + assert.equal(docsBaseUrl, 'foobar'); + }); + + test('no probe', async () => { + const probePathMock = sinon + .stub(model.restApiService, 'probePath') + .resolves(false); + const docsBaseUrl = await model.getDocsBaseUrl(undefined); + assert.equal( + probePathMock.lastCall.args[0], + `${getBaseUrl()}/Documentation/index.html` + ); + assert.equal( + docsBaseUrl, + 'https://gerrit-review.googlesource.com/Documentation' + ); + }); +}); diff --git a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts index acc7cdd964..2350594216 100644 --- a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts +++ b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts @@ -151,8 +151,6 @@ import {escapeAndWrapSearchOperatorValue} from '../../utils/string-util'; import {FlagsService, KnownExperimentId} from '../flags/flags'; const MAX_PROJECT_RESULTS = 25; -export const PROBE_PATH = '/Documentation/index.html'; -export const DOCS_BASE_PATH = '/Documentation'; const Requests = { SEND_DIFF_DRAFT: 'sendDiffDraft', @@ -290,8 +288,6 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable { readonly _etags = grEtagDecorator; // Shared across instances. - getDocsBaseUrlCachedPromise: Promise<string | null> | undefined; - // readonly, but set in tests. _projectLookup = projectLookup; // Shared across instances. @@ -3399,26 +3395,6 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable { }) as Promise<DashboardInfo | undefined>; } - /** - * Get the docs base URL from either the server config or by probing. - * - * @return A promise that resolves with the docs base URL. - */ - getDocsBaseUrl(config: ServerInfo | undefined): Promise<string | null> { - if (!this.getDocsBaseUrlCachedPromise) { - this.getDocsBaseUrlCachedPromise = new Promise(resolve => { - if (config?.gerrit?.doc_url) { - resolve(config.gerrit.doc_url); - } else { - this.probePath(getBaseUrl() + PROBE_PATH).then(ok => { - resolve(ok ? getBaseUrl() + DOCS_BASE_PATH : null); - }); - } - }); - } - return this.getDocsBaseUrlCachedPromise; - } - getDocumentationSearches(filter: string): Promise<DocResult[] | undefined> { filter = filter.trim(); const encodedFilter = encodeURIComponent(filter); diff --git a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl_test.ts b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl_test.ts index 624bcb5aa6..1e1439d8e4 100644 --- a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl_test.ts +++ b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl_test.ts @@ -17,7 +17,6 @@ import { createAccountDetailWithId, createChange, createComment, - createGerritInfo, createParsedChange, createServerInfo, } from '../../test/test-data-generators'; @@ -54,7 +53,6 @@ import { RevisionId, RevisionPatchSetNum, RobotCommentInfo, - ServerInfo, Timestamp, UrlEncodedCommentId, } from '../../types/common'; @@ -1629,51 +1627,4 @@ suite('gr-rest-api-service-impl tests', () => { anonymizedUrl: '/accounts/self/starred.changes/*', }); }); - - suite('getDocsBaseUrl tests', () => { - test('null config', async () => { - const probePathMock = sinon.stub(element, 'probePath').resolves(true); - const docsBaseUrl = await element.getDocsBaseUrl(undefined); - assert.equal( - probePathMock.lastCall.args[0], - `${getBaseUrl()}/Documentation/index.html` - ); - assert.equal(docsBaseUrl, `${getBaseUrl()}/Documentation`); - }); - - test('no doc config', async () => { - const probePathMock = sinon.stub(element, 'probePath').resolves(true); - const config: ServerInfo = { - ...createServerInfo(), - gerrit: createGerritInfo(), - }; - const docsBaseUrl = await element.getDocsBaseUrl(config); - assert.equal( - probePathMock.lastCall.args[0], - `${getBaseUrl()}/Documentation/index.html` - ); - assert.equal(docsBaseUrl, `${getBaseUrl()}/Documentation`); - }); - - test('has doc config', async () => { - const probePathMock = sinon.stub(element, 'probePath').resolves(true); - const config: ServerInfo = { - ...createServerInfo(), - gerrit: {...createGerritInfo(), doc_url: 'foobar'}, - }; - const docsBaseUrl = await element.getDocsBaseUrl(config); - assert.isFalse(probePathMock.called); - assert.equal(docsBaseUrl, 'foobar'); - }); - - test('no probe', async () => { - const probePathMock = sinon.stub(element, 'probePath').resolves(false); - const docsBaseUrl = await element.getDocsBaseUrl(undefined); - assert.equal( - probePathMock.lastCall.args[0], - `${getBaseUrl()}/Documentation/index.html` - ); - assert.isNotOk(docsBaseUrl); - }); - }); }); diff --git a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api.ts b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api.ts index c70f7803f3..c96e9783ac 100644 --- a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api.ts +++ b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api.ts @@ -371,8 +371,6 @@ export interface RestApiService extends Finalizable { endpoint: string ): Promise<string>; - getDocsBaseUrl(config?: ServerInfo): Promise<string | null>; - createChange( repo: RepoName, branch: BranchName, diff --git a/polygerrit-ui/app/test/mocks/gr-rest-api_mock.ts b/polygerrit-ui/app/test/mocks/gr-rest-api_mock.ts index e0a16825bb..7969264741 100644 --- a/polygerrit-ui/app/test/mocks/gr-rest-api_mock.ts +++ b/polygerrit-ui/app/test/mocks/gr-rest-api_mock.ts @@ -77,11 +77,6 @@ import { createDefaultPreferences, } from '../../constants/constants'; import {ParsedChangeInfo} from '../../types/types'; -import {getBaseUrl} from '../../utils/url-util'; -import { - DOCS_BASE_PATH, - PROBE_PATH, -} from '../../services/gr-rest-api/gr-rest-api-impl'; export const grRestApiMock: RestApiService = { addAccountEmail(): Promise<Response> { @@ -317,16 +312,6 @@ export const grRestApiMock: RestApiService = { // eslint-disable-next-line @typescript-eslint/no-explicit-any return Promise.resolve({}) as any; }, - getDocsBaseUrl(config?: ServerInfo): Promise<string | null> { - if (config?.gerrit?.doc_url) { - return Promise.resolve(config.gerrit.doc_url); - } else { - return this.probePath(getBaseUrl() + PROBE_PATH).then(ok => - Promise.resolve(ok ? getBaseUrl() + DOCS_BASE_PATH : null) - ); - } - return Promise.resolve(''); - }, getDocumentationSearches(): Promise<DocResult[] | undefined> { return Promise.resolve([]); }, diff --git a/polygerrit-ui/app/utils/url-util.ts b/polygerrit-ui/app/utils/url-util.ts index 3d1bf7e715..96edc7e35e 100644 --- a/polygerrit-ui/app/utils/url-util.ts +++ b/polygerrit-ui/app/utils/url-util.ts @@ -17,6 +17,16 @@ export function getBaseUrl(): string { return self.CANONICAL_PATH || ''; } +export function getDocUrl(docsBaseUrl: string, relativeUrl: string): string { + if (docsBaseUrl.endsWith('/')) { + docsBaseUrl = docsBaseUrl.slice(0, -1); + } + if (relativeUrl.startsWith('/')) { + relativeUrl = relativeUrl.slice(1); + } + return `${docsBaseUrl}/${relativeUrl}`; +} + /** * Return the url to use for login. If the server configuration * contains the `loginUrl` in the `auth` section then that custom url diff --git a/polygerrit-ui/app/utils/url-util_test.ts b/polygerrit-ui/app/utils/url-util_test.ts index f8be92a2ee..a92d8b158c 100644 --- a/polygerrit-ui/app/utils/url-util_test.ts +++ b/polygerrit-ui/app/utils/url-util_test.ts @@ -16,6 +16,7 @@ import { toPathname, toSearchParams, sameOrigin, + getDocUrl, } from './url-util'; import {assert} from '@open-wc/testing'; import {createAuth} from '../test/test-data-generators'; @@ -38,6 +39,15 @@ suite('url-util tests', () => { }); }); + suite('getDocUrl tests', () => { + test('getDocUrl', () => { + assert.deepEqual(getDocUrl('a', 'b'), 'a/b'); + assert.deepEqual(getDocUrl('a/', 'b'), 'a/b'); + assert.deepEqual(getDocUrl('a', '/b'), 'a/b'); + assert.deepEqual(getDocUrl('a/', '/b'), 'a/b'); + }); + }); + suite('loginUrl tests', () => { const authConfig = createAuth(); |