Make context.call() call plugin actions correctly

In PolyGerrit, context.call() should will now use same context URL as in
GWT UI.

For change context:
/changes/project~changeid/pluginname~action

For revision context:
/changes/project~changeid/revisions/revisionid/pluginname~action

Feature: Issue 7685
Change-Id: I6db97ecdec9ab1f923148e2b65c8c347201f7ee3
This commit is contained in:
Viktar Donich
2017-11-07 12:16:46 -08:00
parent 4092a164a3
commit 7374aa9abf
4 changed files with 43 additions and 6 deletions

View File

@@ -640,6 +640,9 @@
// Plugin actions always contain ~ in the key.
if (a.indexOf('~') !== -1) {
pluginActions.push(actions[a]);
const patchNum = type === ActionType.REVISION ? this.patchNum : null;
this.$.restAPI.getChangeActionURL(this.changeNum, patchNum, '/' + a)
.then(url => actions[a].__url = url);
// Add server-side provided plugin actions to overflow menu.
this._overflowActions.push({
type,

View File

@@ -114,11 +114,32 @@ limitations under the License.
assert.isFalse(element._shouldHideActions({base: ['test']}, false));
});
test('plugin actions', () => {
test('plugin revision actions', () => {
sandbox.stub(element.$.restAPI, 'getChangeActionURL').returns(
Promise.resolve('the-url'));
element.revisionActions = {
'plugin~action': {},
};
assert.isOk(element.revisionActions['plugin~action']);
flush(() => {
assert.isTrue(element.$.restAPI.getChangeActionURL.calledWith(
element.changeNum, element.patchNum, '/plugin~action'));
assert.equal(element.revisionActions['plugin~action'].__url, 'the-url');
});
});
test('plugin change actions', () => {
sandbox.stub(element.$.restAPI, 'getChangeActionURL').returns(
Promise.resolve('the-url'));
element.actions = {
'plugin~action': {},
};
assert.isOk(element.actions['plugin~action']);
flush(() => {
assert.isTrue(element.$.restAPI.getChangeActionURL.calledWith(
element.changeNum, null, '/plugin~action'));
assert.equal(element.revisionActions['plugin~action'].__url, 'the-url');
});
});
test('not supported actions are filtered out', () => {

View File

@@ -79,9 +79,18 @@
return this.div(checkbox, this.msg(title));
};
GrPluginActionContext.prototype.prependLabel = function(title, checkbox) {
return this.label(checkbox, title);
};
GrPluginActionContext.prototype.call = function(payload, onSuccess) {
this.plugin._send(
this.action.method, '/' + this.action.__key, onSuccess, payload);
if (!this.action.__url) {
console.warn(`Unable to ${this.action.method} to ${this.action.__key}!`);
return;
}
this.plugin.restApi()
.send(this.action.method, this.action.__url, payload)
.then(onSuccess);
};
window.GrPluginActionContext = GrPluginActionContext;

View File

@@ -115,13 +115,17 @@ limitations under the License.
instance.action = {
method: 'METHOD',
__key: 'key',
__url: '/changes/1/revisions/2/foo~bar',
};
sandbox.stub(plugin, '_send');
const sendStub = sandbox.stub().returns(Promise.resolve());
sandbox.stub(plugin, 'restApi').returns({
send: sendStub,
});
const payload = {foo: 'foo'};
const successStub = sandbox.stub();
instance.call(payload, successStub);
assert.isTrue(
plugin._send.calledWith('METHOD', '/key', successStub, payload));
assert.isTrue(sendStub.calledWith(
'METHOD', '/changes/1/revisions/2/foo~bar', payload));
});
});
</script>