Add commits-behind count to update status

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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-15 09:24:27 +00:00
parent 19bb4ec4d4
commit 584fe806d9

View file

@ -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) { async function refreshStatus(repo) {
const target = getDeployTarget(repo) const target = getDeployTarget(repo)
if (!target) return null if (!target) return null
@ -178,6 +191,8 @@ async function refreshStatus(repo) {
getForgejoCommit(repo), getForgejoCommit(repo),
]) ])
const deployed = !!currentSha const deployed = !!currentSha
const updateAvailable = !!(deployed && latest && currentSha && latest.sha !== currentSha)
const commitsBehind = updateAvailable ? await getCommitsBehind(repo, currentSha, latest.sha) : null
const entry = { const entry = {
repo, repo,
host: target.host, host: target.host,
@ -185,7 +200,8 @@ async function refreshStatus(repo) {
currentCommitAt: currentTimestamp || null, currentCommitAt: currentTimestamp || null,
latestCommit: latest ? latest.sha.slice(0, 12) : 'unknown', latestCommit: latest ? latest.sha.slice(0, 12) : 'unknown',
latestCommitAt: latest?.timestamp || null, latestCommitAt: latest?.timestamp || null,
updateAvailable: !!(deployed && latest && currentSha && latest.sha !== currentSha), updateAvailable,
commitsBehind,
deployed, deployed,
checkedAt: new Date().toISOString(), checkedAt: new Date().toISOString(),
} }