diff --git a/contrib/checks-ofborg.js b/contrib/checks-ofborg.js new file mode 100644 index 0000000..e47ef8d --- /dev/null +++ b/contrib/checks-ofborg.js @@ -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); +});