-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortable-badges.js
More file actions
162 lines (133 loc) · 5.71 KB
/
sortable-badges.js
File metadata and controls
162 lines (133 loc) · 5.71 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
// This is free and unencumbered software released into the public domain.
// See the UNLICENSE file for details.
// Thanks to Claude AI for the batch-based loading
// approach to stay within GitHub's request rate limit.
function addTokenUI() {
const controlDiv = document.createElement('div');
controlDiv.style.position = 'fixed';
controlDiv.style.top = '10px';
controlDiv.style.left = '10px';
controlDiv.style.background = '#f0f0f0';
controlDiv.style.padding = '10px';
controlDiv.style.borderRadius = '5px';
controlDiv.style.border = '1px solid #ccc';
const tokenInput = document.createElement('input');
tokenInput.type = 'password';
tokenInput.placeholder = 'GitHub token (workflow read only)';
tokenInput.style.marginRight = '10px';
const loadButton = document.createElement('button');
loadButton.textContent = 'Load Build Status';
const progress = document.createElement('div');
progress.style.marginTop = '5px';
progress.style.fontSize = '12px';
controlDiv.appendChild(tokenInput);
controlDiv.appendChild(loadButton);
controlDiv.appendChild(progress);
document.body.appendChild(controlDiv);
return { tokenInput, loadButton, progress };
}
async function getWorkflowStatus(badgeUrl, token) {
const match = badgeUrl.match(/github\.com\/([^/]+)\/([^/]+)\/actions\/workflows\/([^/]+)\/badge\.svg/);
if (!match) return null;
const [, owner, repo, workflow] = match;
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow}/runs?per_page=1`;
try {
console.log('Fetching:', apiUrl);
const headers = token ? { 'Authorization': `token ${token}` } : {};
const response = await fetch(apiUrl, { headers });
if (!response.ok) {
console.error('API request failed:', response.status, await response.text());
return null;
}
const data = await response.json();
console.log('API response:', data);
if (data.workflow_runs && data.workflow_runs.length > 0) {
const latestRun = data.workflow_runs[0];
return {
conclusion: latestRun.conclusion || 'unknown',
status: latestRun.status,
timestamp: latestRun.updated_at
};
}
return null;
} catch (error) {
console.error('Error fetching workflow status:', error);
return null;
}
}
function getSortKey(status) {
if (!status) return 'z_unknown';
// Sort successful builds first, then in-progress, then failed
if (status.conclusion === 'success') return 'a_success_' + status.timestamp;
if (status.status === 'in_progress') return 'b_running_' + status.timestamp;
if (status.conclusion === 'failure') return 'c_failed_' + status.timestamp;
// Other states like skipped, cancelled, etc.
return 'd_other_' + status.conclusion + '_' + status.timestamp;
}
async function loadBadgesGradually(token, progressCallback) {
// Initialize sort keys
const badges = document.getElementsByClassName('badge');
Array.from(badges).forEach(badge => {
badge.setAttribute('sorttable_customkey', 'loading');
badge.setAttribute('title', 'loading');
});
const badgeArray = Array.from(badges);
const batchSize = 1; // number of badges per batch
const delayMs = 200; // time between batches
let processedCount = 0;
async function loadBatch(startIndex) {
const batch = badgeArray.slice(startIndex, startIndex + batchSize);
console.log(`Loading batch starting at ${startIndex}`);
// Process each badge in the batch
for (const badge of batch) {
const img = badge.querySelector('img');
if (img && img.dataset.src) {
console.log('Processing badge:', img.dataset.src);
// Get workflow status first
const status = await getWorkflowStatus(img.dataset.src, token);
const sortKey = getSortKey(status);
badge.setAttribute('sorttable_customkey', sortKey);
badge.setAttribute('title', sortKey);
console.log('Set sort key:', sortKey);
// Then load the badge image
img.src = img.dataset.src;
// Update progress
processedCount++;
progressCallback(processedCount, badgeArray.length);
}
}
// Schedule next batch if there are more badges
if (startIndex + batchSize < badgeArray.length) {
console.log(`Scheduling next batch in ${delayMs}ms`);
return new Promise(resolve => {
setTimeout(() => {
loadBatch(startIndex + batchSize).then(resolve);
}, delayMs);
});
}
}
// Start loading first batch.
await loadBatch(0);
}
// Initialize UI and handle load button click.
const { tokenInput, loadButton, progress } = addTokenUI();
loadButton.addEventListener('click', async () => {
const token = tokenInput.value.trim();
if (!token) {
alert('Please enter a GitHub token. You can create one at https://github.com/settings/tokens with only "workflow" read permission.');
return;
}
loadButton.disabled = true;
loadButton.textContent = 'Loading...';
try {
await loadBadgesGradually(token, (current, total) => {
progress.textContent = `Progress: ${current}/${total} (${Math.round(current/total*100)}%)`;
});
} catch (error) {
console.error('Error loading badges:', error);
alert('Error loading badges: ' + error.message);
} finally {
loadButton.disabled = false;
loadButton.textContent = 'Load Build Status';
}
});