From 8cd81ff48d6e0bb4e6c5a7023ea00938baf4dbac Mon Sep 17 00:00:00 2001 From: ciat-777 Date: Sun, 15 Mar 2026 13:39:19 +0800 Subject: [PATCH] feat: enhance PR label backfilling with size validation and warning tracking --- .github/workflows/backfill-pr-labels.yml | 56 ++++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/.github/workflows/backfill-pr-labels.yml b/.github/workflows/backfill-pr-labels.yml index d499772..f8e1a80 100644 --- a/.github/workflows/backfill-pr-labels.yml +++ b/.github/workflows/backfill-pr-labels.yml @@ -85,6 +85,7 @@ jobs: let inspected = 0; let updated = 0; let skipped = 0; + let warned = 0; for (const pull of pulls) { if (inspected >= limit) { @@ -93,6 +94,12 @@ jobs: inspected += 1; + const { data: fullPull } = await github.rest.pulls.get({ + owner, + repo, + pull_number: pull.number, + }); + const files = await github.paginate(github.rest.pulls.listFiles, { owner, repo, @@ -101,38 +108,50 @@ jobs: }); const desiredAreaLabels = computeAreaLabels(files); - const desiredSizeLabel = computeSizeLabel(pull.additions + pull.deletions); + const totalChanges = fullPull.additions + fullPull.deletions; + const hasValidSizeData = Number.isFinite(totalChanges) && totalChanges >= 0; const currentLabels = pull.labels.map((label) => label.name); const missingAreaLabels = desiredAreaLabels.filter((label) => !currentLabels.includes(label)); const currentSizeLabels = currentLabels.filter((label) => sizeLabels.includes(label)); - const sizeMatches = currentSizeLabels.length === 1 && currentSizeLabels[0] === desiredSizeLabel; + const desiredSizeLabel = hasValidSizeData ? computeSizeLabel(totalChanges) : null; + const sizeMatches = + hasValidSizeData && + currentSizeLabels.length === 1 && + currentSizeLabels[0] === desiredSizeLabel; - if (missingAreaLabels.length === 0 && sizeMatches) { + if (!hasValidSizeData) { + warned += 1; + core.warning(`Skipping size label for PR #${pull.number}: invalid additions/deletions values.`); + } + + if (missingAreaLabels.length === 0 && (hasValidSizeData ? sizeMatches : currentSizeLabels.length === 0)) { skipped += 1; continue; } - for (const label of currentSizeLabels) { - if (label === desiredSizeLabel) { - continue; - } + if (hasValidSizeData) { + for (const label of currentSizeLabels) { + if (label === desiredSizeLabel) { + continue; + } - try { - await github.rest.issues.removeLabel({ - owner, - repo, - issue_number: pull.number, - name: label, - }); - } catch (error) { - if (error.status !== 404) { - throw error; + try { + await github.rest.issues.removeLabel({ + owner, + repo, + issue_number: pull.number, + name: label, + }); + } catch (error) { + if (error.status !== 404) { + throw error; + } } } } const labelsToAdd = [...missingAreaLabels]; - if (!sizeMatches) { + if (hasValidSizeData && !sizeMatches) { labelsToAdd.push(desiredSizeLabel); } @@ -153,4 +172,5 @@ jobs: .addRaw(`Inspected PRs: ${inspected}\n`) .addRaw(`Updated PRs: ${updated}\n`) .addRaw(`Skipped PRs: ${skipped}\n`) + .addRaw(`Warnings: ${warned}\n`) .write();