Replace comment route in url bar with meaningful url
Comment route is of type /comment/<commentId>. This url displays the
commentId in the url bar which is not significant to the user and
hides the file and line number information.
Hence, replace the url with normal diff url.
Change-Id: Id135b77caf3f69edd6656768b32bb10a1df89e6d
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
index 9d24b3e..cbee7c5 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
@@ -941,6 +941,23 @@
this._commitRange = commit && baseCommit ? {commit, baseCommit} : undefined;
}
+ _updateUrlToDiffUrl(lineNum?: number, leftSide?: boolean) {
+ if (!this._change) return;
+ if (!this._patchRange) return;
+ if (!this._changeNum) return;
+ if (!this._path) return;
+ const url = GerritNav.getUrlForDiffById(
+ this._changeNum,
+ this._change.project,
+ this._path,
+ this._patchRange.patchNum,
+ this._patchRange.basePatchNum,
+ lineNum,
+ leftSide
+ );
+ history.replaceState(null, '', url);
+ }
+
_initPatchRange() {
let leftSide = false;
if (!this._change) return;
@@ -991,6 +1008,12 @@
}
if (!this._patchRange) throw new Error('Failed to initialize patchRange.');
this._initLineOfInterestAndCursor(leftSide);
+
+ if (this.params?.commentId) {
+ // url is of type /comment/{commentId} which isn't meaningful
+ this._updateUrlToDiffUrl(this._focusLineNum, leftSide);
+ }
+
this._commentMap = this._getPaths(this._patchRange);
this._commentsForDiff = this._getCommentsForPath(
@@ -1437,26 +1460,12 @@
_: Event,
detail: {side: Side | CommentSide; number: number}
) {
- if (!this._change) return;
- if (!this._path) return;
- if (!this._changeNum) return;
- if (!this._patchRange) return;
-
- const number = detail.number;
// for on-comment-anchor-tap side can be PARENT/REVISIONS
// for on-line-selected side can be left/right
- const leftSide =
- detail.side === Side.LEFT || detail.side === CommentSide.PARENT;
- const url = GerritNav.getUrlForDiffById(
- this._changeNum,
- this._change.project,
- this._path,
- this._patchRange.patchNum,
- this._patchRange.basePatchNum,
- number,
- leftSide
+ this._updateUrlToDiffUrl(
+ detail.number,
+ detail.side === Side.LEFT || detail.side === CommentSide.PARENT
);
- history.replaceState(null, '', url);
}
_computeDownloadDropdownLinks(
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
index 9a08723..7ef75ed 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
@@ -184,6 +184,8 @@
test('comment route', () => {
const initLineOfInterestAndCursorStub =
sinon.stub(element, '_initLineOfInterestAndCursor');
+ const getUrlStub = sinon.stub(GerritNav, 'getUrlForDiffById');
+ const replaceStateStub = sinon.stub(history, 'replaceState');
sinon.stub(element, '_getFiles');
sinon.stub(element.reporting, 'diffViewDisplayed');
sinon.stub(element.$.diffHost, 'reload').returns(Promise.resolve());
@@ -213,6 +215,10 @@
assert.equal(element._focusLineNum, 10);
assert.equal(element._patchRange.patchNum, 11);
assert.equal(element._patchRange.basePatchNum, 2);
+
+ assert.isTrue(replaceStateStub.called);
+ assert.isTrue(getUrlStub.calledWithExactly('42', 'test-project',
+ '/COMMIT_MSG', 11, 2, 10, true));
});
});