diff --git a/plugin/actions/GiteaAction.js b/plugin/actions/GiteaAction.js index 377f8a4..fd65044 100644 --- a/plugin/actions/GiteaAction.js +++ b/plugin/actions/GiteaAction.js @@ -23,32 +23,54 @@ class GiteaAction extends ActionBase { } } + semverAtLeast(version, major, minor) { + const parts = version.replace(/[^0-9.]/g, '').split('.').map(Number); + if (parts[0] !== major) return parts[0] > major; + return (parts[1] || 0) >= minor; + } + fetchRun() { this.debounce(async () => { if (!this.config.url || !this.config.owner || !this.config.repo) { - this.renderButton(null); + this.renderButton(null, false); return; } try { const headers = this.config.token ? { Authorization: `token ${this.config.token}` } : {}; + + const versionRes = await fetch(`${this.config.url}/api/v1/version`, { headers }); + if (!versionRes.ok) { + this.renderButton({ error: versionRes.status }, false); + return; + } + const { version } = await versionRes.json(); + + if (!this.semverAtLeast(version, 1, 23)) { + this.renderButton(null, true); + return; + } + const response = await fetch( - `${this.config.url}/api/v1/repos/${this.config.owner}/${this.config.repo}/actions/runs?limit=1`, + `${this.config.url}/api/v1/repos/${this.config.owner}/${this.config.repo}/actions/runs?limit=20`, { headers } ); if (!response.ok) { - this.renderButton({ error: response.status }); + this.renderButton({ error: response.status }, false); return; } + const data = await response.json(); - const run = data.workflow_runs && data.workflow_runs[0]; - this.renderButton(run || null); + const runs = data.workflow_runs || []; + const activeStatuses = ['in_progress', 'queued', 'waiting', 'requested', 'action_required']; + const runningRun = runs.find(r => activeStatuses.includes(r.status)); + this.renderButton(runningRun || runs[0] || null, false); } catch (e) { - this.renderButton({ error: 'ERR' }); + this.renderButton({ error: 'ERR' }, false); } }); } - renderButton(run) { + renderButton(run, unsupported) { const { canvas, ctx } = this.createCanvas(); // Owner — top @@ -65,22 +87,25 @@ class GiteaAction extends ActionBase { // Status symbol — center let symbol, color; - if (!run) { + if (unsupported) { + symbol = '⊘'; + color = '#555555'; + } else if (!run) { symbol = '?'; color = '#666666'; } else if (run.error) { symbol = '✗'; color = '#ff6b6b'; - } else if (run.status === 'running' || run.status === 'waiting') { + } else if (['in_progress', 'queued', 'waiting', 'requested', 'action_required'].includes(run.status)) { symbol = '⟳'; color = '#f0c040'; - } else if (run.conclusion === 'success') { + } else if (run.conclusion === 'success' || run.status === 'success') { symbol = '✓'; color = '#6bff6b'; - } else if (run.conclusion === 'failure') { + } else if (run.conclusion === 'failure' || run.status === 'failure') { symbol = '✗'; color = '#ff6b6b'; - } else if (run.conclusion === 'cancelled') { + } else if (['cancelled', 'skipped', 'timed_out', 'neutral'].includes(run.conclusion || run.status)) { symbol = '⊘'; color = '#aaaaaa'; } else {