-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks-plugin.js
More file actions
524 lines (471 loc) · 16.3 KB
/
Copy pathchecks-plugin.js
File metadata and controls
524 lines (471 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* FirmwareCI Checks Plugin for Gerrit
* Provides integration between FirmwareCI job requests and Gerrit checks.
* @version 1.1.0
* @author FirmwareCI Team
*/
// Configuration constants
const CONFIG = Object.freeze({
PLUGIN_VERSION: "1.1.0",
API_VERSION: "3.12.0",
FIRMWARE_CI_API_URL: "https://api.firmwareci.9esec.dev:8443/v0",
FIRMWARE_CI_URL: "https://app.firmware-ci.com",
ORGANIZATION: "ORG-NAME",
POLLING_INTERVAL_SECONDS: 60,
});
// Job status mapping
const STATUS_MAP = Object.freeze({
WORKFLOW: {
queued: "SCHEDULED",
preparing: "SCHEDULED",
running: "RUNNING",
succeeded: "COMPLETED",
failed: "COMPLETED",
aborted: "COMPLETED",
},
JOB: {
succeeded: { category: "SUCCESS", color: "CYAN" },
failed: { category: "ERROR", color: "PINK" },
aborted: { category: "WARNING", color: "GRAY" },
running: { category: "INFO", color: "GRAY" },
queued: { category: "INFO", color: "GRAY" },
preparing: { category: "INFO", color: "GRAY" },
},
});
// Gerrit ChangeKind values that mean the code did NOT change between patchsets.
// REWORK is the only kind that signals a real code change; everything else
// (trivial rebases, message-only updates, merge first-parent updates) leaves the
// tree identical. Used to decide when checks from an older patchset may be carried
// over to a newer one that CI never ran on (e.g. the cherry-pick patchset Gerrit
// creates at submit time).
function isNonCodeChange(kind) {
return !!kind && kind !== "REWORK";
}
/**
* Main plugin class that handles FirmwareCI integration with Gerrit checks
*/
class FirmwareChecks {
/**
* Creates a new instance of the FirmwareCI checks provider
* @param {Object} plugin - The Gerrit plugin instance
*/
constructor(plugin) {
this.plugin = plugin;
}
/**
* Fetches check results from FirmwareCI for a Gerrit change.
*
* The FirmwareCI server pins job requests to the exact patchset/commit they ran
* on. When the current patchset has no runs (typically the cherry-pick patchset
* Gerrit creates at submit, which CI never ran on) we fall back to the most
* recent earlier patchset that does have runs, but only across non-code-change
* patchsets so we never present stale results for changed code.
*
* @param {Object} changeData - Change data from Gerrit
* @returns {Promise<Object>} - Check response for Gerrit
*/
async fetch(changeData) {
try {
// Fast path: query the patchset currently being viewed.
let apiData;
try {
apiData = await this.queryChecks(
changeData.changeNumber,
changeData.patchsetNumber,
changeData.repo,
changeData.patchsetSha
);
} catch (error) {
return {
responseCode: "ERROR",
errorMessage:
error instanceof Error ? error.message : String(error),
};
}
if (apiData?.data?.length > 0) {
return this.mapToChecksResponse(apiData, changeData);
}
// Fallback: carry over runs from the newest earlier patchset that has them,
// as long as nothing but non-code-change patchsets sit in between.
const fallback = await this.findFallbackChecks(changeData);
if (fallback) {
return this.mapToChecksResponse(
fallback.apiData,
changeData,
fallback.fromPatchset
);
}
return { responseCode: "OK", summaryMessage: "", runs: [] };
} catch (error) {
console.error("Error fetching checks from FirmwareCI:", error);
return {
responseCode: "ERROR",
errorMessage: `Failed to fetch checks: ${
error instanceof Error ? error.message : String(error)
}`,
};
}
}
/**
* Queries the FirmwareCI gerrit-checks endpoint for one patchset.
* @returns {Promise<Object>} - Parsed API response
* @throws {Error} - When the API responds with a non-OK status
*/
async queryChecks(changeNumber, patchset, project, commitHash) {
const requestUrl = `${CONFIG.FIRMWARE_CI_API_URL}/gerrit-checks`;
const requestBody = {
change_number: String(changeNumber),
patchset: String(patchset),
project: project,
commit_hash: commitHash,
};
const response = await fetch(requestUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error(await this.extractErrorMessage(response));
}
return response.json();
}
/**
* Finds runs to carry over from an earlier patchset when the current one is
* empty. Walks patchsets downward from the current one, stopping as soon as a
* real code change (REWORK) is encountered, and returns the first earlier
* patchset that has runs.
* @param {Object} changeData - Change data from Gerrit
* @returns {Promise<?{apiData: Object, fromPatchset: number}>}
*/
async findFallbackChecks(changeData) {
const current = Number(changeData.patchsetNumber);
if (!Number.isFinite(current) || current <= 1) {
return null;
}
let revisions;
try {
revisions = await this.fetchRevisions(changeData.changeNumber);
} catch (error) {
console.error(
"FirmwareCI: failed to load Gerrit revisions for fallback:",
error
);
return null;
}
for (let p = current - 1; p >= 1; p--) {
// The step from patchset p to p+1 must be a non-code change for runs on p
// to still describe the code at the current patchset.
const above = revisions[p + 1];
if (!above || !isNonCodeChange(above.kind)) {
break;
}
const rev = revisions[p];
if (!rev) continue;
let apiData;
try {
apiData = await this.queryChecks(
changeData.changeNumber,
p,
changeData.repo,
rev.sha
);
} catch (error) {
// A failed fallback probe is treated as "no runs", not a hard error.
continue;
}
if (apiData?.data?.length > 0) {
return { apiData, fromPatchset: p };
}
}
return null;
}
/**
* Loads the change's revisions from Gerrit, mapped by patchset number.
* @param {(string|number)} changeNumber
* @returns {Promise<Object<number, {sha: string, kind: string}>>}
*/
async fetchRevisions(changeNumber) {
const change = await this.plugin
.restApi()
.get(`/changes/${changeNumber}/?o=ALL_REVISIONS`);
const map = {};
const revisions = change?.revisions || {};
for (const sha of Object.keys(revisions)) {
const rev = revisions[sha];
map[rev._number] = { sha, kind: rev.kind };
}
return map;
}
/**
* Builds the error message from a non-OK API response.
* @param {Response} response - Fetch API response object
* @returns {Promise<string>}
*/
async extractErrorMessage(response) {
let errorMessage = `HTTP ${response.status}`;
try {
const errorBody = await response.text();
const errorData = JSON.parse(errorBody);
errorMessage = errorData.error || errorMessage;
} catch (e) {
// Use default error message if parsing fails
}
return errorMessage;
}
/**
* Maps FirmwareCI API data to Gerrit checks format
* @param {Object} apiData - Data from FirmwareCI API
* @param {Object} changeData - Change data from Gerrit
* @param {number} [carriedFromPatchset] - Set when runs are carried over from
* an earlier (no-code-change) patchset; used to annotate the runs.
* @returns {Object} - Check response for Gerrit
*/
mapToChecksResponse(apiData, changeData, carriedFromPatchset) {
// Handle empty or invalid responses
if (!apiData?.data || apiData.data.length === 0) {
return {
responseCode: "OK",
summaryMessage: "",
runs: [],
};
}
try {
const runs = this.processJobRequests(
apiData.data,
changeData,
carriedFromPatchset
);
return { responseCode: "OK", runs };
} catch (error) {
console.error("Error processing API data:", error);
return {
responseCode: "ERROR",
errorMessage: `Failed to process API data: ${
error instanceof Error ? error.message : String(error)
}`,
};
}
}
/**
* Processes job requests into Gerrit check runs.
*
* One run per workflow (job request); its individual test jobs are the run's
* results. This matches Gerrit's run/result model: selecting a run in the Runs
* panel narrows the Results to that workflow's tests, and the result filter box
* matches on checkName/summary/tags across them.
*
* @param {Array} jobRequests - List of job requests from API
* @param {Object} changeData - Change data from Gerrit
* @param {number} [carriedFromPatchset]
* @returns {Array} - List of runs for Gerrit checks
*/
processJobRequests(jobRequests, changeData, carriedFromPatchset) {
// Group job requests by workflow ID and assign attempt numbers so reruns of
// a workflow collapse into attempts of one run.
const workflowGroups = this.groupJobRequestsByWorkflow(jobRequests);
this.assignAttemptNumbers(workflowGroups);
return jobRequests.map((jobRequest) =>
this.createJobRequestRun(jobRequest, changeData, carriedFromPatchset)
);
}
/**
* Groups job requests by their workflow ID
* @param {Array} jobRequests - List of job requests
* @returns {Object} - Job requests grouped by workflow ID
*/
groupJobRequestsByWorkflow(jobRequests) {
const workflowGroups = {};
for (const jobRequest of jobRequests) {
const workflowId = jobRequest.workflow_id || "unknown";
if (!workflowGroups[workflowId]) {
workflowGroups[workflowId] = [];
}
workflowGroups[workflowId].push(jobRequest);
}
return workflowGroups;
}
/**
* Assigns attempt numbers to job requests based on their position in the
* workflow group. Job requests arrive newest-first, so the newest gets the
* highest attempt number.
* @param {Object} workflowGroups - Job requests grouped by workflow ID
*/
assignAttemptNumbers(workflowGroups) {
for (const workflowId in workflowGroups) {
const requests = workflowGroups[workflowId];
const lastIndex = requests.length;
requests.forEach((jobRequest, index) => {
// Newest has highest attempt number
jobRequest._attempt = lastIndex - index;
});
}
}
/**
* Creates a check run for a job request (one run per workflow).
* @param {Object} jobRequest - Job request data
* @param {Object} changeData - Change data from Gerrit
* @param {number} [carriedFromPatchset]
* @returns {Object} - Check run for Gerrit
*/
createJobRequestRun(jobRequest, changeData, carriedFromPatchset) {
const jobStats = this.calculateJobStats(jobRequest.jobs || []);
const statusParts = [];
if (jobStats.succeeded > 0) statusParts.push(`${jobStats.succeeded} succeeded`);
if (jobStats.failed > 0) statusParts.push(`${jobStats.failed} failed`);
if (jobStats.queued > 0) statusParts.push(`${jobStats.queued} queued`);
if (jobStats.running > 0) statusParts.push(`${jobStats.running} running`);
let statusDescription = `${jobStats.total} jobs${
statusParts.length > 0 ? ` (${statusParts.join(", ")})` : ""
}`;
let checkDescription = jobRequest.workflow_name;
if (carriedFromPatchset) {
const note = `results carried from patch set ${carriedFromPatchset} (no code change since)`;
statusDescription = `${statusDescription} · ${note}`;
checkDescription = note;
}
const requestId = jobRequest.id;
const run = {
change: changeData.changeNumber,
patchset: changeData.patchsetNumber,
externalId: requestId,
checkName: `Firmware-CI: ${jobRequest.workflow_name}`,
checkDescription,
checkLink: encodeURI(`https://docs.firmware-ci.com`),
statusLink: encodeURI(
`${CONFIG.FIRMWARE_CI_URL}/${CONFIG.ORGANIZATION}/job-requests/${requestId}`
),
labelName: "Verified",
status: this.mapWorkflowStatus(jobRequest.status),
attempt: jobRequest._attempt || 0,
statusDescription,
links: [this.jobRequestLink(requestId)],
results: [],
};
// Add timestamps if available
if (
jobRequest.start_time &&
jobRequest.start_time !== "0001-01-01T00:00:00Z"
) {
run.startedTimestamp = new Date(jobRequest.start_time);
run.scheduledTimestamp = new Date(jobRequest.start_time);
if (jobRequest.duration) {
run.finishedTimestamp = new Date(
run.startedTimestamp.getTime() +
this.parseDuration(jobRequest.duration)
);
}
}
// Add results for individual jobs (tests)
if (jobRequest.jobs?.length > 0) {
run.results = jobRequest.jobs.map((job) =>
this.createJobResult(job, jobRequest._attempt)
);
}
return run;
}
/**
* Calculates job statistics for a job request
* @param {Array} jobs - List of jobs
* @returns {Object} - Job statistics
*/
calculateJobStats(jobs) {
const total = jobs.length;
const succeeded = jobs.filter((job) => job.status === "succeeded").length;
const failed = jobs.filter((job) => job.status === "failed").length;
const queued = jobs.filter((job) => job.status === "queued").length;
const running = jobs.filter((job) => job.status === "running").length;
return { total, succeeded, failed, queued, running };
}
/**
* Creates a result for an individual job (test) within a workflow run.
*
* Intentionally omits `checkName`: Gerrit's RunResult overrides the run's
* checkName with the result's own if present, which breaks run-based filtering
* (results are matched to their run by equal checkName). Leaving it unset makes
* the result inherit the run's "Firmware-CI: <workflow>" name, so selecting the
* workflow run shows its tests. The test name goes in `summary`.
*
* @param {Object} job - Job data
* @param {number} attempt - Attempt number
* @returns {Object} - Result object for Gerrit
*/
createJobResult(job, attempt) {
const jobStatus = job.status || "unknown";
const statusInfo = STATUS_MAP.JOB[jobStatus] || STATUS_MAP.JOB.queued;
return {
externalId: job.id,
category: statusInfo.category,
summary: job.test?.name || "Unnamed Test",
message: job.error || job.test?.description || "",
attempt: attempt,
tags: [
{
name: jobStatus.toUpperCase(),
color: statusInfo.color,
},
],
links: [this.jobLink(job.id)],
};
}
/**
* Link to a job's detail page in FirmwareCI.
* @param {string} jobId
* @returns {Object} - Link object for Gerrit
*/
jobLink(jobId) {
return {
url: `${CONFIG.FIRMWARE_CI_URL}/${CONFIG.ORGANIZATION}/jobs/${jobId}`,
tooltip: "View job details",
primary: true,
icon: "EXTERNAL",
};
}
/**
* Link to a job request's (workflow) detail page in FirmwareCI.
* @param {string} requestId
* @returns {Object} - Link object for Gerrit
*/
jobRequestLink(requestId) {
return {
url: `${CONFIG.FIRMWARE_CI_URL}/${CONFIG.ORGANIZATION}/job-requests/${requestId}`,
tooltip: "View workflow details in FirmwareCI",
primary: true,
icon: "EXTERNAL",
};
}
/**
* Maps workflow status to Gerrit check status
* @param {string} status - FirmwareCI workflow status
* @returns {string} - Gerrit check status
*/
mapWorkflowStatus(status) {
return STATUS_MAP.WORKFLOW[status] || "RUNNABLE";
}
/**
* Parses duration string to milliseconds
* @param {string} duration - Duration string (e.g., "10m32s")
* @returns {number} - Duration in milliseconds
*/
parseDuration(duration) {
let milliseconds = 0;
const minutes = duration.match(/(\d+)m/);
const seconds = duration.match(/(\d+)s/);
if (minutes?.[1]) {
milliseconds += parseInt(minutes[1], 10) * 60 * 1000;
}
if (seconds?.[1]) {
milliseconds += parseInt(seconds[1], 10) * 1000;
}
return milliseconds;
}
}
// Install the plugin
window.Gerrit.install(async (plugin) => {
const firmwareChecks = new FirmwareChecks(plugin);
plugin
.checks()
.register(
{ fetch: (change) => firmwareChecks.fetch(change) },
{ fetchPollingIntervalSeconds: CONFIG.POLLING_INTERVAL_SECONDS }
);
});