4747// FURTHER release files a new issue and leaves a supersession comment on the
4848// older open one; it never closes it.
4949//
50+ // An OPEN issue for the current target is kept current (#5067): while the
51+ // target stays put, a server can upgrade on its own, be added, or move
52+ // between groups (the v1 -> v2 migration), and the issue would otherwise
53+ // keep pointing at servers with no work left. Its body is re-rendered and
54+ // rewritten when it differs, as the monthly refresh does, except that a
55+ // ticked checklist box alone is not a difference. Its scope labels move by
56+ // the DELTA between what the old body's rows called for and what the new
57+ // rows do, so a label triage or a maintainer applied, or removed, stays as
58+ // they left it. A closed issue is left alone.
59+ //
5060// "An issue exists" and "it was analyzed" are different claims. The workflow
5161// stamps `ANALYSIS_MARKER` on its comment, and an OPEN issue for the current
5262// target with no such comment from the automation is handed to the analysis
@@ -270,6 +280,20 @@ export function buildIssueTitle(state) {
270280 return `chore(deps): upgrade the ${ state . group . label } to ${ state . target } ` ;
271281}
272282
283+ /**
284+ * The scope labels for the places a copy is behind, or none when that is
285+ * every server (the issue is then repository-wide).
286+ *
287+ * @param {string[] } wheres each behind row's `where`
288+ * @returns {string[] }
289+ */
290+ function scopesFor ( wheres ) {
291+ const scopes = [
292+ ...new Set ( wheres . map ( ( w ) => scopeLabel ( `${ w } /` ) ) . filter ( Boolean ) ) ,
293+ ] . sort ( ) ;
294+ return scopes . length === SERVERS . length ? [ ] : scopes ;
295+ }
296+
273297/**
274298 * Labels: the sweep's, plus each server with a copy that is BEHIND (a server
275299 * already at the target has no work here), unless that is every server (then
@@ -278,17 +302,55 @@ export function buildIssueTitle(state) {
278302 * @param {NonNullable<ReturnType<typeof groupState>> } state
279303 */
280304export function issueLabels ( state ) {
281- const scopes = [
282- ...new Set (
283- state . rows
284- . filter ( ( r ) => r . behind )
285- . map ( ( r ) => scopeLabel ( `${ r . where } /` ) )
286- . filter ( Boolean ) ,
287- ) ,
288- ] . sort ( ) ;
289- return scopes . length === SERVERS . length
290- ? [ ...SWEEP_LABELS ]
291- : [ ...SWEEP_LABELS , ...scopes ] ;
305+ return [
306+ ...SWEEP_LABELS ,
307+ ...scopesFor ( state . rows . filter ( ( r ) => r . behind ) . map ( ( r ) => r . where ) ) ,
308+ ] ;
309+ }
310+
311+ /** A row of the table `buildIssueBody` writes: its `where`, and `Behind`. */
312+ const ROW_RE = / ^ \| ` .* ?` \| ` ( [ ^ ` ] * ) ` \| .* \| ( \* \* y e s \* \* | n o ) \| $ / gm;
313+
314+ /**
315+ * The scope labels an issue body's rows called for when the sweep wrote it,
316+ * read back from its table.
317+ *
318+ * @param {string | undefined } body
319+ * @returns {string[] }
320+ */
321+ export function bodyScopes ( body ) {
322+ return scopesFor (
323+ [ ...( body ?? "" ) . matchAll ( ROW_RE ) ]
324+ . filter ( ( m ) => m [ 2 ] === "**yes**" )
325+ . map ( ( m ) => m [ 1 ] ) ,
326+ ) ;
327+ }
328+
329+ /** A body with every checklist box unticked, so ticking one is not a change. */
330+ const unticked = ( body ) => ( body ?? "" ) . replace ( / ^ - \[ [ x X ] \] / gm, "- [ ]" ) ;
331+
332+ /**
333+ * What an OPEN issue for the current target needs to match the current
334+ * state, or `null` when it already does. Labels move only by the delta
335+ * between the old body's rows and the new ones, so labels the sweep did not
336+ * apply (or that someone removed) are left as they are.
337+ *
338+ * @param {{body?: string, labels?: Array<{name?: string}>} } issue
339+ * @param {NonNullable<ReturnType<typeof groupState>> } state
340+ * @returns {{body: string, addLabels: string[], removeLabels: string[]} | null }
341+ */
342+ export function refreshPlan ( issue , state ) {
343+ const body = buildIssueBody ( state ) ;
344+ // Labels follow the rows, so an unchanged body means unchanged labels.
345+ if ( unticked ( issue . body ) === unticked ( body ) ) return null ;
346+ const had = new Set ( ( issue . labels ?? [ ] ) . map ( ( l ) => l ?. name ) ) ;
347+ const before = bodyScopes ( issue . body ) ;
348+ const after = bodyScopes ( body ) ;
349+ return {
350+ body,
351+ addLabels : after . filter ( ( l ) => ! before . includes ( l ) && ! had . has ( l ) ) ,
352+ removeLabels : before . filter ( ( l ) => ! after . includes ( l ) && had . has ( l ) ) ,
353+ } ;
292354}
293355
294356/**
@@ -643,6 +705,19 @@ export function main({
643705 // OPEN only: a closed issue keeps suppressing its target, and must not
644706 // be handed a fresh analysis every night.
645707 if ( match && match . state === "OPEN" ) {
708+ const refresh = refreshPlan ( match , state ) ;
709+ if ( refresh ) {
710+ writer . edit ( number , refresh ) ;
711+ log (
712+ `${ SWEEP } : refreshed #${ number } for what is installed now` +
713+ ( refresh . addLabels . length
714+ ? `; added ${ refresh . addLabels . join ( ", " ) } `
715+ : "" ) +
716+ ( refresh . removeLabels . length
717+ ? `; removed ${ refresh . removeLabels . join ( ", " ) } `
718+ : "" ) ,
719+ ) ;
720+ }
646721 if ( hasAnalysis ( issueComments ( repo , number , spawn ) ) ) {
647722 log (
648723 `${ SWEEP } : ${ state . group . label } ${ state . target } has an issue and an analysis; no-op` ,
0 commit comments