From 22a86edc7f2376e3632588ab08ee4b6553ca3529 Mon Sep 17 00:00:00 2001 From: Dhruv Srivastava Date: Tue, 17 Dec 2019 16:46:18 +0100 Subject: [PATCH] Show revert change and revert submission options instead of 2 buttons Change-Id: I5b7d6b6b5335637ffeff599ec62a16792fe46604 --- .../gr-change-actions/gr-change-actions.html | 3 + .../gr-change-actions/gr-change-actions.js | 49 ++--- .../gr-change-actions_test.html | 172 ++++++++++++++---- .../gr-confirm-revert-dialog.html | 49 ++++- .../gr-confirm-revert-dialog.js | 117 +++++++++++- .../gr-confirm-revert-dialog_test.html | 11 +- .../gr-confirm-revert-submission-dialog.js | 6 +- ...confirm-revert-submission-dialog_test.html | 10 +- 8 files changed, 341 insertions(+), 76 deletions(-) diff --git a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.html b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.html index f4bd6a6ee5..283bd743c5 100644 --- a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.html +++ b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.html @@ -205,6 +205,9 @@ limitations under the License. { + this._revertChanges = changes; + this._showActionDialog(this.$.confirmRevertDialog); + }); } showRevertSubmissionDialog() { @@ -932,7 +935,7 @@ this.$.restAPI.getChanges('', query) .then(changes => { this.$.confirmRevertSubmissionDialog. - populateRevertSubmissionMessage( + _populateRevertSubmissionMessage( this.commitMessage, this.change, changes); this._showActionDialog(this.$.confirmRevertSubmissionDialog); }); @@ -1143,20 +1146,24 @@ ); } - _handleRevertDialogConfirm() { + _handleRevertDialogConfirm(e) { + const revertType = e.detail.revertType; + const message = e.detail.message; const el = this.$.confirmRevertDialog; this.$.overlay.close(); el.hidden = true; - this._fireAction('/revert', this.actions.revert, false, - {message: el.message}); - } - - _handleRevertSubmissionDialogConfirm() { - const el = this.$.confirmRevertSubmissionDialog; - this.$.overlay.close(); - el.hidden = true; - this._fireAction('/revert_submission', this.actions.revert_submission, - false, {message: el.message}); + switch (revertType) { + case REVERT_TYPES.REVERT_SINGLE_CHANGE: + this._fireAction('/revert', this.actions.revert, false, + {message}); + break; + case REVERT_TYPES.REVERT_SUBMISSION: + this._fireAction('/revert_submission', this.actions.revert_submission, + false, {message}); + break; + default: + console.error('invalid revert type'); + } } _handleAbandonDialogConfirm() { diff --git a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions_test.html b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions_test.html index ec5c5a5485..e4ea218390 100644 --- a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions_test.html +++ b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions_test.html @@ -37,6 +37,7 @@ limitations under the License. diff --git a/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog.js b/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog.js index bf727ec861..438440e05f 100644 --- a/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog.js +++ b/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog.js @@ -19,6 +19,13 @@ const ERR_COMMIT_NOT_FOUND = 'Unable to find the commit hash of this change.'; + const CHANGE_SUBJECT_LIMIT = 50; + + // TODO(dhruvsri): clean up repeated definitions after moving to js modules + const REVERT_TYPES = { + REVERT_SINGLE_CHANGE: 1, + REVERT_SUBMISSION: 2, + }; /** * @appliesMixin Gerrit.FireMixin @@ -45,12 +52,55 @@ static get properties() { return { message: String, + _revertType: { + type: Number, + value: REVERT_TYPES.REVERT_SINGLE_CHANGE, + }, + _showRevertSubmission: { + type: Boolean, + value: false, + }, + changes: { + type: Array, + value() { return []; }, + }, + change: Object, + commitMessage: String, }; } - populateRevertMessage(message, commitHash) { + static get observers() { + return [ + 'onInputUpdate(change, commitMessage, changes)', + ]; + } + + _computeIfSingleRevert(revertType) { + return revertType === REVERT_TYPES.REVERT_SINGLE_CHANGE; + } + + _computeIfRevertSubmission(revertType) { + return revertType === REVERT_TYPES.REVERT_SUBMISSION; + } + + _modifyRevertMsg(change, commitMessage, message) { + return this.$.jsAPI.modifyRevertMsg(change, + message, commitMessage); + } + + onInputUpdate(change, commitMessage, changes) { + if (!change || !changes) return; + this._populateRevertSingleChangeMessage( + change, commitMessage, change.current_revision); + if (changes.length > 1) { + this._populateRevertSubmissionMessage( + change, changes); + } + } + + _populateRevertSingleChangeMessage(change, commitMessage, commitHash) { // Figure out what the revert title should be. - const originalTitle = message.split('\n')[0]; + const originalTitle = (commitMessage || '').split('\n')[0]; const revertTitle = `Revert "${originalTitle}"`; if (!commitHash) { this.fire('show-alert', {message: ERR_COMMIT_NOT_FOUND}); @@ -58,20 +108,77 @@ } const revertCommitText = `This reverts commit ${commitHash}.`; - this.message = `${revertTitle}\n\n${revertCommitText}\n\n` + + this.revertSingleChangeMessage = + `${revertTitle}\n\n${revertCommitText}\n\n` + `Reason for revert: \n`; + // This is to give plugins a chance to update message + this.revertSingleChangeMessage = + this._modifyRevertMsg(change, commitMessage, + this.revertSingleChangeMessage); + this.message = this.revertSingleChangeMessage; + } + + _getTrimmedChangeSubject(subject) { + if (!subject) return ''; + if (subject.length < CHANGE_SUBJECT_LIMIT) return subject; + return subject.substring(0, CHANGE_SUBJECT_LIMIT) + '...'; + } + + _modifyRevertSubmissionMsg(change) { + return this.$.jsAPI.modifyRevertSubmissionMsg(change, + this.revertSubmissionMessage, this.commitMessage); + } + + _populateRevertSubmissionMessage(change, changes) { + // Follow the same convention of the revert + const commitHash = change.current_revision; + if (!commitHash) { + this.fire('show-alert', {message: ERR_COMMIT_NOT_FOUND}); + return; + } + if (!changes || changes.length <= 1) return; + const submissionId = change.submission_id; + const revertTitle = 'Revert submission ' + submissionId; + this.changes = changes; + this.revertSubmissionMessage = revertTitle + '\n\n' + + 'Reason for revert: \n'; + this.revertSubmissionMessage += 'Reverted Changes:\n'; + changes.forEach(change => { + this.revertSubmissionMessage += change.change_id.substring(0, 10) + ':' + + this._getTrimmedChangeSubject(change.subject) + '\n'; + }); + this.revertSubmissionMessage = this._modifyRevertSubmissionMsg(change); + this.message = this.revertSubmissionMessage; + this._revertType = REVERT_TYPES.REVERT_SUBMISSION; + this._showRevertSubmission = true; + } + + _handleRevertSingleChangeClicked() { + if (this._revertType === REVERT_TYPES.REVERT_SINGLE_CHANGE) return; + this.revertSubmissionMessage = this.message; + this.message = this.revertSingleChangeMessage; + this._revertType = REVERT_TYPES.REVERT_SINGLE_CHANGE; + } + + _handleRevertSubmissionClicked() { + if (this._revertType === REVERT_TYPES.REVERT_SUBMISSION) return; + this._revertType = REVERT_TYPES.REVERT_SUBMISSION; + this.revertSingleChangeMessage = this.message; + this.message = this.revertSubmissionMessage; } _handleConfirmTap(e) { e.preventDefault(); e.stopPropagation(); - this.fire('confirm', null, {bubbles: false}); + this.fire('confirm', {revertType: this._revertType, + message: this.message}, {bubbles: false}); } _handleCancelTap(e) { e.preventDefault(); e.stopPropagation(); - this.fire('cancel', null, {bubbles: false}); + this.fire('cancel', {revertType: this._revertType}, + {bubbles: false}); } } diff --git a/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog_test.html b/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog_test.html index dbdfba229a..1d28d3226f 100644 --- a/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog_test.html +++ b/polygerrit-ui/app/elements/change/gr-confirm-revert-dialog/gr-confirm-revert-dialog_test.html @@ -50,13 +50,14 @@ limitations under the License. assert.isNotOk(element.message); const alertStub = sandbox.stub(); element.addEventListener('show-alert', alertStub); - element.populateRevertMessage('not a commitHash in sight', undefined); + element._populateRevertSingleChangeMessage({}, + 'not a commitHash in sight', undefined); assert.isTrue(alertStub.calledOnce); }); test('single line', () => { assert.isNotOk(element.message); - element.populateRevertMessage( + element._populateRevertSingleChangeMessage({}, 'one line commit\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert "one line commit"\n\n' + @@ -67,7 +68,7 @@ limitations under the License. test('multi line', () => { assert.isNotOk(element.message); - element.populateRevertMessage( + element._populateRevertSingleChangeMessage({}, 'many lines\ncommit\n\nmessage\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert "many lines"\n\n' + @@ -78,7 +79,7 @@ limitations under the License. test('issue above change id', () => { assert.isNotOk(element.message); - element.populateRevertMessage( + element._populateRevertSingleChangeMessage({}, 'much lines\nvery\n\ncommit\n\nBug: Issue 42\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert "much lines"\n\n' + @@ -89,7 +90,7 @@ limitations under the License. test('revert a revert', () => { assert.isNotOk(element.message); - element.populateRevertMessage( + element._populateRevertSingleChangeMessage({}, 'Revert "one line commit"\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert "Revert "one line commit""\n\n' + diff --git a/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog.js b/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog.js index d59f5cd1b4..ae8dfa53c1 100644 --- a/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog.js +++ b/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog.js @@ -50,7 +50,7 @@ }; } - getTrimmedChangeSubject(subject) { + _getTrimmedChangeSubject(subject) { if (!subject) return ''; if (subject.length < CHANGE_SUBJECT_LIMIT) return subject; return subject.substring(0, CHANGE_SUBJECT_LIMIT) + '...'; @@ -61,7 +61,7 @@ this.message, this.commitMessage); } - populateRevertSubmissionMessage(message, change, changes) { + _populateRevertSubmissionMessage(message, change, changes) { // Follow the same convention of the revert const commitHash = change.current_revision; if (!commitHash) { @@ -77,7 +77,7 @@ changes = changes || []; changes.forEach(change => { this.message += change.change_id.substring(0, 10) + ': ' + - this.getTrimmedChangeSubject(change.subject) + '\n'; + this._getTrimmedChangeSubject(change.subject) + '\n'; }); this.message = this._modifyRevertSubmissionMsg(change); } diff --git a/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog_test.html b/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog_test.html index cc4bd548fa..af99c7e6ee 100644 --- a/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog_test.html +++ b/polygerrit-ui/app/elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog_test.html @@ -51,7 +51,7 @@ limitations under the License. assert.isNotOk(element.message); const alertStub = sandbox.stub(); element.addEventListener('show-alert', alertStub); - element.populateRevertSubmissionMessage( + element._populateRevertSubmissionMessage( 'not a commitHash in sight' ); assert.isTrue(alertStub.calledOnce); @@ -59,7 +59,7 @@ limitations under the License. test('single line', () => { assert.isNotOk(element.message); - element.populateRevertSubmissionMessage( + element._populateRevertSubmissionMessage( 'one line commit\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert submission\n\n' + @@ -69,7 +69,7 @@ limitations under the License. test('multi line', () => { assert.isNotOk(element.message); - element.populateRevertSubmissionMessage( + element._populateRevertSubmissionMessage( 'many lines\ncommit\n\nmessage\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert submission\n\n' + @@ -79,7 +79,7 @@ limitations under the License. test('issue above change id', () => { assert.isNotOk(element.message); - element.populateRevertSubmissionMessage( + element._populateRevertSubmissionMessage( 'test \nvery\n\ncommit\n\nBug: Issue 42\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert submission\n\n' + @@ -89,7 +89,7 @@ limitations under the License. test('revert a revert', () => { assert.isNotOk(element.message); - element.populateRevertSubmissionMessage( + element._populateRevertSubmissionMessage( 'Revert "one line commit"\n\nChange-Id: abcdefg\n', 'abcd123'); const expected = 'Revert submission\n\n' +