From 584fe806d9ebe211e8c959e8396cf8d178b5dd8f Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Wed, 15 Jul 2026 09:24:27 +0000 Subject: [PATCH] Add commits-behind count to update status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calls Forgejo compare API when an update is available to get the exact number of commits between deployed and latest — exposed as commitsBehind in the status payload. Degrades gracefully to null if the compare call fails. Co-Authored-By: Claude Sonnet 4.6 --- updater/src/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/updater/src/index.js b/updater/src/index.js index 8fbaaaa..7e23582 100644 --- a/updater/src/index.js +++ b/updater/src/index.js @@ -162,6 +162,19 @@ async function getForgejoCommit(repo) { } } +async function getCommitsBehind(repo, baseSha, headSha) { + try { + const headers = { Accept: 'application/json' } + if (FORGEJO_TOKEN) headers['Authorization'] = `token ${FORGEJO_TOKEN}` + const res = await fetch(`${FORGEJO_URL}/api/v1/repos/${FORGEJO_ORG}/${repo}/compare/${baseSha}...${headSha}`, { headers }) + if (!res.ok) return null + const data = await res.json() + return typeof data.total_commits === 'number' ? data.total_commits : null + } catch { + return null + } +} + async function refreshStatus(repo) { const target = getDeployTarget(repo) if (!target) return null @@ -178,6 +191,8 @@ async function refreshStatus(repo) { getForgejoCommit(repo), ]) const deployed = !!currentSha + const updateAvailable = !!(deployed && latest && currentSha && latest.sha !== currentSha) + const commitsBehind = updateAvailable ? await getCommitsBehind(repo, currentSha, latest.sha) : null const entry = { repo, host: target.host, @@ -185,7 +200,8 @@ async function refreshStatus(repo) { currentCommitAt: currentTimestamp || null, latestCommit: latest ? latest.sha.slice(0, 12) : 'unknown', latestCommitAt: latest?.timestamp || null, - updateAvailable: !!(deployed && latest && currentSha && latest.sha !== currentSha), + updateAvailable, + commitsBehind, deployed, checkedAt: new Date().toISOString(), }