feat(contrib/frontend/gerrit): design a simple status & check frontend for Gerrit

It uses imaginary APIs for now, but it's OK.

This has showed up a bunch of generalizations we will need in our own
API.

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
This commit is contained in:
raito 2024-12-17 01:20:47 +01:00
parent ee8a644830
commit 2c086a6d78

88
contrib/checks-ofborg.js Normal file
View file

@ -0,0 +1,88 @@
/* Inspired from the Lix setup.
* Inspired from the Buildbot setup.
*
* Designed for OfBorg custom checks & server API.
* Original-Author: puckipedia
*/
Gerrit.install((plugin) => {
const serverInfo = plugin.serverInfo();
const { statcheck_base_uri, enabled_projects } = serverInfo.plugin;
const configuration = {
baseUri: statcheck_base_uri,
// TODO: use directly ofborg API for this.
supportedProjects: enabled_projects,
};
function makeStatcheckUri(suffix) {
return `${configuration.baseUri}/${suffix}`;
}
let checksProvider;
checksProvider = {
async fetch({ repo, patchsetSha, changeNumber, patchsetNumber }, runBefore = false) {
if (!configuration.supportedProjects.includes(repo)) {
return { responseCode: 'OK' };
}
let num = changeNumber.toString(10);
// Iterate over all check runs.
let checksFetch = await fetch(makeStatcheckUri(`changes/${num}/versions/${patchsetNumber}/checks`), { credentials: 'include' });
if (checksFetch.status === 400) {
if ((await checksFetch.json()).error === 'invalid origin' && !runBefore) {
return await checksProvider.fetch({ repo, patchsetSha, changeNumber, patchsetNumber }, true);
}
return { responseCode: 'OK' }
} else if (checksFetch.status === 403) {
console.warn(`Failed to fetch change '${changeNumber}' for authorization reasons, automatic login is still a WIP.`);
return { responseCode: 'NOT_LOGGED_IN', loginCallback() {
} };
}
let checks = await checksFetch.json();
if (checks.length === 0) {
return { responseCode: 'OK' };
}
let runs = [];
let links = [];
for (let check of checks) {
let checkrun = {
attempt: check.id,
checkName: check.name,
externalId: check.id,
status: check.status,
checkLink: null, // TODO: have a proper and nice web URI
labelName: 'Verified', // TODO: generalize what label a check affects.
results: [],
links: [], // TODO: have a proper web uri
};
if (check.started_at !== null) {
checkrun.startedTimestamp = new Date(check.started_at * 1000);
}
if (check.completed_at !== null) {
checkrun.finishedTimestamp = new Date(check.completed_at * 1000);
}
if (check.results !== null) {
checkrun.results = [
{
category: "SUCCESS",
summary: check.summary
}
];
}
runs.push(checkrun);
}
return { responseCode: 'OK', runs, links };
}
};
plugin.checks().register(checksProvider);
});