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