|
54 | 54 | // rewritten when it differs, as the monthly refresh does, except that a |
55 | 55 | // ticked checklist box alone is not a difference. Its scope labels move by |
56 | 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. That holds when the last outdated copy catches up or |
| 57 | +// rows do, and a label is removed only if the sweep applied it (recorded on |
| 58 | +// the body's last line, since a row can call for a label someone had |
| 59 | +// already applied), so a label triage or a maintainer applied, or removed, |
| 60 | +// stays as they left it. That holds when the last outdated copy catches up or |
59 | 61 | // leaves the group too: the issue is rewritten to say nothing is behind, |
60 | 62 | // though the sweep still closes nothing. A closed issue is left alone. |
61 | 63 | // |
@@ -341,31 +343,66 @@ export function bodyScopes(body) { |
341 | 343 | ); |
342 | 344 | } |
343 | 345 |
|
| 346 | +/** The record, on a body's last line, of the scope labels the sweep applied. */ |
| 347 | +const APPLIED_RE = /^<!-- sdk-watch:scopes(?: ([^ ]+))? -->$/m; |
| 348 | + |
| 349 | +/** |
| 350 | + * @param {string[]} scopes |
| 351 | + * @returns {string} the record line for `scopes` |
| 352 | + */ |
| 353 | +export function buildAppliedMarker(scopes) { |
| 354 | + return `<!-- sdk-watch:scopes${scopes.length ? ` ${scopes.join(",")}` : ""} -->`; |
| 355 | +} |
| 356 | + |
| 357 | +/** |
| 358 | + * The scope labels the sweep itself applied to an issue. A row calling for |
| 359 | + * a label that was already there does not make it the sweep's, so this is |
| 360 | + * recorded rather than read off the rows. A body written before the record |
| 361 | + * existed falls back to its rows, which is what the sweep applied at filing. |
| 362 | + * |
| 363 | + * @param {string | undefined} body |
| 364 | + * @returns {string[]} |
| 365 | + */ |
| 366 | +export function appliedScopes(body) { |
| 367 | + const match = APPLIED_RE.exec(body ?? ""); |
| 368 | + if (!match) return bodyScopes(body); |
| 369 | + return match[1] ? match[1].split(",") : []; |
| 370 | +} |
| 371 | + |
344 | 372 | /** A body with every checklist box unticked, so ticking one is not a change. */ |
345 | 373 | const unticked = (body) => (body ?? "").replace(/^- \[[xX]\]/gm, "- [ ]"); |
346 | 374 |
|
347 | 375 | /** |
348 | 376 | * What an OPEN issue for the current target needs to match the current |
349 | 377 | * state, or `null` when it already does. Labels move only by the delta |
350 | | - * between the old body's rows and the new ones, so labels the sweep did not |
351 | | - * apply (or that someone removed) are left as they are. |
| 378 | + * between the old body's rows and the new ones: one is added only when the |
| 379 | + * rows newly call for it and it is absent, and removed only when the rows |
| 380 | + * stop calling for it and the sweep is the one that applied it. A label |
| 381 | + * triage or a maintainer applied, or removed, is left as they left it. |
352 | 382 | * |
353 | 383 | * @param {{body?: string, labels?: Array<{name?: string}>}} issue |
354 | 384 | * @param {NonNullable<ReturnType<typeof groupState>>} state |
355 | 385 | * @returns {{body: string, addLabels: string[], removeLabels: string[]} | null} |
356 | 386 | */ |
357 | 387 | export function refreshPlan(issue, state) { |
358 | | - const body = buildIssueBody(state); |
359 | | - // Labels follow the rows, so an unchanged body means unchanged labels. |
360 | | - if (unticked(issue.body) === unticked(body)) return null; |
361 | 388 | const had = new Set((issue.labels ?? []).map((l) => l?.name)); |
362 | 389 | const before = bodyScopes(issue.body); |
363 | | - const after = bodyScopes(body); |
364 | | - return { |
365 | | - body, |
366 | | - addLabels: after.filter((l) => !before.includes(l) && !had.has(l)), |
367 | | - removeLabels: before.filter((l) => !after.includes(l) && had.has(l)), |
368 | | - }; |
| 390 | + const after = bodyScopes(buildIssueBody(state)); |
| 391 | + const owned = appliedScopes(issue.body); |
| 392 | + const addLabels = after.filter((l) => !before.includes(l) && !had.has(l)); |
| 393 | + const removeLabels = before.filter( |
| 394 | + (l) => !after.includes(l) && had.has(l) && owned.includes(l), |
| 395 | + ); |
| 396 | + const applied = [ |
| 397 | + ...new Set([ |
| 398 | + ...owned.filter((l) => !removeLabels.includes(l)), |
| 399 | + ...addLabels, |
| 400 | + ]), |
| 401 | + ].sort(); |
| 402 | + const body = buildIssueBody(state, applied); |
| 403 | + // Labels follow the rows, so an unchanged body means unchanged labels. |
| 404 | + if (unticked(issue.body) === unticked(body)) return null; |
| 405 | + return { body, addLabels, removeLabels }; |
369 | 406 | } |
370 | 407 |
|
371 | 408 | /** |
@@ -410,8 +447,15 @@ export function checklist(state) { |
410 | 447 | ]; |
411 | 448 | } |
412 | 449 |
|
413 | | -/** @param {NonNullable<ReturnType<typeof groupState>>} state */ |
414 | | -export function buildIssueBody(state) { |
| 450 | +/** |
| 451 | + * @param {NonNullable<ReturnType<typeof groupState>>} state |
| 452 | + * @param {string[]} [applied] the scope labels the sweep has applied; at |
| 453 | + * filing, the ones `issueLabels` gives |
| 454 | + */ |
| 455 | +export function buildIssueBody( |
| 456 | + state, |
| 457 | + applied = issueLabels(state).filter((l) => !SWEEP_LABELS.includes(l)), |
| 458 | +) { |
415 | 459 | const { group, rows, target } = state; |
416 | 460 | const registry = group.ecosystem === "pypi" ? "PyPI" : "npm"; |
417 | 461 | const table = rows |
@@ -439,6 +483,7 @@ export function buildIssueBody(state) { |
439 | 483 | `Release notes: https://github.com/${group.repo}/releases`, |
440 | 484 | "", |
441 | 485 | "Rewritten by the nightly SDK watch (#4874) when the last outdated copy cleared. The sweep closes nothing: close this issue by hand once a maintainer has confirmed it.", |
| 486 | + buildAppliedMarker(applied), |
442 | 487 | ].join("\n"); |
443 | 488 | } |
444 | 489 |
|
@@ -467,6 +512,7 @@ export function buildIssueBody(state) { |
467 | 512 | "An automated review of what changed upstream, and which parts of these servers it touches, is posted as a comment below.", |
468 | 513 | "", |
469 | 514 | "A later run will not refile this issue. A **further** release files its own issue and leaves a supersession note here.", |
| 515 | + buildAppliedMarker(applied), |
470 | 516 | ].join("\n"); |
471 | 517 | } |
472 | 518 |
|
|
0 commit comments