
Formerly, gr-diff would make its own REST calls for diff comments in the patch range and path being viewed. However, these calls by the diff view were redundant to the more general comment requests made by either gr-diff-view (which requests all comments to support skipping to the next file with comments) or gr-change-view (which requests comments individually for each inline diff). With this change, the diff component no longer loads comments for itself, but is rather provided the comments from its host view via a public property. In this way the host view can load the more-general comment data, and filter it down for the diff view's params. Introduces the gr-comment-api component to simplify loading and filtering diff comments for use by diff views. By using this new component: * The diff view makes only one request for comments (including drafts and robot comments) to support skipping to the next comment and displaying comments in the diff view. * The change view makes only one request for comments for all inline diffs. Bug: Issue 5299 Change-Id: Ia14a01619f1f9881aa0d253fd3f49af9a17f3dce
175 lines
5.6 KiB
JavaScript
175 lines
5.6 KiB
JavaScript
// Copyright (C) 2017 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
(function() {
|
|
'use strict';
|
|
|
|
const PARENT = 'PARENT';
|
|
|
|
Polymer({
|
|
is: 'gr-comment-api',
|
|
|
|
properties: {
|
|
_changeNum: Number,
|
|
_comments: Object,
|
|
_drafts: Object,
|
|
_robotComments: Object,
|
|
},
|
|
|
|
behaviors: [
|
|
Gerrit.PatchSetBehavior,
|
|
],
|
|
|
|
/**
|
|
* Load all comments (with drafts and robot comments) for the given change
|
|
* number. The returned promise resolves when the comments have loaded, but
|
|
* does not yield the comment data.
|
|
*
|
|
* @param {!number} changeNum
|
|
* @return {!Promise}
|
|
*/
|
|
loadAll(changeNum) {
|
|
this._changeNum = changeNum;
|
|
|
|
// Reset comment arrays.
|
|
this._comments = undefined;
|
|
this._drafts = undefined;
|
|
this._robotComments = undefined;
|
|
|
|
const promises = [];
|
|
promises.push(this.$.restAPI.getDiffComments(changeNum)
|
|
.then(comments => { this._comments = comments; }));
|
|
promises.push(this.$.restAPI.getDiffRobotComments(changeNum)
|
|
.then(robotComments => { this._robotComments = robotComments; }));
|
|
promises.push(this.$.restAPI.getLoggedIn()
|
|
.then(loggedIn => {
|
|
if (!loggedIn) { return Promise.resolve({}); }
|
|
return this.$.restAPI.getDiffDrafts(changeNum);
|
|
})
|
|
.then(drafts => { this._drafts = drafts; }));
|
|
|
|
return Promise.all(promises);
|
|
},
|
|
|
|
/**
|
|
* Get an object mapping file paths to a boolean representing whether that
|
|
* path contains diff comments in the given patch set (including drafts and
|
|
* robot comments).
|
|
*
|
|
* Paths with comments are mapped to true, whereas paths without comments
|
|
* are not mapped.
|
|
*
|
|
* @param {!Object} patchRange The patch-range object containing patchNum
|
|
* and basePatchNum properties to represent the range.
|
|
* @return {Object}
|
|
*/
|
|
getPaths(patchRange) {
|
|
const responses = [this._comments, this._drafts, this._robotComments];
|
|
const commentMap = {};
|
|
for (const response of responses) {
|
|
for (const path in response) {
|
|
if (response.hasOwnProperty(path) &&
|
|
response[path].some(c => this._isInPatchRange(c, patchRange))) {
|
|
commentMap[path] = true;
|
|
}
|
|
}
|
|
}
|
|
return commentMap;
|
|
},
|
|
|
|
/**
|
|
* Get the comments (with drafts and robot comments) for a path and
|
|
* patch-range. Returns an object with left and right properties mapping to
|
|
* arrays of comments in on either side of the patch range for that path.
|
|
*
|
|
* @param {!string} path
|
|
* @param {!Object} patchRange The patch-range object containing patchNum
|
|
* and basePatchNum properties to represent the range.
|
|
* @param {Object} opt_projectConfig Optional project config object to
|
|
* include in the meta sub-object.
|
|
* @return {Object}
|
|
*/
|
|
getCommentsForPath(path, patchRange, opt_projectConfig) {
|
|
const comments = this._comments[path] || [];
|
|
const drafts = this._drafts[path] || [];
|
|
const robotComments = this._robotComments[path] || [];
|
|
|
|
drafts.forEach(d => { d.__draft = true; });
|
|
|
|
const all = comments.concat(drafts).concat(robotComments);
|
|
|
|
const baseComments = all.filter(c =>
|
|
this._isInBaseOfPatchRange(c, patchRange));
|
|
const revisionComments = all.filter(c =>
|
|
this._isInRevisionOfPatchRange(c, patchRange));
|
|
|
|
return {
|
|
meta: {
|
|
changeNum: this._changeNum,
|
|
path,
|
|
patchRange,
|
|
projectConfig: opt_projectConfig,
|
|
},
|
|
left: baseComments,
|
|
right: revisionComments,
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Whether the given comment should be included in the base side of the
|
|
* given patch range.
|
|
* @param {!Object} comment
|
|
* @param {!Object} range
|
|
* @return {boolean}
|
|
*/
|
|
_isInBaseOfPatchRange(comment, range) {
|
|
// If the base of the range is the parent of the patch:
|
|
if (range.basePatchNum === PARENT &&
|
|
comment.side === PARENT &&
|
|
this.patchNumEquals(comment.patch_set, range.patchNum)) {
|
|
return true;
|
|
}
|
|
// If the base of the range is not the parent of the patch:
|
|
if (range.basePatchNum !== PARENT &&
|
|
comment.side !== PARENT &&
|
|
this.patchNumEquals(comment.patch_set, range.basePatchNum)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
|
|
/**
|
|
* Whether the given comment should be included in the revision side of the
|
|
* given patch range.
|
|
* @param {!Object} comment
|
|
* @param {!Object} range
|
|
* @return {boolean}
|
|
*/
|
|
_isInRevisionOfPatchRange(comment, range) {
|
|
return comment.side !== PARENT &&
|
|
this.patchNumEquals(comment.patch_set, range.patchNum);
|
|
},
|
|
|
|
/**
|
|
* Whether the given comment should be included in the given patch range.
|
|
* @param {!Object} comment
|
|
* @param {!Object} range
|
|
* @return {boolean}
|
|
*/
|
|
_isInPatchRange(comment, range) {
|
|
return this._isInBaseOfPatchRange(comment, range) ||
|
|
this._isInRevisionOfPatchRange(comment, range);
|
|
},
|
|
});
|
|
})();
|