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 Antonio Barone
parent a62582cf65
commit 4cd7c02b2a
4 changed files with 91 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ limitations under the License.
<link rel="import" href="../../core/gr-reporting/gr-reporting.html">
<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
<link rel="import" href="../../shared/gr-comment-thread/gr-comment-thread.html">
<link rel="import" href="../../shared/gr-js-api-interface/gr-js-api-interface.html">
<link rel="import" href="../gr-diff/gr-diff.html">
<dom-module id="gr-diff-host">
@@ -45,8 +46,10 @@ limitations under the License.
error-message="[[_errorMessage]]"
base-image="[[_baseImage]]"
revision-image=[[_revisionImage]]
coverage-ranges="[[_coverageRanges]]"
blame="[[_blame]]"
diff="[[diff]]"></gr-diff>
<gr-js-api-interface id="jsAPI"></gr-js-api-interface>
<gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
<gr-reporting id="reporting" category="diff"></gr-reporting>
</template>

View File

@@ -193,6 +193,16 @@
value: null,
},
/**
* TODO(brohlfs): Replace Object type by Gerrit.CoverageRange.
*
* @type {!Array<!Object>}
*/
_coverageRanges: {
type: Array,
value: () => [],
},
_loadedWhitespaceLevel: String,
_parentIndex: {
@@ -247,6 +257,21 @@
this._errorMessage = null;
const whitespaceLevel = this._getIgnoreWhitespace();
this._coverageRanges = [];
const {changeNum, path, patchRange: {basePatchNum, patchNum}} = this;
this.$.jsAPI.getCoverageRanges(changeNum, path, basePatchNum, patchNum).
then(coverageRanges => {
if (changeNum !== this.changeNum ||
path !== this.path ||
basePatchNum !== this.patchRange.basePatchNum ||
patchNum !== this.patchRange.patchNum) {
return;
}
this._coverageRanges = coverageRanges;
}).catch(err => {
console.warn('Loading coverage ranges failed: ', err);
});
const diffRequest = this._getDiff()
.then(diff => {
this._loadedWhitespaceLevel = whitespaceLevel;

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 => {};
}
@@ -57,6 +59,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