From 35aea69d311b270868b1811f81d10c7ed3bfa9e5 Mon Sep 17 00:00:00 2001 From: Dmitrii Filippov Date: Tue, 7 Apr 2020 12:14:11 +0200 Subject: [PATCH] Get rid of global Gerrit Api methods Change-Id: I240680b46956e5d50eaf3857b14e331596c44e01 --- .../admin/gr-admin-view/gr-admin-view.js | 3 +- .../gr-change-list-item.js | 3 +- .../gr-change-list/gr-change-list.js | 3 +- .../gr-change-actions/gr-change-actions.js | 3 +- .../gr-change-metadata-it_test.html | 3 + .../gr-change-metadata_test.html | 6 +- .../change/gr-change-view/gr-change-view.js | 4 +- .../gr-change-view/gr-change-view_test.html | 5 +- .../change/gr-file-list/gr-file-list.js | 3 +- .../gr-reply-dialog-it_test.html | 6 +- .../gr-error-manager_test.html | 4 + .../core/gr-main-header/gr-main-header.js | 3 +- .../app/elements/custom-dark-theme_test.html | 3 +- .../app/elements/custom-light-theme_test.html | 3 +- polygerrit-ui/app/elements/gr-app.js | 9 +- .../gr-admin-api/gr-admin-api_test.html | 7 +- .../gr-dom-hooks/gr-dom-hooks_test.html | 3 + .../gr-endpoint-decorator.js | 3 +- .../gr-endpoint-decorator_test.html | 9 +- .../gr-external-style/gr-external-style.js | 3 +- .../gr-external-style_test.html | 4 + .../plugins/gr-plugin-host/gr-plugin-host.js | 5 +- .../gr-popup-interface_test.html | 2 + .../plugins/gr-repo-api/gr-repo-api_test.html | 6 +- .../gr-settings-api/gr-settings-api_test.html | 6 +- .../gr-styles-api/gr-styles-api_test.html | 9 +- .../gr-theme-api/gr-theme-api_test.html | 6 +- .../elements/shared/gr-avatar/gr-avatar.js | 3 +- .../shared/gr-avatar/gr-avatar_test.html | 14 +- .../gr-event-interface_test.html | 4 + .../gr-annotation-actions-context_test.html | 2 + .../gr-annotation-actions-js-api_test.html | 4 + .../gr-change-actions-js-api_test.html | 7 +- .../gr-change-reply-js-api_test.html | 4 + .../shared/gr-js-api-interface/gr-gerrit.js | 237 +++++++++--------- .../gr-js-api-interface/gr-gerrit_test.html | 3 + .../gr-js-api-interface-element.js | 5 +- .../gr-js-api-interface_test.html | 8 +- .../gr-plugin-action-context_test.html | 3 + .../gr-plugin-endpoints.js | 4 +- .../gr-plugin-endpoints_test.html | 3 + .../gr-plugin-loader_test.html | 65 ++--- .../gr-plugin-rest-api_test.html | 3 + .../gr-js-api-interface/gr-public-js-api.js | 9 +- 44 files changed, 307 insertions(+), 195 deletions(-) diff --git a/polygerrit-ui/app/elements/admin/gr-admin-view/gr-admin-view.js b/polygerrit-ui/app/elements/admin/gr-admin-view/gr-admin-view.js index a0841f805c..b318ee6650 100644 --- a/polygerrit-ui/app/elements/admin/gr-admin-view/gr-admin-view.js +++ b/polygerrit-ui/app/elements/admin/gr-admin-view/gr-admin-view.js @@ -43,6 +43,7 @@ import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-beh import {AdminNavBehavior} from '../../../behaviors/gr-admin-nav-behavior/gr-admin-nav-behavior.js'; import {URLEncodingBehavior} from '../../../behaviors/gr-url-encoding-behavior/gr-url-encoding-behavior.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; const INTERNAL_GROUP_REGEX = /^[\da-f]{40}$/; @@ -118,7 +119,7 @@ class GrAdminView extends mixinBehaviors( [ reload() { const promises = [ this.$.restAPI.getAccount(), - Gerrit.awaitPluginsLoaded(), + pluginLoader.awaitPluginsLoaded(), ]; return Promise.all(promises).then(result => { this._account = result[0]; diff --git a/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.js b/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.js index 2fa287c9c1..da13492389 100644 --- a/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.js +++ b/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.js @@ -39,6 +39,7 @@ import {URLEncodingBehavior} from '../../../behaviors/gr-url-encoding-behavior/g import {RESTClientBehavior} from '../../../behaviors/rest-client-behavior/rest-client-behavior.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; import {pluginEndpoints} from '../../shared/gr-js-api-interface/gr-plugin-endpoints.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; const CHANGE_SIZE = { XS: 10, @@ -99,7 +100,7 @@ class GrChangeListItem extends mixinBehaviors( [ /** @override */ attached() { super.attached(); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { this._dynamicCellEndpoints = pluginEndpoints.getDynamicEndpoints( 'change-list-item-cell'); }); diff --git a/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.js b/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.js index 48ac61e916..0a19ae179b 100644 --- a/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.js +++ b/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.js @@ -37,6 +37,7 @@ import {KeyboardShortcutBehavior} from '../../../behaviors/keyboard-shortcut-beh import {RESTClientBehavior} from '../../../behaviors/rest-client-behavior/rest-client-behavior.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; import {pluginEndpoints} from '../../shared/gr-js-api-interface/gr-plugin-endpoints.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; const NUMBER_FIXED_COLUMNS = 3; const CLOSED_STATUS = ['MERGED', 'ABANDONED']; @@ -177,7 +178,7 @@ class GrChangeList extends mixinBehaviors( [ /** @override */ attached() { super.attached(); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { this._dynamicHeaderEndpoints = pluginEndpoints.getDynamicEndpoints( 'change-list-header'); }); diff --git a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js index 2cd5de533a..d65757a84d 100644 --- a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js +++ b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js @@ -43,6 +43,7 @@ import {htmlTemplate} from './gr-change-actions_html.js'; import {PatchSetBehavior} from '../../../behaviors/gr-patch-set-behavior/gr-patch-set-behavior.js'; import {RESTClientBehavior} from '../../../behaviors/rest-client-behavior/rest-client-behavior.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; const ERR_BRANCH_EMPTY = 'The destination branch can’t be empty.'; const ERR_COMMIT_EMPTY = 'The commit message can’t be empty.'; @@ -533,7 +534,7 @@ class GrChangeActions extends mixinBehaviors( [ } _handleLoadingComplete() { - Gerrit.awaitPluginsLoaded().then(() => this._loading = false); + pluginLoader.awaitPluginsLoaded().then(() => this._loading = false); } _sendShowRevisionActions(detail) { diff --git a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html index 8c9eedd299..4fa474253a 100644 --- a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html +++ b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html @@ -43,6 +43,9 @@ import './gr-change-metadata.js'; import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js'; import {resetPlugins} from '../../../test/test-utils.js'; import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; +import {initGerritPluginApi} from '../../shared/gr-js-api-interface/gr-gerrit.js'; + +initGerritPluginApi(); suite('gr-change-metadata integration tests', () => { let sandbox; diff --git a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata_test.html b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata_test.html index 3288fb065c..36b0caa69a 100644 --- a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata_test.html +++ b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata_test.html @@ -35,6 +35,10 @@ import '../../../test/common-test-setup.js'; import '../../core/gr-router/gr-router.js'; import './gr-change-metadata.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; +import {initGerritPluginApi} from '../../shared/gr-js-api-interface/gr-gerrit.js'; + +initGerritPluginApi(); suite('gr-change-metadata tests', () => { let element; @@ -758,7 +762,7 @@ suite('gr-change-metadata tests', () => { }, '0.1', 'http://some/plugins/url.html'); - Gerrit._loadPlugins([]); + pluginLoader.loadPlugins([]); flush(() => { assert.strictEqual(hookEl.plugin, plugin); assert.strictEqual(hookEl.change, element.change); diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js index c9869ce7f1..edd6ed80db 100644 --- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js +++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js @@ -483,7 +483,7 @@ class GrChangeView extends mixinBehaviors( [ this._setDiffViewMode(); }); - Gerrit.awaitPluginsLoaded() + pluginLoader.awaitPluginsLoaded() .then(() => { this._dynamicTabHeaderEndpoints = pluginEndpoints.getDynamicEndpoints('change-view-tab-header'); @@ -1164,7 +1164,7 @@ class GrChangeView extends mixinBehaviors( [ } _maybeShowRevertDialog() { - Gerrit.awaitPluginsLoaded() + pluginLoader.awaitPluginsLoaded() .then(this._getLoggedIn.bind(this)) .then(loggedIn => { if (!loggedIn || !this._change || diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html index 2586b4e184..1f0af30a7c 100644 --- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html +++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html @@ -50,6 +50,9 @@ import {_testOnly_resetEndpoints} from '../../shared/gr-js-api-interface/gr-plug import {util} from '../../../scripts/util.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; +import {initGerritPluginApi} from '../../shared/gr-js-api-interface/gr-gerrit.js'; + +initGerritPluginApi(); suite('gr-change-view tests', () => { const kb = KeyboardShortcutBinder; @@ -311,7 +314,7 @@ suite('gr-change-view tests', () => { }); element = fixture('basic'); sandbox.stub(element.$.actions, 'reload').returns(Promise.resolve()); - Gerrit._loadPlugins([]); + pluginLoader.loadPlugins([]); Gerrit.install( plugin => { plugin.registerDynamicCustomComponent( diff --git a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.js b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.js index 90f9ed7ec9..eb85cd7e43 100644 --- a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.js +++ b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.js @@ -45,6 +45,7 @@ import {GrFileListConstants} from '../gr-file-list-constants.js'; import {GrCountStringFormatter} from '../../shared/gr-count-string-formatter/gr-count-string-formatter.js'; import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; import {pluginEndpoints} from '../../shared/gr-js-api-interface/gr-plugin-endpoints.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; // Maximum length for patch set descriptions. const PATCH_DESC_MAX_LENGTH = 500; @@ -299,7 +300,7 @@ class GrFileList extends mixinBehaviors( [ /** @override */ attached() { super.attached(); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { this._dynamicHeaderEndpoints = pluginEndpoints.getDynamicEndpoints( 'change-view-file-list-header'); this._dynamicContentEndpoints = pluginEndpoints.getDynamicEndpoints( diff --git a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog-it_test.html b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog-it_test.html index 4f921bd689..f7e41159ac 100644 --- a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog-it_test.html +++ b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog-it_test.html @@ -42,6 +42,10 @@ import '../../plugins/gr-plugin-host/gr-plugin-host.js'; import './gr-reply-dialog.js'; import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js'; import {resetPlugins} from '../../../test/test-utils.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; +import {initGerritPluginApi} from '../../shared/gr-js-api-interface/gr-gerrit.js'; + +initGerritPluginApi(); suite('gr-reply-dialog tests', () => { let element; @@ -149,7 +153,7 @@ suite('gr-reply-dialog tests', () => { const importSpy = sandbox.spy(element.shadowRoot .querySelector('gr-endpoint-decorator'), '_import'); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { Promise.all(importSpy.returnValues).then(() => { flush(() => { const textarea = element.$.textarea.getNativeTextarea(); diff --git a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html index 44c3cdedf7..8788ef098f 100644 --- a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html +++ b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html @@ -39,6 +39,10 @@ void (0); import '../../../test/common-test-setup.js'; import './gr-error-manager.js'; import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js'; +import {initGerritPluginApi} from '../../shared/gr-js-api-interface/gr-gerrit.js'; + +initGerritPluginApi(); + suite('gr-error-manager tests', () => { let element; let sandbox; diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js index d8188b52e0..3294f5f6e9 100644 --- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js +++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js @@ -31,6 +31,7 @@ import {htmlTemplate} from './gr-main-header_html.js'; import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js'; import {DocsUrlBehavior} from '../../../behaviors/docs-url-behavior/docs-url-behavior.js'; import {AdminNavBehavior} from '../../../behaviors/gr-admin-nav-behavior/gr-admin-nav-behavior.js'; +import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js'; const DEFAULT_LINKS = [{ title: 'Changes', @@ -266,7 +267,7 @@ class GrMainHeader extends mixinBehaviors( [ const promises = [ this.$.restAPI.getAccount(), this.$.restAPI.getTopMenus(), - Gerrit.awaitPluginsLoaded(), + pluginLoader.awaitPluginsLoaded(), ]; return Promise.all(promises).then(result => { diff --git a/polygerrit-ui/app/elements/custom-dark-theme_test.html b/polygerrit-ui/app/elements/custom-dark-theme_test.html index 3d707ea8fe..c04e4d06ec 100644 --- a/polygerrit-ui/app/elements/custom-dark-theme_test.html +++ b/polygerrit-ui/app/elements/custom-dark-theme_test.html @@ -34,6 +34,7 @@ limitations under the License. import '../test/common-test-setup.js'; import './gr-app.js'; import {util} from '../scripts/util.js'; +import {pluginLoader} from './shared/gr-js-api-interface/gr-plugin-loader.js'; suite('gr-app custom dark theme tests', () => { let sandbox; @@ -74,7 +75,7 @@ suite('gr-app custom dark theme tests', () => { const importForThemeSpy = sandbox.spy( element.$['app-element'].$.externalStyleForTheme, '_import'); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { Promise.all(importSpy.returnValues.concat(importForThemeSpy.returnValues)) .then(() => { flush(done); diff --git a/polygerrit-ui/app/elements/custom-light-theme_test.html b/polygerrit-ui/app/elements/custom-light-theme_test.html index 5ca89f220c..fbd382594b 100644 --- a/polygerrit-ui/app/elements/custom-light-theme_test.html +++ b/polygerrit-ui/app/elements/custom-light-theme_test.html @@ -34,6 +34,7 @@ limitations under the License. import '../test/common-test-setup.js'; import './gr-app.js'; import {util} from '../scripts/util.js'; +import {pluginLoader} from './shared/gr-js-api-interface/gr-plugin-loader.js'; suite('gr-app custom light theme tests', () => { let sandbox; @@ -74,7 +75,7 @@ suite('gr-app custom light theme tests', () => { const importForThemeSpy = sandbox.spy( element.$['app-element'].$.externalStyleForTheme, '_import'); - Gerrit.awaitPluginsLoaded().then(() => { + pluginLoader.awaitPluginsLoaded().then(() => { Promise.all(importSpy.returnValues.concat(importForThemeSpy.returnValues)) .then(() => { flush(done); diff --git a/polygerrit-ui/app/elements/gr-app.js b/polygerrit-ui/app/elements/gr-app.js index ce6d613ac6..c9910d39e0 100644 --- a/polygerrit-ui/app/elements/gr-app.js +++ b/polygerrit-ui/app/elements/gr-app.js @@ -35,11 +35,6 @@ setCancelSyntheticClickEvents(false); import 'polymer-resin/standalone/polymer-resin.js'; import {initGlobalVariables} from './gr-app-global-var-init.js'; -// Initialize global variables before any other imports -// If a plugin is loaded in one of the subsequent import (with async/await, for -// example), then all global variables must be initialized at this time -initGlobalVariables(); - import './gr-app-element.js'; import './change-list/gr-embed-dashboard/gr-embed-dashboard.js'; import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; @@ -47,6 +42,7 @@ import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mix import {PolymerElement} from '@polymer/polymer/polymer-element.js'; import {htmlTemplate} from './gr-app_html.js'; import {SafeTypes} from '../behaviors/safe-types-behavior/safe-types-behavior.js'; +import {initGerritPluginApi} from './shared/gr-js-api-interface/gr-gerrit.js'; security.polymer_resin.install({ allowedIdentifierPrefixes: [''], @@ -64,3 +60,6 @@ class GrApp extends GestureEventListeners( } customElements.define(GrApp.is, GrApp); + +initGlobalVariables(); +initGerritPluginApi(); diff --git a/polygerrit-ui/app/elements/plugins/gr-admin-api/gr-admin-api_test.html b/polygerrit-ui/app/elements/plugins/gr-admin-api/gr-admin-api_test.html index 41e88a1e0b..89f9cdb442 100644 --- a/polygerrit-ui/app/elements/plugins/gr-admin-api/gr-admin-api_test.html +++ b/polygerrit-ui/app/elements/plugins/gr-admin-api/gr-admin-api_test.html @@ -27,6 +27,11 @@ limitations under the License.