-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks-plugin.js
More file actions
376 lines (337 loc) · 11 KB
/
checks-plugin.js
File metadata and controls
376 lines (337 loc) · 11 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
/**
* FirmwareCI Checks Plugin for Gerrit
* Provides integration between FirmwareCI job requests and Gerrit checks.
* @version 1.0.0
* @author FirmwareCI Team
*/
// Configuration constants
const CONFIG = Object.freeze({
PLUGIN_VERSION: "1.0.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" },
},
});
/**
* 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 API
* @param {Object} changeData - Change data from Gerrit
* @returns {Promise<Object>} - Check response for Gerrit
*/
async fetch(changeData) {
try {
const requestUrl = `${CONFIG.FIRMWARE_CI_API_URL}/gerrit-checks`;
const requestBody = {
change_number: changeData.changeNumber.toString(),
patchset: changeData.patchsetNumber.toString(),
project: changeData.repo,
commit_hash: changeData.patchsetSha,
};
const response = await fetch(requestUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(requestBody),
});
if (!response.ok) {
return this.handleErrorResponse(response);
}
const apiData = await response.json();
return this.mapToChecksResponse(apiData, changeData);
} 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)
}`,
};
}
}
/**
* Handles error responses from the API
* @param {Response} response - Fetch API response object
* @returns {Object} - Error response for Gerrit
*/
async handleErrorResponse(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 {
responseCode: "ERROR",
errorMessage,
};
}
/**
* Maps FirmwareCI API data to Gerrit checks format
* @param {Object} apiData - Data from FirmwareCI API
* @param {Object} changeData - Change data from Gerrit
* @returns {Object} - Check response for Gerrit
*/
mapToChecksResponse(apiData, changeData) {
// 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);
console.log(runs);
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 to generate Gerrit check runs
* @param {Array} jobRequests - List of job requests from API
* @param {Object} changeData - Change data from Gerrit
* @returns {Array} - List of runs for Gerrit checks
*/
processJobRequests(jobRequests, changeData) {
// Group job requests by workflow ID and assign attempt numbers
const workflowGroups = this.groupJobRequestsByWorkflow(jobRequests);
this.assignAttemptNumbers(workflowGroups);
// Build check runs from job requests
return jobRequests.map((jobRequest) =>
this.createJobRequestRun(jobRequest, changeData)
);
}
/**
* 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
* @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
* @param {Object} jobRequest - Job request data
* @param {Object} changeData - Change data from Gerrit
* @returns {Object} - Check run for Gerrit
*/
createJobRequestRun(jobRequest, changeData) {
const jobStats = this.calculateJobStats(jobRequest.jobs || []);
let 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`);
const statusDescription = `${jobStats.total} jobs${
statusParts.length > 0 ? ` (${statusParts.join(", ")})` : ""
}`;
const requestId = jobRequest.id;
const run = {
change: changeData.changeNumber,
patchset: changeData.patchsetNumber,
externalId: requestId,
checkName: `Firmware-CI: ${jobRequest.workflow_name}`,
checkDescription: `${jobRequest.workflow_name}`,
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.createJobRequestLink(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
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 link for a job request
* @param {string} requestId - Job request ID
* @returns {Link} - Link object for Gerrit
*/
createJobRequestLink(requestId) {
return {
url: `${CONFIG.FIRMWARE_CI_URL}/${CONFIG.ORGANIZATION}/job-requests/${requestId}`,
tooltip: "View workflow details in FirmwareCI",
primary: true,
icon: "EXTERNAL",
};
}
/**
* Creates a result for an individual job
* @param {Object} job - Job data
* @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,
checkName: job.test?.name || "Unnamed Test",
category: statusInfo.category,
summary: job.error || job.test?.description || "",
attempt: attempt,
tags: [
{
name: jobStatus.toUpperCase(),
color: statusInfo.color,
},
],
links: [
{
url: `${CONFIG.FIRMWARE_CI_URL}/${CONFIG.ORGANIZATION}/jobs/${job.id}`,
tooltip: "View job details",
primary: true,
icon: "EXTERNAL",
},
],
};
}
/**
* Handles errors from rerun API calls
* @param {Response} response - Fetch API response object
* @returns {Object} - Error response for Gerrit
*/
async handleRerunError(response) {
const errorText = await response.text();
let errorMessage = errorText;
try {
const errorData = JSON.parse(errorText);
errorMessage = errorData.error || errorText;
} catch (e) {
// Use default error message if parsing fails
}
return { message: `Failed to rerun job: ${errorMessage}` };
}
/**
* 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 }
);
});