Support creating comments on merge parents

Bug: Issue 4760
Change-Id: I66ced578819b6f48d7d1535a54f1debf0e35374e
This commit is contained in:
Wyatt Allen
2017-11-17 10:57:46 -08:00
parent a744e2303f
commit c56ece73e2
8 changed files with 157 additions and 13 deletions

View File

@@ -95,6 +95,7 @@ limitations under the License.
baseImage: Object, baseImage: Object,
revisionImage: Object, revisionImage: Object,
projectName: String, projectName: String,
parentIndex: Number,
_builder: Object, _builder: Object,
_groups: Array, _groups: Array,
_layers: Array, _layers: Array,
@@ -251,18 +252,25 @@ limitations under the License.
}, },
_getDiffBuilder(diff, comments, prefs) { _getDiffBuilder(diff, comments, prefs) {
let builder = null;
if (this.isImageDiff) { if (this.isImageDiff) {
return new GrDiffBuilderImage(diff, comments, prefs, builder = new GrDiffBuilderImage(diff, comments, prefs,
this.projectName, this.diffElement, this.baseImage, this.projectName, this.diffElement, this.baseImage,
this.revisionImage); this.revisionImage);
} else if (this.viewMode === DiffViewMode.SIDE_BY_SIDE) { } else if (this.viewMode === DiffViewMode.SIDE_BY_SIDE) {
return new GrDiffBuilderSideBySide(diff, comments, prefs, builder = new GrDiffBuilderSideBySide(diff, comments, prefs,
this.projectName, this.diffElement, this._layers); this.projectName, this.diffElement, this._layers);
} else if (this.viewMode === DiffViewMode.UNIFIED) { } else if (this.viewMode === DiffViewMode.UNIFIED) {
return new GrDiffBuilderUnified(diff, comments, prefs, builder = new GrDiffBuilderUnified(diff, comments, prefs,
this.projectName, this.diffElement, this._layers); this.projectName, this.diffElement, this._layers);
} }
throw Error('Unsupported diff view mode: ' + this.viewMode); if (!builder) {
throw Error('Unsupported diff view mode: ' + this.viewMode);
}
if (this.parentIndex) {
builder.setParentIndex(this.parentIndex);
}
return builder;
}, },
_clearDiffContent() { _clearDiffContent() {

View File

@@ -47,6 +47,7 @@
this._outputEl = outputEl; this._outputEl = outputEl;
this.groups = []; this.groups = [];
this._blameInfo = null; this._blameInfo = null;
this._parentIndex = undefined;
this.layers = layers || []; this.layers = layers || [];
@@ -353,6 +354,7 @@
threadGroupEl.isOnParent = isOnParent; threadGroupEl.isOnParent = isOnParent;
threadGroupEl.projectName = this._projectName; threadGroupEl.projectName = this._projectName;
threadGroupEl.range = range; threadGroupEl.range = range;
threadGroupEl.parentIndex = this._parentIndex;
return threadGroupEl; return threadGroupEl;
}; };
@@ -368,7 +370,9 @@
let isOnParent = comments[0].side === 'PARENT' || false; let isOnParent = comments[0].side === 'PARENT' || false;
if (line.type === GrDiffLine.Type.REMOVE || if (line.type === GrDiffLine.Type.REMOVE ||
opt_side === GrDiffBuilder.Side.LEFT) { opt_side === GrDiffBuilder.Side.LEFT) {
if (this._comments.meta.patchRange.basePatchNum === 'PARENT') { if (this._comments.meta.patchRange.basePatchNum === 'PARENT' ||
Gerrit.PatchSetBehavior.isMergeParent(
this._comments.meta.patchRange.basePatchNum)) {
isOnParent = true; isOnParent = true;
} else { } else {
patchNum = this._comments.meta.patchRange.basePatchNum; patchNum = this._comments.meta.patchRange.basePatchNum;
@@ -586,6 +590,10 @@
} }
}; };
GrDiffBuilder.prototype.setParentIndex = function(index) {
this._parentIndex = index;
};
/** /**
* Find the blame cell for a given line number. * Find the blame cell for a given line number.
* @param {number} lineNum * @param {number} lineNum

View File

@@ -34,6 +34,7 @@ limitations under the License.
comments="[[thread.comments]]" comments="[[thread.comments]]"
comment-side="[[thread.commentSide]]" comment-side="[[thread.commentSide]]"
is-on-parent="[[isOnParent]]" is-on-parent="[[isOnParent]]"
parent-index="[[parentIndex]]"
change-num="[[changeNum]]" change-num="[[changeNum]]"
location-range="[[thread.locationRange]]" location-range="[[thread.locationRange]]"
patch-num="[[thread.patchNum]]" patch-num="[[thread.patchNum]]"

View File

@@ -30,6 +30,10 @@
type: Boolean, type: Boolean,
value: false, value: false,
}, },
parentIndex: {
type: Number,
value: null,
},
_threads: { _threads: {
type: Array, type: Array,
value() { return []; }, value() { return []; },

View File

@@ -48,7 +48,10 @@
type: Boolean, type: Boolean,
value: false, value: false,
}, },
parentIndex: {
type: Number,
value: null,
},
_showActions: Boolean, _showActions: Boolean,
_lastComment: Object, _lastComment: Object,
_orderedComments: Array, _orderedComments: Array,
@@ -301,6 +304,9 @@
end_character: opt_range.endChar, end_character: opt_range.endChar,
}; };
} }
if (this.parentIndex) {
d.parent = this.parentIndex;
}
return d; return d;
}, },

View File

@@ -270,7 +270,8 @@ limitations under the License.
line-wrapping="[[lineWrapping]]" line-wrapping="[[lineWrapping]]"
is-image-diff="[[isImageDiff]]" is-image-diff="[[isImageDiff]]"
base-image="[[_baseImage]]" base-image="[[_baseImage]]"
revision-image="[[_revisionImage]]"> revision-image="[[_revisionImage]]"
parent-index="[[_parentIndex]]">
<table <table
id="diffTable" id="diffTable"
class$="[[_diffTableClass]]" class$="[[_diffTableClass]]"

View File

@@ -137,6 +137,11 @@
notify: true, notify: true,
computed: '_computeIsBlameLoaded(_blame)', computed: '_computeIsBlameLoaded(_blame)',
}, },
_parentIndex: {
type: Number,
computed: '_computeParentIndex(patchRange.*)',
},
}, },
behaviors: [ behaviors: [
@@ -415,12 +420,27 @@
return threadEl; return threadEl;
}, },
/** @return {number} */ /**
* The value to be used for the patch number of new comments created at the
* given line and content elements.
*
* In two cases of creating a comment on the left side, the patch number to
* be used should actually be right side of the patch range:
* - When the patch range is against the parent comment of a normal change.
* Such comments declare themmselves to be on the left using side=PARENT.
* - If the patch range is against the indexed parent of a merge change.
* Such comments declare themselves to be on the given parent by
* specifying the parent index via parent=i.
*
* @return {number}
*/
_getPatchNumByLineAndContent(lineEl, contentEl) { _getPatchNumByLineAndContent(lineEl, contentEl) {
let patchNum = this.patchRange.patchNum; let patchNum = this.patchRange.patchNum;
if ((lineEl.classList.contains(DiffSide.LEFT) || if ((lineEl.classList.contains(DiffSide.LEFT) ||
contentEl.classList.contains('remove')) && contentEl.classList.contains('remove')) &&
this.patchRange.basePatchNum !== 'PARENT') { this.patchRange.basePatchNum !== 'PARENT' &&
!this.isMergeParent(this.patchRange.basePatchNum)) {
patchNum = this.patchRange.basePatchNum; patchNum = this.patchRange.basePatchNum;
} }
return patchNum; return patchNum;
@@ -428,13 +448,13 @@
/** @return {boolean} */ /** @return {boolean} */
_getIsParentCommentByLineAndContent(lineEl, contentEl) { _getIsParentCommentByLineAndContent(lineEl, contentEl) {
let isOnParent = false;
if ((lineEl.classList.contains(DiffSide.LEFT) || if ((lineEl.classList.contains(DiffSide.LEFT) ||
contentEl.classList.contains('remove')) && contentEl.classList.contains('remove')) &&
this.patchRange.basePatchNum === 'PARENT') { (this.patchRange.basePatchNum === 'PARENT' ||
isOnParent = true; this.isMergeParent(this.patchRange.basePatchNum))) {
return true;
} }
return isOnParent; return false;
}, },
/** @return {string} */ /** @return {string} */
@@ -717,5 +737,15 @@
_computeWarningClass(showWarning) { _computeWarningClass(showWarning) {
return showWarning ? 'warn' : ''; return showWarning ? 'warn' : '';
}, },
/**
* @return {number|null}
*/
_computeParentIndex(patchRangeRecord) {
if (!this.isMergeParent(patchRangeRecord.base.basePatchNum)) {
return null;
}
return this.getParentIndex(patchRangeRecord.base.basePatchNum);
},
}); });
})(); })();

View File

@@ -64,6 +64,92 @@ limitations under the License.
assert.equal(element._diffLength(mock.diffResponse), 52); assert.equal(element._diffLength(mock.diffResponse), 52);
}); });
suite('_get{PatchNum|IsParentComment}ByLineAndContent', () => {
let lineEl;
let contentEl;
setup(() => {
element = fixture('basic');
lineEl = document.createElement('td');
contentEl = document.createElement('span');
});
suite('_getPatchNumByLineAndContent', () => {
test('right side', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
lineEl.classList.add('right');
assert.equal(element._getPatchNumByLineAndContent(lineEl, contentEl),
4);
});
test('left side parent by linenum', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
lineEl.classList.add('left');
assert.equal(element._getPatchNumByLineAndContent(lineEl, contentEl),
4);
});
test('left side parent by content', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
contentEl.classList.add('remove');
assert.equal(element._getPatchNumByLineAndContent(lineEl, contentEl),
4);
});
test('left side merge parent', () => {
element.patchRange = {patchNum: 4, basePatchNum: -2};
contentEl.classList.add('remove');
assert.equal(element._getPatchNumByLineAndContent(lineEl, contentEl),
4);
});
test('left side non parent', () => {
element.patchRange = {patchNum: 4, basePatchNum: 3};
contentEl.classList.add('remove');
assert.equal(element._getPatchNumByLineAndContent(lineEl, contentEl),
3);
});
});
suite('_getIsParentCommentByLineAndContent', () => {
test('right side', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
lineEl.classList.add('right');
assert.isFalse(
element._getIsParentCommentByLineAndContent(lineEl, contentEl));
});
test('left side parent by linenum', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
lineEl.classList.add('left');
assert.isTrue(
element._getIsParentCommentByLineAndContent(lineEl, contentEl));
});
test('left side parent by content', () => {
element.patchRange = {patchNum: 4, basePatchNum: 'PARENT'};
contentEl.classList.add('remove');
assert.isTrue(
element._getIsParentCommentByLineAndContent(lineEl, contentEl));
});
test('left side merge parent', () => {
element.patchRange = {patchNum: 4, basePatchNum: -2};
contentEl.classList.add('remove');
assert.isTrue(
element._getIsParentCommentByLineAndContent(lineEl, contentEl));
});
test('left side non parent', () => {
element.patchRange = {patchNum: 4, basePatchNum: 3};
contentEl.classList.add('remove');
assert.isFalse(
element._getIsParentCommentByLineAndContent(lineEl, contentEl));
});
});
});
suite('not logged in', () => { suite('not logged in', () => {
setup(() => { setup(() => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {