Add a plugin API for coverage data

Change-Id: Id09bf2da548b8def10420ea85addc9575c734ab4
This commit is contained in:
brohlfs
2019-03-01 17:28:32 +01:00
committed by Ben Rohlfs
parent b2577e4f78
commit 2a07f944f5
4 changed files with 91 additions and 0 deletions

View File

@@ -26,6 +26,8 @@
// notifying their listeners in the notify function.
this._annotationLayers = [];
this._coverageProvider = null;
// Default impl is a no-op.
this._addLayerFunc = annotationActionsContext => {};
}
@@ -55,6 +57,37 @@
return this;
};
/**
* The specified function will be called when a gr-diff component is built,
* and feeds the returned coverage data into the diff. Optional.
*
* Be sure to call this only once and only from one plugin. Multiple coverage
* providers are not supported. A second call will just overwrite the
* provider of the first call.
*
* TODO(brohlfs): Replace Array<Object> type by Array<Gerrit.CoverageRange>.
*
* @param {function(changeNum, path, basePatchNum, patchNum):
* !Promise<!Array<Object>>} coverageProvider
* @return {GrAnnotationActionsInterface}
*/
GrAnnotationActionsInterface.prototype.setCoverageProvider = function(
coverageProvider) {
if (this._coverageProvider) {
console.warn('Overwriting an existing coverage provider.');
}
this._coverageProvider = coverageProvider;
return this;
};
/**
* Used by Gerrit to look up the coverage provider. Not intended to be called
* by plugins.
*/
GrAnnotationActionsInterface.prototype.getCoverageProvider = function() {
return this._coverageProvider;
};
/**
* Returns a checkbox HTMLElement that can be used to toggle annotations
* on/off. The checkbox will be initially disabled. Plugins should enable it

View File

@@ -228,6 +228,36 @@
return layers;
},
/**
* Retrieves coverage data possibly provided by a plugin.
*
* Will wait for plugins to be loaded. If multiple plugins offer a coverage
* provider, the first one is used. If no plugin offers a coverage provider,
* will resolve to [].
*
* TODO(brohlfs): Replace Array<Object> type by Array<Gerrit.CoverageRange>.
*
* @param {string|number} changeNum
* @param {string} path
* @param {string|number} basePatchNum
* @param {string|number} patchNum
* @return {!Promise<!Array<Object>>}
*/
getCoverageRanges(changeNum, path, basePatchNum, patchNum) {
return Gerrit.awaitPluginsLoaded().then(() => {
for (const annotationApi of
this._getEventCallbacks(EventType.ANNOTATE_DIFF)) {
const provider = annotationApi.getCoverageProvider();
// Only one coverage provider makes sense. If there are more, then we
// simply ignore them.
if (provider) {
return provider(changeNum, path, basePatchNum, patchNum);
}
}
return [];
});
},
getAdminMenuLinks() {
const links = [];
for (const adminApi of