|
| 1 | +// SPDX-FileCopyrightText: 2026 The Linux Foundation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +/* global ACTION_GATE_API_URL */ |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const API_BASE = |
| 9 | + (typeof window !== "undefined" && window.ACTION_GATE_API_URL) || window.location.origin; |
| 10 | + |
| 11 | +function $(id) { return document.getElementById(id); } |
| 12 | + |
| 13 | +function escapeHtml(s) { |
| 14 | + const d = document.createElement("div"); |
| 15 | + d.textContent = s; |
| 16 | + return d.innerHTML; |
| 17 | +} |
| 18 | + |
| 19 | +// ── State ───────────────────────────────────────────────────────────────────── |
| 20 | + |
| 21 | +const state = { page: 1, perPage: 30, total: 0, totalPages: 1 }; |
| 22 | + |
| 23 | +// ── API helper ──────────────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +async function apiFetch(path) { |
| 26 | + const res = await fetch(`${API_BASE}${path}`); |
| 27 | + if (!res.ok) throw new Error(`API ${res.status}`); |
| 28 | + return res.json(); |
| 29 | +} |
| 30 | + |
| 31 | +// ── Rendering ───────────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +function renderRepos(repos) { |
| 34 | + const tbody = $("repos-body"); |
| 35 | + if (!repos.length) { |
| 36 | + tbody.innerHTML = `<tr><td colspan="5" class="empty-state">No repositories found.</td></tr>`; |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + tbody.innerHTML = repos |
| 41 | + .map((r) => { |
| 42 | + const fullName = `${escapeHtml(r.owner)}/${escapeHtml(r.name)}`; |
| 43 | + const ghLink = `https://github.com/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.name)}`; |
| 44 | + const activeCount = r._count?.attestations ?? 0; |
| 45 | + const mode = r.mode === "BLOCK" ? "Block" : r.mode === "WARN" ? "Warn" : escapeHtml(r.mode); |
| 46 | + const modeClass = r.mode === "BLOCK" ? "badge badge-danger" : "badge badge-warn"; |
| 47 | + const added = new Date(r.createdAt).toLocaleDateString(); |
| 48 | + return `<tr> |
| 49 | + <td><a href="${escapeHtml(ghLink)}" target="_blank" rel="noopener">${fullName}</a></td> |
| 50 | + <td><span class="${modeClass}">${mode}</span></td> |
| 51 | + <td>${activeCount}</td> |
| 52 | + <td>${r.expiryDays ?? "—"}</td> |
| 53 | + <td>${added}</td> |
| 54 | + </tr>`; |
| 55 | + }) |
| 56 | + .join(""); |
| 57 | +} |
| 58 | + |
| 59 | +function updatePagination() { |
| 60 | + $("page-info").textContent = `Page ${state.page} of ${state.totalPages}`; |
| 61 | + $("btn-prev").disabled = state.page <= 1; |
| 62 | + $("btn-next").disabled = state.page >= state.totalPages; |
| 63 | + $("pagination").hidden = state.totalPages <= 1; |
| 64 | +} |
| 65 | + |
| 66 | +// ── Data fetching ───────────────────────────────────────────────────────────── |
| 67 | + |
| 68 | +async function loadRepos() { |
| 69 | + $("loading").hidden = false; |
| 70 | + $("error-msg").hidden = true; |
| 71 | + $("table-wrapper").hidden = true; |
| 72 | + |
| 73 | + try { |
| 74 | + const data = await apiFetch( |
| 75 | + `/api/v1/repositories?page=${state.page}&per_page=${state.perPage}` |
| 76 | + ); |
| 77 | + state.total = data.total ?? 0; |
| 78 | + state.totalPages = Math.max(1, Math.ceil(state.total / state.perPage)); |
| 79 | + |
| 80 | + renderRepos(data.repositories ?? []); |
| 81 | + updatePagination(); |
| 82 | + |
| 83 | + $("loading").hidden = true; |
| 84 | + $("table-wrapper").hidden = false; |
| 85 | + } catch (err) { |
| 86 | + $("loading").hidden = true; |
| 87 | + $("error-msg").textContent = `Failed to load repositories: ${err.message}`; |
| 88 | + $("error-msg").hidden = false; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// ── Auth (display only — no login required for this page) ───────────────────── |
| 93 | + |
| 94 | +function getToken() { |
| 95 | + return localStorage.getItem("gh_token"); |
| 96 | +} |
| 97 | + |
| 98 | +async function checkAuth() { |
| 99 | + const token = getToken(); |
| 100 | + if (!token) return; |
| 101 | + try { |
| 102 | + const res = await fetch("https://api.github.com/user", { |
| 103 | + headers: { Authorization: `Bearer ${token}` }, |
| 104 | + }); |
| 105 | + if (!res.ok) return; |
| 106 | + const user = await res.json(); |
| 107 | + $("user-avatar").src = user.avatar_url; |
| 108 | + $("user-avatar").alt = `@${escapeHtml(user.login)}`; |
| 109 | + $("user-login").textContent = `@${user.login}`; |
| 110 | + $("user-info").hidden = false; |
| 111 | + } catch { /* ignore */ } |
| 112 | +} |
| 113 | + |
| 114 | +// ── Events ──────────────────────────────────────────────────────────────────── |
| 115 | + |
| 116 | +$("btn-prev").addEventListener("click", () => { |
| 117 | + if (state.page > 1) { state.page--; loadRepos(); } |
| 118 | +}); |
| 119 | +$("btn-next").addEventListener("click", () => { |
| 120 | + if (state.page < state.totalPages) { state.page++; loadRepos(); } |
| 121 | +}); |
| 122 | +$("btn-logout")?.addEventListener("click", () => { |
| 123 | + localStorage.removeItem("gh_token"); |
| 124 | + window.location.href = "index.html"; |
| 125 | +}); |
| 126 | + |
| 127 | +// ── Init ────────────────────────────────────────────────────────────────────── |
| 128 | + |
| 129 | +checkAuth(); |
| 130 | +loadRepos(); |
| 131 | + |
| 132 | +// ── Deploy SHA link (same pattern as main dashboard) ────────────────────────── |
| 133 | + |
| 134 | +(function deployShaLink() { |
| 135 | + const el = $("deploy-sha"); |
| 136 | + if (!el) return; |
| 137 | + const sha = el.textContent.trim(); |
| 138 | + if (!sha || sha === "__GIT_SHA__") { |
| 139 | + el.hidden = true; |
| 140 | + return; |
| 141 | + } |
| 142 | + const link = document.createElement("a"); |
| 143 | + link.href = `https://github.com/__GITHUB_REPO__/commit/${encodeURIComponent(sha)}`; |
| 144 | + link.target = "_blank"; |
| 145 | + link.rel = "noopener noreferrer"; |
| 146 | + link.textContent = sha; |
| 147 | + link.className = "deploy-sha"; |
| 148 | + el.replaceWith(link); |
| 149 | +})(); |
0 commit comments