-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-dashboard.js
465 lines (401 loc) · 14 KB
/
generate-dashboard.js
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
import { writeFile } from 'fs/promises';
import { mkdir } from 'fs/promises';
const GITHUB_ORGS = ['mage-os', 'mage-os-lab'];
async function fetchOrgData(orgName) {
const query = `
query ($org: String!) {
organization(login: $org) {
repositories(first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
name
url
updatedAt
isArchived
issues(states: OPEN, first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
totalCount
nodes {
title
url
createdAt
updatedAt
labels(first: 5) {
nodes {
name
color
}
}
}
}
pullRequests(states: OPEN, first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
totalCount
nodes {
title
url
createdAt
updatedAt
author {
login
}
}
}
}
}
}
}
`;
const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ query, variables: { org: orgName } })
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}
const result = await response.json();
// Filter out archived repositories
if (result.data && result.data.organization) {
result.data.organization.repositories.nodes = result.data.organization.repositories.nodes.filter(repo => !repo.isArchived);
}
return result;
}
function generateOrgSection(orgName, data) {
const repos = data.data.organization.repositories.nodes;
const activeRepos = repos.filter(repo =>
repo.issues.totalCount > 0 || repo.pullRequests.totalCount > 0
);
if (activeRepos.length === 0) {
return '';
}
return `
<section class="mb-5">
<h2 class="display-6 mb-4">${orgName}</h2>
<div class="two-columns">
${activeRepos.map(repo => `
<div class="col">
<div class="card h-100">
<div class="card-header">
<h2><a href="${repo.url}" class="text-decoration-none" target="_blank">${repo.name}</a></h2>
</div>
<div class="card-body">
${repo.issues.totalCount > 0 ? `
<h3 class="h6 table-title">Issues</h3>
<table class="table table-hover">
<tbody>
${repo.issues.nodes.map(issue => `
<tr>
<td>
<a href="${issue.url}" class="text-decoration-none truncate-text" target="_blank" title="${issue.title}">${issue.title}</a>
</td>
</tr>
`).join('')}
</tbody>
</table>
` : ''}
${repo.pullRequests.totalCount > 0 ? `
<h3 class="h6 table-title">Pull Requests</h3>
<table class="table table-hover">
<tbody>
${repo.pullRequests.nodes.map(pr => `
<tr>
<td>
<a href="${pr.url}" class="text-decoration-none truncate-text" target="_blank" title="${pr.title}">${pr.title}</a>
</td>
</tr>
`).join('')}
</tbody>
</table>
` : ''}
</div>
</div>
</div>
`).join('')}
</div>
</section>
`;
}
async function generateWorkflowRunsSection(orgDataMap) {
// Only get repositories from the mage-os organization
const mageOsRepos = orgDataMap['mage-os']?.data?.organization?.repositories?.nodes || [];
// Filter out archived repositories and sort alphabetically
const sortedRepos = mageOsRepos
.filter(repo => !repo.isArchived)
.sort((a, b) => a.name.localeCompare(b.name));
// Fetch workflow runs for each repository
const reposWithRuns = await Promise.all(
sortedRepos.map(async (repo) => {
try {
const runsData = await fetchWorkflowRunsForRepo('mage-os', repo.name);
return {
...repo,
lastRunDate: runsData.workflow_runs && runsData.workflow_runs.length > 0
? new Date(runsData.workflow_runs[0].created_at)
: null
};
} catch (error) {
console.error(`Error fetching workflow runs for ${repo.name}:`, error);
return {
...repo,
lastRunDate: null
};
}
})
);
return `
<section class="mb-5">
<h2 class="display-6 mb-4">Workflow Runs</h2>
<table id="workflowRunsTable" class="table table-bordered table-hover sortable" style="width:auto">
<thead>
<tr>
<th>Repository</th>
<th>Last Workflow Run</th>
</tr>
</thead>
<tbody>
${reposWithRuns.map(repo => {
const formattedDate = repo.lastRunDate
? repo.lastRunDate.toISOString().replace('T', ' ').substring(0, 19)
: '-';
return `
<tr>
<td><a href="${repo.url}" class="text-decoration-none" target="_blank">${repo.name}</a></td>
<td>${formattedDate}</td>
</tr>
`;
}).join('')}
</tbody>
</table>
</section>
`;
}
async function fetchWorkflowRunsForRepo(owner, repo) {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/actions/runs?per_page=1`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
'Accept': 'application/vnd.github+json'
}
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}
return response.json();
}
async function fetchMagentoRepos() {
let page = 1;
let allRepos = [];
let hasMoreRepos = true;
while (hasMoreRepos) {
const response = await fetch(`https://api.github.com/orgs/magento/repos?per_page=100&page=${page}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
'Accept': 'application/vnd.github+json'
}
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText} (Status: ${response.status})`);
}
const repos = await response.json();
// If we received fewer repos than requested, this must be the last page
if (repos.length < 100) {
hasMoreRepos = false;
}
allRepos = [...allRepos, ...repos];
page++;
}
// Filter out archived repositories
return allRepos.filter(repo => !repo.archived);
}
async function generateMissingMirrorsSection(orgDataMap) {
// Fetch all non-archived repositories from the Magento organization
console.log('Fetching non-archived Magento repositories...');
const magentoRepos = await fetchMagentoRepos();
// Extract all Mage-OS repositories
const mageOsRepos = orgDataMap['mage-os']?.data?.organization?.repositories?.nodes || [];
// Create a Map of Magento repos for faster lookups
const magentoReposMap = new Map(
magentoRepos.map(repo => [repo.name, repo])
);
// Create a Set of mirrored repo names (without the "mirror-" prefix) for faster lookups
const mirroredRepoNames = new Set(
mageOsRepos
.filter(repo => repo.name.startsWith('mirror-'))
.map(repo => repo.name.substring(7))
);
// Find repositories that don't have mirrors - more efficient than filter
const unmirroredRepos = [];
for (const [name, repo] of magentoReposMap.entries()) {
if (!mirroredRepoNames.has(name)) {
unmirroredRepos.push(repo);
}
}
// Sort only once after filtering
unmirroredRepos.sort((a, b) => a.name.localeCompare(b.name));
// Pre-compile the row generation function outside the loop
const generateRow = repo => {
const formattedDate = repo.updated_at
? new Date(repo.updated_at).toISOString().split('T')[0] + ' ' +
new Date(repo.updated_at).toISOString().split('T')[1].substring(0, 8)
: '-';
return `
<tr>
<td><a href="${repo.html_url}" class="text-decoration-none" target="_blank">${repo.name}</a></td>
<td>${repo.description || '-'}</td>
<td>${formattedDate}</td>
</tr>
`;
};
// Build all rows at once and join
const tableRows = unmirroredRepos.map(generateRow).join('');
return `
<section class="mb-5">
<h2 class="display-6 mb-4">Magento Repositories Without Mage-OS Mirrors</h2>
<table class="table table-bordered table-hover sortable" style="width:auto">
<thead>
<tr>
<th>Repository</th>
<th>Description</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
${tableRows}
</tbody>
</table>
</section>
`;
}
function generateHTML(orgSections, missingMirrorsSection, workflowSection) {
const lastUpdate = new Date().toISOString();
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mage-OS Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/sortable.min.js" defer></script>
<style>
body {
padding: 1rem;
}
.two-columns {
max-width: 1400px;
columns: 2;
column-gap: 1.5rem;
}
.label {
display: inline-block;
padding: 0.15rem 0.5rem;
border-radius: 1rem;
font-size: 0.75em;
margin: 0.15rem 0.15rem 0.15rem 0;
}
.last-update {
color: #6c757d;
font-size: 0.8em;
}
.card {
height: 100%;
break-inside: avoid;
margin-bottom: 1.5rem;
display: inline-block;
width: 100%;
}
.card-header h2 {
margin: 0;
font-size: 1.25rem;
}
.card-body {
padding: 0 !important;
}
.table {
font-size: 0.9rem;
margin-bottom: 0;
table-layout: fixed;
}
.card .table {
width: 100%;
}
.card .table td {
vertical-align: middle;
width: 100%;
max-width: 0;
}
.card .table:last-child tr:last-child td {
border-bottom: none;
}
.card-body {
padding: 1rem 0;
padding-bottom: 0;
}
.table-title {
padding: 0.5rem;
margin-block: 1rem 0;
}
.truncate-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: block;
}
.sortable th {
cursor: pointer;
}
.sortable th[aria-sort=ascending]::after {
content: " ▲";
}
.sortable th[aria-sort=descending]::after {
content: " ▼";
}
</style>
</head>
<body>
<div class="container">
<header class="mb-4">
<h1 class="display-5">Mage-OS Dashboard</h1>
<p class="last-update">Last updated: ${new Date(lastUpdate).toLocaleString()}</p>
</header>
${orgSections}
${missingMirrorsSection}
${workflowSection}
</div>
</body>
</html>
`;
}
async function main() {
try {
const orgDataMap = {};
for (const orgName of GITHUB_ORGS) {
console.log(`Fetching data for ${orgName}...`);
const data = await fetchOrgData(orgName);
if (data.errors) {
console.error(`Error fetching data for ${orgName}:`, data.errors);
continue; // Skip this org but continue with others
}
orgDataMap[orgName] = data;
}
if (Object.keys(orgDataMap).length === 0) {
throw new Error('No organization data was successfully retrieved');
}
let orgSections = '';
for (const [orgName, data] of Object.entries(orgDataMap)) {
orgSections += generateOrgSection(orgName, data);
}
const missingMirrorsSection = await generateMissingMirrorsSection(orgDataMap);
const workflowSection = await generateWorkflowRunsSection(orgDataMap);
const html = generateHTML(orgSections, missingMirrorsSection, workflowSection);
await mkdir('dist', { recursive: true });
await writeFile('dist/index.html', html);
console.log('Dashboard generated successfully!');
} catch (error) {
console.error('Error generating dashboard:', error);
process.exit(1);
}
}
main();