gitea show WIP PR counter
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -7,12 +7,25 @@ class GiteaPRAction extends ActionBase {
|
|||||||
filter: 'review_requested',
|
filter: 'review_requested',
|
||||||
refreshRate: '4',
|
refreshRate: '4',
|
||||||
};
|
};
|
||||||
|
this.firstSeen = {};
|
||||||
|
this.settingsLoaded = false;
|
||||||
|
|
||||||
|
this.$UD.onDidReceiveSettings(jsn => {
|
||||||
|
if (jsn.context !== this.context) return;
|
||||||
|
const s = jsn.settings || {};
|
||||||
|
if (s.firstSeen) {
|
||||||
|
// Persisted timestamps win over in-memory ones set during race window
|
||||||
|
this.firstSeen = Object.assign({}, this.firstSeen, s.firstSeen);
|
||||||
|
}
|
||||||
|
this.settingsLoaded = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setActive() {}
|
setActive() {}
|
||||||
|
|
||||||
setParams(jsn) {
|
setParams(jsn) {
|
||||||
this.config = Object.assign(this.config, (jsn && jsn.param) || {});
|
this.config = Object.assign(this.config, (jsn && jsn.param) || {});
|
||||||
|
this.$UD.getSettings(this.context);
|
||||||
this.startTimer(() => this.fetchPRs(), this.config.refreshRate);
|
this.startTimer(() => this.fetchPRs(), this.config.refreshRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +46,7 @@ class GiteaPRAction extends ActionBase {
|
|||||||
const filter = this.config.filter || 'review_requested';
|
const filter = this.config.filter || 'review_requested';
|
||||||
const base = `${this.config.url}/api/v1/repos/issues/search?type=pullrequest&state=open&limit=50`;
|
const base = `${this.config.url}/api/v1/repos/issues/search?type=pullrequest&state=open&limit=50`;
|
||||||
|
|
||||||
const fetchIds = async (url) => {
|
const fetchPRs = async (url) => {
|
||||||
const r = await fetch(url, { headers, signal: AbortSignal.timeout(8000) });
|
const r = await fetch(url, { headers, signal: AbortSignal.timeout(8000) });
|
||||||
console.log('GiteaPR status:', r.status, url);
|
console.log('GiteaPR status:', r.status, url);
|
||||||
if (!r.ok) {
|
if (!r.ok) {
|
||||||
@@ -42,24 +55,44 @@ class GiteaPRAction extends ActionBase {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const data = await r.json();
|
const data = await r.json();
|
||||||
return Array.isArray(data) ? data.map(i => i.id) : [];
|
return Array.isArray(data) ? data : [];
|
||||||
};
|
};
|
||||||
|
|
||||||
let ids;
|
let prs;
|
||||||
if (filter === 'both') {
|
if (filter === 'both') {
|
||||||
const [a, b] = await Promise.all([
|
const [a, b] = await Promise.all([
|
||||||
fetchIds(`${base}&assigned=true`),
|
fetchPRs(`${base}&assigned=true`),
|
||||||
fetchIds(`${base}&review_requested=true`),
|
fetchPRs(`${base}&review_requested=true`),
|
||||||
]);
|
]);
|
||||||
if (a === null || b === null) { this.renderButton(null, true); return; }
|
if (a === null || b === null) { this.renderButton(null, true); return; }
|
||||||
ids = [...new Set([...a, ...b])];
|
const seen = new Set();
|
||||||
|
prs = [...a, ...b].filter(pr => seen.has(pr.id) ? false : seen.add(pr.id));
|
||||||
} else {
|
} else {
|
||||||
const param = filter === 'assigned' ? '&assigned=true' : '&review_requested=true';
|
const param = filter === 'assigned' ? '&assigned=true' : '&review_requested=true';
|
||||||
ids = await fetchIds(`${base}${param}`);
|
prs = await fetchPRs(`${base}${param}`);
|
||||||
if (ids === null) { this.renderButton(null, true); return; }
|
if (prs === null) { this.renderButton(null, true); return; }
|
||||||
}
|
}
|
||||||
|
|
||||||
this.renderButton(ids.length, false);
|
const wip = prs.filter(pr => pr.title?.startsWith('WIP: ')).length;
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
// Update first-seen map: add new, remove gone
|
||||||
|
const currentIds = new Set(prs.map(pr => String(pr.id)));
|
||||||
|
for (const pr of prs) {
|
||||||
|
if (!(String(pr.id) in this.firstSeen)) this.firstSeen[String(pr.id)] = now;
|
||||||
|
}
|
||||||
|
for (const id of Object.keys(this.firstSeen)) {
|
||||||
|
if (!currentIds.has(id)) delete this.firstSeen[id];
|
||||||
|
}
|
||||||
|
if (this.settingsLoaded) {
|
||||||
|
this.$UD.setSettings({ ...this.config, firstSeen: this.firstSeen }, this.context);
|
||||||
|
}
|
||||||
|
|
||||||
|
const overdue = prs.filter(pr =>
|
||||||
|
!pr.title?.startsWith('WIP: ') &&
|
||||||
|
(now - (this.firstSeen[String(pr.id)] ?? now)) > 24 * 60 * 60 * 1000
|
||||||
|
).length;
|
||||||
|
this.renderButton(prs.length, false, wip, overdue);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// timeout or network error = VPN likely down
|
// timeout or network error = VPN likely down
|
||||||
this.renderButton(null, false);
|
this.renderButton(null, false);
|
||||||
@@ -67,7 +100,7 @@ class GiteaPRAction extends ActionBase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
renderButton(count, isError) {
|
renderButton(count, isError, wip = 0, overdue = 0) {
|
||||||
const { canvas, ctx } = this.createCanvas();
|
const { canvas, ctx } = this.createCanvas();
|
||||||
|
|
||||||
// Label — top
|
// Label — top
|
||||||
@@ -76,35 +109,65 @@ class GiteaPRAction extends ActionBase {
|
|||||||
ctx.fillText('Pull Requests', 98, 30);
|
ctx.fillText('Pull Requests', 98, 30);
|
||||||
|
|
||||||
if (count === null) {
|
if (count === null) {
|
||||||
// No connection — show plug/disconnected icon
|
|
||||||
ctx.fillStyle = isError ? '#ff6b6b' : '#888888';
|
ctx.fillStyle = isError ? '#ff6b6b' : '#888888';
|
||||||
ctx.font = '70px "Source Han Sans SC"';
|
ctx.font = '70px "Source Han Sans SC"';
|
||||||
ctx.textBaseline = 'alphabetic';
|
ctx.textBaseline = 'alphabetic';
|
||||||
const bm = ctx.measureText('⚡');
|
const bm = ctx.measureText('⚡');
|
||||||
ctx.fillText('⚡', 98, 98 + (bm.actualBoundingBoxAscent - bm.actualBoundingBoxDescent) / 2);
|
ctx.fillText('⚡', 98, 98 + (bm.actualBoundingBoxAscent - bm.actualBoundingBoxDescent) / 2);
|
||||||
ctx.textBaseline = 'middle';
|
ctx.textBaseline = 'middle';
|
||||||
|
|
||||||
ctx.fillStyle = isError ? '#ff6b6b' : '#888888';
|
ctx.fillStyle = isError ? '#ff6b6b' : '#888888';
|
||||||
ctx.font = 'bold 22px "Source Han Sans SC"';
|
ctx.font = 'bold 22px "Source Han Sans SC"';
|
||||||
ctx.fillText(isError ? 'API Error' : 'Offline', 98, 166);
|
ctx.fillText(isError ? 'API Error' : 'Offline', 98, 166);
|
||||||
} else {
|
} else {
|
||||||
// Count — large center number (visually centered using actual bounding box)
|
|
||||||
const color = count === 0 ? '#6bff6b' : count < 5 ? '#f0c040' : '#ff6b6b';
|
|
||||||
ctx.fillStyle = color;
|
|
||||||
const fSize = count > 99 ? 60 : count > 9 ? 80 : 96;
|
|
||||||
ctx.font = `bold ${fSize}px "Source Han Sans SC"`;
|
|
||||||
const text = count > 999 ? '999+' : String(count);
|
|
||||||
ctx.textBaseline = 'alphabetic';
|
|
||||||
const m = ctx.measureText(text);
|
|
||||||
ctx.fillText(text, 98, 98 + (m.actualBoundingBoxAscent - m.actualBoundingBoxDescent) / 2);
|
|
||||||
ctx.textBaseline = 'middle';
|
|
||||||
|
|
||||||
// Subtext
|
|
||||||
const filter = this.config.filter || 'review_requested';
|
const filter = this.config.filter || 'review_requested';
|
||||||
const label = filter === 'assigned' ? 'assigned' : filter === 'review_requested' ? 'review req.' : 'open';
|
const label = filter === 'assigned' ? 'assigned' : filter === 'review_requested' ? 'review req.' : 'open';
|
||||||
ctx.fillStyle = '#888888';
|
const color = count === 0 ? '#6bff6b' : count < 5 ? '#f0c040' : '#ff6b6b';
|
||||||
ctx.font = '18px "Source Han Sans SC"';
|
const text = count > 999 ? '999+' : String(count);
|
||||||
ctx.fillText(label, 98, 166);
|
|
||||||
|
const drawCount = (centerY, fSizes) => {
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
const fSize = count > 99 ? fSizes[0] : count > 9 ? fSizes[1] : fSizes[2];
|
||||||
|
ctx.font = `bold ${fSize}px "Source Han Sans SC"`;
|
||||||
|
ctx.textBaseline = 'alphabetic';
|
||||||
|
const m = ctx.measureText(text);
|
||||||
|
ctx.fillText(text, 98, centerY + (m.actualBoundingBoxAscent - m.actualBoundingBoxDescent) / 2);
|
||||||
|
ctx.textBaseline = 'middle';
|
||||||
|
};
|
||||||
|
|
||||||
|
if (wip > 0 && overdue > 0) {
|
||||||
|
// Most compact: 4 items
|
||||||
|
drawCount(75, [44, 56, 68]);
|
||||||
|
ctx.fillStyle = '#555555';
|
||||||
|
ctx.font = '15px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(label, 98, 120);
|
||||||
|
ctx.fillStyle = '#ff8c00';
|
||||||
|
ctx.font = 'bold 26px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(`WIP: ${wip}`, 98, 147);
|
||||||
|
ctx.fillStyle = '#ff3333';
|
||||||
|
ctx.font = 'bold 24px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(`>24h: ${overdue}`, 98, 175);
|
||||||
|
} else if (wip > 0) {
|
||||||
|
drawCount(85, [48, 64, 76]);
|
||||||
|
ctx.fillStyle = '#666666';
|
||||||
|
ctx.font = '16px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(label, 98, 140);
|
||||||
|
ctx.fillStyle = '#ff8c00';
|
||||||
|
ctx.font = 'bold 30px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(`WIP: ${wip}`, 98, 168);
|
||||||
|
} else if (overdue > 0) {
|
||||||
|
drawCount(88, [52, 68, 80]);
|
||||||
|
ctx.fillStyle = '#666666';
|
||||||
|
ctx.font = '16px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(label, 98, 142);
|
||||||
|
ctx.fillStyle = '#ff3333';
|
||||||
|
ctx.font = 'bold 28px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(`>24h: ${overdue}`, 98, 170);
|
||||||
|
} else {
|
||||||
|
drawCount(98, [60, 80, 96]);
|
||||||
|
ctx.fillStyle = '#888888';
|
||||||
|
ctx.font = '18px "Source Han Sans SC"';
|
||||||
|
ctx.fillText(label, 98, 166);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setIcon(canvas);
|
this.setIcon(canvas);
|
||||||
|
|||||||
Reference in New Issue
Block a user