diff --git a/polygerrit-ui/README.md b/polygerrit-ui/README.md index 302551bc83..2fb9e5c5fd 100644 --- a/polygerrit-ui/README.md +++ b/polygerrit-ui/README.md @@ -434,12 +434,10 @@ const reloadStub = sinon .stub(element, '_reload') .callsFake(() => Promise.resolve()); -stub('gr-rest-api-interface', { - getDiffComments() { return Promise.resolve({}); }, - getDiffRobotComments() { return Promise.resolve({}); }, - getDiffDrafts() { return Promise.resolve({}); }, - _fetchSharedCacheURL() { return Promise.resolve({}); }, -}); +stubRestApi('getDiffComments').returns(Promise.resolve({})); +stubRestApi('getDiffRobotComments').returns(Promise.resolve({})); +stubRestApi('getDiffDrafts').returns(Promise.resolve({})); +stubRestApi('_fetchSharedCacheURL').returns(Promise.resolve({})); ``` In such cases, validate the input and output of a stub/fake method. Quite often @@ -451,61 +449,9 @@ const reloadStub = sinon // GrChangeView._reload method returns an array .callsFake(() => Promise.resolve([])); // return [] here -stub('gr-rest-api-interface', { ... // Fix return type: - _fetchSharedCacheURL() { return Promise.resolve({} as ParsedJSON); }, -}); -``` - -If a method has multiple overloads, you can use one of 2 options: -``` -// Option 1: less accurate, but shorter: -function getCommentsStub() { - return Promise.resolve({}); -} - -stub('gr-rest-api-interface', { - ... - getDiffComments: (getCommentsStub as unknown) as RestApiService['getDiffComments'], - getDiffRobotComments: (getCommentsStub as unknown) as RestApiService['getDiffRobotComments'], - getDiffDrafts: (getCommentsStub as unknown) as RestApiService['getDiffDrafts'], - ... -}); - -// Option 2: more accurate, but longer. -// Step 1: define the same overloads for stub: -function getDiffCommentsStub( - changeNum: NumericChangeId -): Promise; -function getDiffCommentsStub( - changeNum: NumericChangeId, - basePatchNum: PatchSetNum, - patchNum: PatchSetNum, - path: string -): Promise; - -// Step 2: implement stub method for differnt input -function getDiffCommentsStub( - _: NumericChangeId, - basePatchNum?: PatchSetNum, -): - | Promise - | Promise { - if (basePatchNum) { - return Promise.resolve({ - baseComments: [], - comments: [], - }); - } - return Promise.resolve({}); -} - -// Step 3: use stubbed function: -stub('gr-rest-api-interface', { - ... - getDiffComments: getDiffCommentsStub, - ... + stubRestApi('_fetchSharedCacheURL').returns(Promise.resolve({} as ParsedJSON)); }); ``` diff --git a/polygerrit-ui/app/elements/change/gr-confirm-cherrypick-dialog/gr-confirm-cherrypick-dialog.ts b/polygerrit-ui/app/elements/change/gr-confirm-cherrypick-dialog/gr-confirm-cherrypick-dialog.ts index c4526bb711..285571635c 100644 --- a/polygerrit-ui/app/elements/change/gr-confirm-cherrypick-dialog/gr-confirm-cherrypick-dialog.ts +++ b/polygerrit-ui/app/elements/change/gr-confirm-cherrypick-dialog/gr-confirm-cherrypick-dialog.ts @@ -35,10 +35,7 @@ import { } from '../../../types/common'; import {ReportingService} from '../../../services/gr-reporting/gr-reporting'; import {customElement, property, observe} from '@polymer/decorators'; -import { - GrAutocomplete, - AutocompleteSuggestion, -} from '../../shared/gr-autocomplete/gr-autocomplete'; +import {AutocompleteSuggestion} from '../../shared/gr-autocomplete/gr-autocomplete'; import {HttpMethod, ChangeStatus} from '../../../constants/constants'; import {hasOwnProperty} from '../../../utils/common-util'; import {dom, EventApi} from '@polymer/polymer/lib/legacy/polymer.dom'; @@ -71,11 +68,9 @@ declare global { } } -// TODO(TS): add type after gr-autocomplete and gr-rest-api-interface -// is converted export interface GrConfirmCherrypickDialog { $: { - branchInput: GrAutocomplete; + branchInput: HTMLElement; }; } diff --git a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts index 54fdb8dc80..312e042f44 100644 --- a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts +++ b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts @@ -776,9 +776,9 @@ export class GrReplyDialog extends KeyboardShortcutMixin( let response: Response = r; // A call to _saveReview could fail with a server error if erroneous // reviewers were requested. This is signalled with a 400 Bad Request - // status. The default gr-rest-api-interface error handling would - // result in a large JSON response body being displayed to the user in - // the gr-error-manager toast. + // status. The default gr-rest-api error handling would result in a large + // JSON response body being displayed to the user in the gr-error-manager + // toast. // // We can modify the error handling behavior by passing this function // through to restAPI as a custom error handling function. Since we're diff --git a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog_test.js b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog_test.js index 7b045a7621..4c4dc52272 100644 --- a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog_test.js +++ b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog_test.js @@ -23,7 +23,7 @@ import {SpecialFilePath} from '../../../constants/constants.js'; import {appContext} from '../../../services/app-context.js'; import {addListenerForTest} from '../../../test/test-utils.js'; import {stubRestApi} from '../../../test/test-utils.js'; -import {JSON_PREFIX} from '../../shared/gr-rest-api-interface/gr-rest-api-interface.js'; +import {JSON_PREFIX} from '../../shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.js'; const basicFixture = fixtureFromElement('gr-reply-dialog'); diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-gerrit.ts b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-gerrit.ts index 74b84bc8f7..32a9238bd9 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-gerrit.ts +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-gerrit.ts @@ -168,7 +168,7 @@ function initGerritPluginsMethods(globalGerritObj: GerritGlobal) { 'Gerrit.getLoggedIn() is deprecated! ' + 'Use plugin.restApi().getLoggedIn()' ); - return document.createElement('gr-rest-api-interface').getLoggedIn(); + return appContext.restApiService.getLoggedIn(); }; globalGerritObj.get = ( diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts index 61d64a860e..bc720c8cbf 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts @@ -47,6 +47,7 @@ import {HttpMethod} from '../../../constants/constants'; import {JsApiService} from './gr-js-api-types'; import {GrChangeActions} from '../../change/gr-change-actions/gr-change-actions'; import {GrChecksApi} from '../../plugins/gr-checks-api/gr-checks-api'; +import {appContext} from '../../../services/app-context'; /** * Plugin-provided custom components can affect content in extension @@ -168,7 +169,7 @@ export class Plugin implements PluginApi { } getServerInfo() { - return document.createElement('gr-rest-api-interface').getConfig(); + return appContext.restApiService.getConfig(); } // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts index 6a32c7f906..61e9ff275d 100644 --- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts +++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts @@ -49,90 +49,90 @@ import { AccountExternalIdInfo, AccountId, AccountInfo, + ActionNameToActionInfoMap, AssigneeInput, Base64File, Base64FileContent, Base64ImageFile, + BlameInfo, BranchInfo, + BranchInput, BranchName, + CapabilityInfoMap, ChangeId, ChangeInfo, ChangeMessageId, + ChangeViewChangeInfo, CommentInfo, CommentInput, CommitId, CommitInfo, ConfigInfo, ConfigInput, + ContributorAgreementInfo, + ContributorAgreementInput, DashboardId, DashboardInfo, DeleteDraftCommentsInput, DiffPreferenceInput, + DocResult, + EditInfo, EditPatchSetNum, EditPreferencesInfo, + EmailAddress, + EmailInfo, EncodedGroupId, + FileNameToFileInfoMap, + FilePathToDiffInfoMap, + FixId, GitRef, GpgKeyId, + GpgKeyInfo, + GpgKeysInput, + GroupAuditEventInfo, GroupId, GroupInfo, GroupInput, + GroupName, + GroupNameToGroupInfoMap, GroupOptionsInput, + Hashtag, HashtagsInput, ImagesForDiff, + IncludedInInfo, + MergeableInfo, NameToProjectInfoMap, + NumericChangeId, ParentPatchSetNum, ParsedJSON, + Password, PatchRange, PatchSetNum, PathToCommentsInfoMap, PathToRobotCommentsInfoMap, + PluginInfo, PreferencesInfo, PreferencesInput, + ProjectAccessInfo, ProjectAccessInfoMap, ProjectAccessInput, ProjectInfo, + ProjectInfoWithName, ProjectInput, ProjectWatchInfo, + RelatedChangesInfo, RepoName, + RequestPayload, ReviewInput, + RevisionId, ServerInfo, SshKeyInfo, - UrlEncodedCommentId, - EditInfo, - FileNameToFileInfoMap, - SuggestedReviewerInfo, - GroupNameToGroupInfoMap, - GroupAuditEventInfo, - RequestPayload, - Password, - ContributorAgreementInput, - ContributorAgreementInfo, - BranchInput, - IncludedInInfo, - TagInput, - PluginInfo, - GpgKeyInfo, - GpgKeysInput, - DocResult, - EmailInfo, - ProjectAccessInfo, - CapabilityInfoMap, - ProjectInfoWithName, - TagInfo, - RelatedChangesInfo, SubmittedTogetherInfo, - NumericChangeId, - EmailAddress, - FixId, - FilePathToDiffInfoMap, - ChangeViewChangeInfo, - BlameInfo, - ActionNameToActionInfoMap, - RevisionId, - GroupName, - Hashtag, + SuggestedReviewerInfo, + TagInfo, + TagInput, TopMenuEntryInfo, - MergeableInfo, + UrlEncodedCommentId, } from '../../../types/common'; import { DiffInfo, @@ -142,9 +142,9 @@ import { import { CancelConditionCallback, ErrorCallback, - RestApiService, GetDiffCommentsOutput, GetDiffRobotCommentsOutput, + RestApiService, } from '../../../services/gr-rest-api/gr-rest-api'; import { CommentSide, @@ -158,7 +158,6 @@ import { import {firePageError, fireServerError} from '../../../utils/event-util'; import {ParsedChangeInfo} from '../../../types/types'; -export const JSON_PREFIX = ")]}'"; const MAX_PROJECT_RESULTS = 25; // This value is somewhat arbitrary and not based on research or calculations. const MAX_UNIFIED_DEFAULT_WINDOW_WIDTH_PX = 850; @@ -301,8 +300,6 @@ declare global { export class GrRestApiInterface extends GestureEventListeners(LegacyElementMixin(PolymerElement)) implements RestApiService { - readonly JSON_PREFIX = JSON_PREFIX; - @property({type: Object}) readonly _cache = siteBasedCache; // Shared across instances. diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js index 42030c7414..4c80db56c2 100644 --- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js +++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js @@ -27,7 +27,7 @@ import { parsePrefixedJSON, readResponsePayload, } from './gr-rest-apis/gr-rest-api-helper.js'; -import {JSON_PREFIX} from './gr-rest-api-interface.js'; +import {JSON_PREFIX} from './gr-rest-apis/gr-rest-api-helper.js'; const basicFixture = fixtureFromElement('gr-rest-api-interface'); @@ -946,8 +946,7 @@ suite('gr-rest-api-interface tests', () => { setup(() => { requestUrl = '/foo/bar'; const mockResponse = {foo: 'bar', baz: 42}; - mockResponseSerial = element.JSON_PREFIX + - JSON.stringify(mockResponse); + mockResponseSerial = JSON_PREFIX + JSON.stringify(mockResponse); sinon.stub(element._restApiHelper, 'urlWithParams') .returns(requestUrl); sinon.stub(element, 'getChangeActionURL') diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts index 1c5d2426ad..d6b6645fe0 100644 --- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts +++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts @@ -35,7 +35,7 @@ import {RpcLogEventDetail} from '../../../../types/events'; import {fireNetworkError, fireServerError} from '../../../../utils/event-util'; import {FetchRequest} from '../../../../types/types'; -const JSON_PREFIX = ")]}'"; +export const JSON_PREFIX = ")]}'"; export interface ResponsePayload { // TODO(TS): readResponsePayload can assign null to the parsed property if diff --git a/polygerrit-ui/app/types/common.ts b/polygerrit-ui/app/types/common.ts index beb86f422f..d60e4760be 100644 --- a/polygerrit-ui/app/types/common.ts +++ b/polygerrit-ui/app/types/common.ts @@ -1612,7 +1612,7 @@ export interface HashtagsInput { } /** - * Defines a patch ranges. Used as input for gr-rest-api-interface methods, + * Defines a patch ranges. Used as input for gr-rest-api methods, * doesn't exist in Rest API */ export interface PatchRange {