-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
301 lines (264 loc) · 8.31 KB
/
main.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
let globalToken = '';
let currentPage = 1;
let totalPages = 1;
let owner = '';
let repo = '';
const perPage = 100; // Changed to 100 to match GitHub API's max per_page
let allItems = [];
let workflows = new Set();
let currentView = 'artifacts';
let isPrivateRepo = false;
function setView(view) {
currentView = view;
document
.getElementById('artifactsButton')
.classList.toggle('active', view === 'artifacts');
document
.getElementById('cachesButton')
.classList.toggle('active', view === 'caches');
}
function handleGetItemsClick() {
owner = document.getElementById('owner').value;
repo = document.getElementById('repo').value;
const token = document.getElementById('token').value;
if (!owner || !repo) {
alert('Please enter GitHub username and repository name.');
return;
}
globalToken = token;
checkRepoPrivacy();
}
async function checkRepoPrivacy() {
try {
const headers = {
Accept: 'application/vnd.github.v3+json',
};
if (globalToken) {
headers.Authorization = `token ${globalToken}`;
}
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repo}`,
{ headers },
);
isPrivateRepo = response.data.private;
getItems(1);
} catch (error) {
handleError(error);
}
}
async function getItems(page) {
const itemsList = document.getElementById('itemsList');
currentPage = page;
itemsList.innerHTML = 'Loading...';
try {
const headers = {
Accept: 'application/vnd.github.v3+json',
};
if (globalToken) {
headers.Authorization = `token ${globalToken}`;
}
const endpoint =
currentView === 'artifacts'
? `https://api.github.com/repos/${owner}/${repo}/actions/artifacts`
: `https://api.github.com/repos/${owner}/${repo}/actions/caches`;
const response = await axios.get(endpoint, {
headers: headers,
params: {
per_page: perPage,
page: page,
},
});
allItems =
currentView === 'artifacts'
? response.data.artifacts
: response.data.actions_caches;
totalPages = Math.ceil(response.data.total_count / perPage);
workflows.clear();
allItems.forEach((item) =>
workflows.add(
currentView === 'artifacts' ? item.workflow_run.head_branch : item.ref,
),
);
updateWorkflowFilter();
displayItems();
} catch (error) {
handleError(error);
}
}
function displayItems() {
const itemsList = document.getElementById('itemsList');
itemsList.innerHTML = allItems.map((item) => createItemHTML(item)).join('');
updatePagination();
}
function createItemHTML(item) {
const isArtifact = currentView === 'artifacts';
const deleteButtonDisabled = !isPrivateRepo ? 'disabled' : '';
const deleteButtonTitle = !isPrivateRepo
? 'Delete is only available for private repositories'
: '';
return `
<li>
<div class="item-header">
<h3 class="item-name">${isArtifact ? item.name : item.key}</h3>
<span class="item-id">ID: ${item.id}</span>
</div>
<div class="item-info">
<span class="item-date">${
isArtifact ? 'Created' : 'Last accessed'
}: ${new Date(
isArtifact ? item.created_at : item.last_accessed_at,
).toLocaleString()}</span>
<span class="item-size">Size: ${(item.size_in_bytes / 1024).toFixed(
2,
)} KB</span>
<span class="item-workflow">${
isArtifact ? 'Workflow Branch' : 'Branch'
}: ${isArtifact ? item.workflow_run.head_branch : item.ref}</span>
</div>
<div class="item-actions">
${
isArtifact
? `
<button class="download-link" onclick="downloadArtifact(${item.id}, this)">
<span class="button-text">Download</span>
<span class="loading-indicator"></span>
</button>
`
: ''
}
<button class="delete-link" onclick="${
isArtifact
? `deleteArtifact(${item.id}, this)`
: `deleteCache('${item.key}', this)`
}" ${deleteButtonDisabled} title="${deleteButtonTitle}">
<span class="button-text">Delete</span>
<span class="loading-indicator"></span>
</button>
</div>
</li>
`;
}
function updatePagination() {
const prevButton = document.getElementById('prevButton');
const nextButton = document.getElementById('nextButton');
const pageInfo = document.getElementById('pageInfo');
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
prevButton.onclick = () => getItems(currentPage - 1);
nextButton.onclick = () => getItems(currentPage + 1);
}
function updateWorkflowFilter() {
const workflowFilter = document.getElementById('workflowFilter');
workflowFilter.innerHTML = '<option value="">All Workflows/Branches</option>';
workflows.forEach((workflow) => {
const option = document.createElement('option');
option.value = workflow;
option.textContent = workflow;
workflowFilter.appendChild(option);
});
}
function applyFilters() {
currentPage = 1;
getItems(currentPage);
}
async function downloadArtifact(artifactId, button) {
try {
button.classList.add('loading');
const headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${globalToken}`,
};
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repo}/actions/artifacts/${artifactId}/zip`,
{
headers: headers,
responseType: 'blob',
},
);
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `artifact-${artifactId}.zip`);
document.body.appendChild(link);
link.click();
link.remove();
} catch (error) {
handleError(error);
} finally {
button.classList.remove('loading');
}
}
async function deleteArtifact(artifactId, button) {
if (!confirm('Are you sure you want to delete this artifact?')) {
return;
}
try {
button.classList.add('loading');
const headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${globalToken}`,
};
await axios.delete(
`https://api.github.com/repos/${owner}/${repo}/actions/artifacts/${artifactId}`,
{ headers: headers },
);
allItems = allItems.filter((item) => item.id !== artifactId);
filteredItems = filteredItems.filter((item) => item.id !== artifactId);
displayItems();
alert('Artifact deleted successfully.');
} catch (error) {
handleError(error);
} finally {
button.classList.remove('loading');
}
}
async function deleteCache(cacheKey, button) {
if (!confirm('Are you sure you want to delete this cache?')) {
return;
}
try {
button.classList.add('loading');
const headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${globalToken}`,
};
await axios.delete(
`https://api.github.com/repos/${owner}/${repo}/actions/caches`,
{
headers: headers,
params: { key: cacheKey },
},
);
allItems = allItems.filter((item) => item.key !== cacheKey);
filteredItems = filteredItems.filter((item) => item.key !== cacheKey);
displayItems();
alert('Cache deleted successfully.');
} catch (error) {
handleError(error);
} finally {
button.classList.remove('loading');
}
}
function handleError(error) {
const itemsList = document.getElementById('itemsList');
if (error.response && error.response.status === 404) {
itemsList.innerHTML =
"Repository not found or you don't have access. If it's a private repository, please provide a valid access token.";
} else if (error.response && error.response.status === 403) {
itemsList.innerHTML =
"You don't have permission to perform this action. If this is a private repository, make sure to provide a valid access token with the necessary permissions.";
} else {
itemsList.innerHTML = `Error: ${
error.response ? error.response.data.message : error.message
}`;
}
}
// Initialize the view
setView('artifacts');
// Add event listeners
document.addEventListener('DOMContentLoaded', () => {
document
.getElementById('getItemsButton')
.addEventListener('click', handleGetItemsClick);
});