summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts')
-rw-r--r--polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts174
1 files changed, 43 insertions, 131 deletions
diff --git a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
index fc22a58a1e..a08bf39d5f 100644
--- a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
+++ b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
@@ -14,16 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import {PolymerElement} from '@polymer/polymer/polymer-element';
-import {htmlTemplate} from './gr-comment-api_html';
-import {customElement, property} from '@polymer/decorators';
import {
- CommentBasics,
PatchRange,
PatchSetNum,
RobotCommentInfo,
UrlEncodedCommentId,
- NumericChangeId,
PathToCommentsInfoMap,
FileInfo,
ParentPatchSetNum,
@@ -35,18 +30,14 @@ import {
CommentThread,
DraftInfo,
isUnresolved,
- UIComment,
createCommentThreads,
isInPatchRange,
isDraftThread,
- isInBaseOfPatchRange,
- isInRevisionOfPatchRange,
isPatchsetLevel,
addPath,
} from '../../../utils/comment-util';
import {PatchSetFile, PatchNumOnly, isPatchSetFile} from '../../../types/types';
-import {appContext} from '../../../services/app-context';
-import {CommentSide, Side} from '../../../constants/constants';
+import {CommentSide} from '../../../constants/constants';
import {pluralize} from '../../../utils/string-util';
import {NormalizedFileInfo} from '../../change/gr-file-list/gr-file-list';
@@ -70,11 +61,11 @@ export class ChangeComments {
* elements of that which uses the gr-comment-api.
*/
constructor(
- comments: PathToCommentsInfoMap | undefined,
- robotComments: {[path: string]: RobotCommentInfo[]} | undefined,
- drafts: {[path: string]: DraftInfo[]} | undefined,
- portedComments: PathToCommentsInfoMap | undefined,
- portedDrafts: PathToCommentsInfoMap | undefined
+ comments?: PathToCommentsInfoMap,
+ robotComments?: {[path: string]: RobotCommentInfo[]},
+ drafts?: {[path: string]: DraftInfo[]},
+ portedComments?: PathToCommentsInfoMap,
+ portedDrafts?: PathToCommentsInfoMap
) {
this._comments = addPath(comments);
this._robotComments = addPath(robotComments);
@@ -119,7 +110,7 @@ export class ChangeComments {
* patchNum and basePatchNum properties to represent the range.
*/
getPaths(patchRange?: PatchRange): CommentMap {
- const responses: {[path: string]: UIComment[]}[] = [
+ const responses: {[path: string]: Comment[]}[] = [
this._comments,
this.drafts,
this._robotComments,
@@ -144,25 +135,11 @@ export class ChangeComments {
}
/**
- * Gets all the comments for a particular thread group. Used for refreshing
- * comments after the thread group has already been built.
- */
- getCommentsForThread(rootId: UrlEncodedCommentId) {
- const allThreads = this.getAllThreadsForChange();
- const threadMatch = allThreads.find(t => t.rootId === rootId);
-
- // In the event that a single draft comment was removed by the thread-list
- // and the diff view is updating comments, there will no longer be a thread
- // found. In this case, return null.
- return threadMatch ? threadMatch.comments : null;
- }
-
- /**
* Gets all the comments and robot comments for the given change.
*/
getAllComments(includeDrafts?: boolean, patchNum?: PatchSetNum) {
const paths = this.getPaths();
- const publishedComments: {[path: string]: CommentBasics[]} = {};
+ const publishedComments: {[path: string]: CommentInfo[]} = {};
for (const path of Object.keys(paths)) {
publishedComments[path] = this.getAllCommentsForPath(
path,
@@ -196,8 +173,8 @@ export class ChangeComments {
path: string,
patchNum?: PatchSetNum,
includeDrafts?: boolean
- ): Comment[] {
- const comments: Comment[] = this._comments[path] || [];
+ ): CommentInfo[] {
+ const comments: CommentInfo[] = this._comments[path] || [];
const robotComments = this._robotComments[path] || [];
let allComments = comments.concat(robotComments);
if (includeDrafts) {
@@ -233,43 +210,18 @@ export class ChangeComments {
return allComments;
}
- cloneWithUpdatedDrafts(drafts: {[path: string]: DraftInfo[]} | undefined) {
- return new ChangeComments(
- this._comments,
- this._robotComments,
- drafts,
- this._portedComments,
- this._portedDrafts
- );
- }
-
- cloneWithUpdatedPortedComments(
- portedComments?: PathToCommentsInfoMap,
- portedDrafts?: PathToCommentsInfoMap
- ) {
- return new ChangeComments(
- this._comments,
- this._robotComments,
- this._drafts,
- portedComments,
- portedDrafts
- );
- }
-
/**
* Get the drafts for a path and optional patch num.
*
* This will return a shallow copy of all drafts every time,
* so changes on any copy will not affect other copies.
*/
- getAllDraftsForPath(path: string, patchNum?: PatchSetNum): Comment[] {
- let comments = this._drafts[path] || [];
+ getAllDraftsForPath(path: string, patchNum?: PatchSetNum): DraftInfo[] {
+ let drafts = this._drafts[path] || [];
if (patchNum) {
- comments = comments.filter(c => c.patch_set === patchNum);
+ drafts = drafts.filter(c => c.patch_set === patchNum);
}
- return comments.map(c => {
- return {...c, __draft: true};
- });
+ return drafts;
}
/**
@@ -277,7 +229,7 @@ export class ChangeComments {
*
* // TODO(taoalpha): maybe merge in *ForPath
*/
- getAllDraftsForFile(file: PatchSetFile): Comment[] {
+ getAllDraftsForFile(file: PatchSetFile): CommentInfo[] {
let allDrafts = this.getAllDraftsForPath(file.path, file.patchNum);
if (file.basePath) {
allDrafts = allDrafts.concat(
@@ -297,8 +249,8 @@ export class ChangeComments {
* @param projectConfig Optional project config object to
* include in the meta sub-object.
*/
- getCommentsForPath(path: string, patchRange: PatchRange): Comment[] {
- let comments: Comment[] = [];
+ getCommentsForPath(path: string, patchRange: PatchRange): CommentInfo[] {
+ let comments: CommentInfo[] = [];
let drafts: DraftInfo[] = [];
let robotComments: RobotCommentInfo[] = [];
if (this._comments && this._comments[path]) {
@@ -311,17 +263,13 @@ export class ChangeComments {
robotComments = this._robotComments[path];
}
- drafts.forEach(d => {
- d.__draft = true;
- });
-
- return comments
- .concat(drafts)
- .concat(robotComments)
+ const all = comments.concat(drafts).concat(robotComments);
+ const final = all
.filter(c => isInPatchRange(c, patchRange))
.map(c => {
return {...c};
});
+ return final;
}
/**
@@ -372,7 +320,7 @@ export class ChangeComments {
// ported comments will involve comments that may not belong to the
// current patchrange, so we need to form threads for them using all
// comments
- const allComments: UIComment[] = this.getAllCommentsForFile(file, true);
+ const allComments: CommentInfo[] = this.getAllCommentsForFile(file, true);
return createCommentThreads(allComments).filter(thread => {
// Robot comments and drafts are not ported over. A human reply to
@@ -388,22 +336,9 @@ export class ChangeComments {
comment => comment.id === portedComment.id
)!;
- if (
- (originalComment.line && !portedComment.line) ||
- (originalComment.range && !portedComment.range)
- ) {
- thread.rangeInfoLost = true;
- }
+ // Original comment shown anyway? No need to port.
+ if (isInPatchRange(originalComment, patchRange)) return false;
- if (
- isInBaseOfPatchRange(thread.comments[0], patchRange) ||
- isInRevisionOfPatchRange(thread.comments[0], patchRange)
- ) {
- // no need to port this thread as it will be rendered by default
- return false;
- }
-
- thread.diffSide = Side.RIGHT;
if (thread.commentSide === CommentSide.PARENT) {
// TODO(dhruvsri): Add handling for merge parents
if (
@@ -411,11 +346,21 @@ export class ChangeComments {
!!thread.mergeParentNum
)
return false;
- thread.diffSide = Side.LEFT;
}
if (!isUnresolved(thread) && !isDraftThread(thread)) return false;
+ if (
+ (originalComment.line && !portedComment.line) ||
+ (originalComment.range && !portedComment.range)
+ ) {
+ thread.rangeInfoLost = true;
+ }
+ // TODO: It probably makes more sense to set the patch_set in
+ // portedComment either in the backend or in the RestApi layer. Then we
+ // could check `!isInPatchRange(portedComment, patchRange)` and then set
+ // thread.patchNum = portedComment.patch_set;
+ thread.patchNum = patchRange.patchNum;
thread.range = portedComment.range;
thread.line = portedComment.line;
thread.ported = true;
@@ -428,8 +373,7 @@ export class ChangeComments {
patchRange: PatchRange
): CommentThread[] {
const threads = createCommentThreads(
- this.getCommentsForFile(file, patchRange),
- patchRange
+ this.getCommentsForFile(file, patchRange)
);
threads.push(...this._getPortedCommentThreads(file, patchRange));
return threads;
@@ -447,7 +391,10 @@ export class ChangeComments {
* @param projectConfig Optional project config object to
* include in the meta sub-object.
*/
- getCommentsForFile(file: PatchSetFile, patchRange: PatchRange): Comment[] {
+ getCommentsForFile(
+ file: PatchSetFile,
+ patchRange: PatchRange
+ ): CommentInfo[] {
const comments = this.getCommentsForPath(file.path, patchRange);
if (file.basePath) {
comments.push(...this.getCommentsForPath(file.basePath, patchRange));
@@ -469,11 +416,11 @@ export class ChangeComments {
file: PatchSetFile | PatchNumOnly,
ignorePatchsetLevelComments?: boolean
) {
- let comments: Comment[] = [];
+ let comments: CommentInfo[] = [];
if (isPatchSetFile(file)) {
comments = this.getAllCommentsForFile(file);
} else {
- comments = this._commentObjToArray(
+ comments = this._commentObjToArray<CommentInfo>(
this.getAllPublishedComments(file.patchNum)
);
}
@@ -584,8 +531,8 @@ export class ChangeComments {
file: PatchSetFile | PatchNumOnly,
ignorePatchsetLevelComments?: boolean
) {
- let comments: Comment[] = [];
- let drafts: Comment[] = [];
+ let comments: CommentInfo[] = [];
+ let drafts: CommentInfo[] = [];
if (isPatchSetFile(file)) {
comments = this.getAllCommentsForFile(file);
@@ -611,38 +558,3 @@ export class ChangeComments {
return createCommentThreads(comments);
}
}
-
-@customElement('gr-comment-api')
-export class GrCommentApi extends PolymerElement {
- static get template() {
- return htmlTemplate;
- }
-
- @property({type: Object})
- _changeComments?: ChangeComments;
-
- private readonly restApiService = appContext.restApiService;
-
- private readonly commentsService = appContext.commentsService;
-
- reloadPortedComments(changeNum: NumericChangeId, patchNum: PatchSetNum) {
- if (!this._changeComments) {
- this.commentsService.loadAll(changeNum);
- return Promise.resolve();
- }
- return Promise.all([
- this.restApiService.getPortedComments(changeNum, patchNum),
- this.restApiService.getPortedDrafts(changeNum, patchNum),
- ]).then(res => {
- if (!this._changeComments) return;
- this._changeComments =
- this._changeComments.cloneWithUpdatedPortedComments(res[0], res[1]);
- });
- }
-}
-
-declare global {
- interface HTMLElementTagNameMap {
- 'gr-comment-api': GrCommentApi;
- }
-}