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(), }