From b6db19e072d902b3f9741c4a0cbe6897100c3e69 Mon Sep 17 00:00:00 2001 From: Viktar Donich Date: Tue, 3 Apr 2018 15:32:54 -0700 Subject: [PATCH] Plugin API for label score updates Plugin may use following API to receive saved label updates: ``` js plugin.changeMetadata().onLabelsChanged( labels => console.log(labels)); ``` Feature: Issue 820117 Change-Id: Id428606db25086cfdf5a5b420e34777cc8b27303 --- .../pg-plugin-change-metadata-api.txt | 16 +++++ Documentation/pg-plugin-dev.txt | 9 +++ Documentation/pg-plugin-endpoints.txt | 7 +- .../gr-change-metadata-it_test.html | 69 +++++++++++++++++-- .../gr-change-metadata.html | 5 +- .../gr-change-metadata/gr-change-metadata.js | 30 ++++++-- .../gr-change-metadata_test.html | 36 ++++------ .../gr-change-metadata-api.html | 22 ++++++ .../gr-change-metadata-api.js | 39 +++++++++++ .../gr-js-api-interface.html | 1 + .../gr-js-api-interface/gr-public-js-api.js | 4 ++ 11 files changed, 201 insertions(+), 37 deletions(-) create mode 100644 Documentation/pg-plugin-change-metadata-api.txt create mode 100644 polygerrit-ui/app/elements/plugins/gr-change-metadata-api/gr-change-metadata-api.html create mode 100644 polygerrit-ui/app/elements/plugins/gr-change-metadata-api/gr-change-metadata-api.js diff --git a/Documentation/pg-plugin-change-metadata-api.txt b/Documentation/pg-plugin-change-metadata-api.txt new file mode 100644 index 0000000000..8348da80a1 --- /dev/null +++ b/Documentation/pg-plugin-change-metadata-api.txt @@ -0,0 +1,16 @@ += Gerrit Code Review - Change metadata plugin API + +This API is provided by +link:pg-plugin-dev.html#change-metadata[plugin.changeMetadata()] and provides +interface for customization and data updates of change metadata. + +== onLabelsChanged +`changeMetadataApi.onLabelsChanged(callback)` + +.Params +- *callback* function that's executed when labels changed on the server. +Callback receives labels with scores applied to the change, map of the label +names to link:rest-api-changes.html#label-info[LabelInfo] entries + +.Returns +- `GrChangeMetadataApi` for chaining. diff --git a/Documentation/pg-plugin-dev.txt b/Documentation/pg-plugin-dev.txt index 580166a6e7..8871424002 100644 --- a/Documentation/pg-plugin-dev.txt +++ b/Documentation/pg-plugin-dev.txt @@ -341,6 +341,15 @@ screen. Deprecated. Use link:#plugin-settings[`plugin.settings()`] instead. +=== changeMetadata +`plugin.changeMetadata()` + +.Params: +- none + +.Returns: +- Instance of link:pg-plugin-change-metadata-api.html[GrChangeMetadataApi]. + === theme `plugin.theme()` diff --git a/Documentation/pg-plugin-endpoints.txt b/Documentation/pg-plugin-endpoints.txt index d3d0a8d9c9..b77a66b80c 100644 --- a/Documentation/pg-plugin-endpoints.txt +++ b/Documentation/pg-plugin-endpoints.txt @@ -69,6 +69,11 @@ link:rest-api-changes.html#change-info[ChangeInfo] current revision displayed, an instance of link:rest-api-changes.html#revision-info[RevisionInfo] +* `labels` ++ +labels with scores applied to the change, map of the label names to +link:rest-api-changes.html#label-info[LabelInfo] entries + === robot-comment-controls The `robot-comment-controls` extension point is located inside each comment rendered on the diff page, and is only visible when the comment is a robot @@ -118,4 +123,4 @@ This endpoint wraps the textarea in the reply dialog. This endpoint decorator wraps the voting buttons in the reply dialog. === header-title -This endpoint wraps the title-text in the application header. \ No newline at end of file +This endpoint wraps the title-text in the application header. diff --git a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html index 9d51339a95..09dcd84fec 100644 --- a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html +++ b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata-it_test.html @@ -29,7 +29,7 @@ limitations under the License. @@ -51,20 +51,42 @@ limitations under the License. 'section.topic', ]; + const labels = { + CI: { + all: [ + {value: 1, name: 'user 2', _account_id: 1}, + {value: 2, name: 'user '}, + ], + values: { + ' 0': 'Don\'t submit as-is', + '+1': 'No score', + '+2': 'Looks good to me', + }, + }, + }; + const getStyle = function(selector, name) { return window.getComputedStyle( Polymer.dom(element.root).querySelector(selector))[name]; }; + function createElement() { + const element = fixture('element'); + element.change = {labels, status: 'NEW'}; + element.revision = {}; + return element; + } + setup(() => { sandbox = sinon.sandbox.create(); + stub('gr-rest-api-interface', { + getConfig() { return Promise.resolve({}); }, + getLoggedIn() { return Promise.resolve(false); }, + deleteVote() { return Promise.resolve({ok: true}); }, + }); stub('gr-change-metadata', { _computeShowLabelStatus() { return true; }, _computeShowReviewersByState() { return true; }, - ready() { - this.change = {labels: [], status: 'NEW'}; - this.serverConfig = {}; - }, }); }); @@ -76,7 +98,7 @@ limitations under the License. suite('by default', () => { setup(done => { - element = fixture('element'); + element = createElement(); flush(done); }); @@ -99,7 +121,7 @@ limitations under the License. ], }, }; - element = fixture('element'); + element = createElement(); const importSpy = sandbox.spy(element.$.externalStyle, '_import'); Gerrit.awaitPluginsLoaded().then(() => { Promise.all(importSpy.returnValues).then(() => { @@ -114,5 +136,38 @@ limitations under the License. }); } }); + + suite('label updates', () => { + let plugin; + let labelChangeStub; + + setup(done => { + Gerrit.install(p => plugin = p, '0.1', + new URL('test/plugin.html?' + Math.random(), + window.location.href).toString()); + sandbox.stub(Gerrit, '_arePluginsLoaded').returns(true); + Gerrit._resolveAllPluginsLoaded(); + element = createElement(); + sandbox.stub(element, '_computeCanDeleteVote').returns(true); + + labelChangeStub = sandbox.stub(); + plugin.changeMetadata().onLabelsChanged(labelChangeStub); + flush(done); + }); + + test('labels changed callback', done => { + assert.equal(labelChangeStub.callCount, 1); + assert.isTrue(labelChangeStub.calledWithExactly(labels)); + assert.equal(labelChangeStub.args[0][0]['CI'].all.length, 2); + MockInteractions.tap(Polymer.dom(element.root).querySelector( + 'gr-account-chip').$.remove); + // Wait for fake rest API response. + flush(() => { + assert.equal(labelChangeStub.callCount, 2); + assert.equal(labelChangeStub.args[1][0]['CI'].all.length, 1); + done(); + }); + }); + }); }); diff --git a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata.html b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata.html index eae9d2e4d4..056feea349 100644 --- a/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata.html +++ b/polygerrit-ui/app/elements/change/gr-change-metadata/gr-change-metadata.html @@ -316,12 +316,12 @@ limitations under the License.