From 3aa8603bd1a4b28aaa318cfe98e195e60d477cc3 Mon Sep 17 00:00:00 2001 From: neveler <55753029+neveler@users.noreply.github.com> Date: Wed, 22 Jul 2026 22:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=20pr-size-label=20=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E6=B5=81=E4=B8=AD=E7=9A=84=20js=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=88=B0=E7=8B=AC=E7=AB=8B=E6=96=87=E4=BB=B6=20(#6458)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/scripts/pr-size-label.js | 105 ++++++++++++++++++++ .github/workflows/pr-size-label.yml | 148 +++------------------------- 2 files changed, 121 insertions(+), 132 deletions(-) create mode 100644 .github/scripts/pr-size-label.js diff --git a/.github/scripts/pr-size-label.js b/.github/scripts/pr-size-label.js new file mode 100644 index 0000000000..6c73121bd9 --- /dev/null +++ b/.github/scripts/pr-size-label.js @@ -0,0 +1,105 @@ +module.exports = async ({ github, context, core }) => { + const owner = context.repo.owner; + const repo = context.repo.repo; + const ignoredFilePatterns = [ + /(?:^|\/)\.cnb\/.+\.ya?ml$/i, + /(?:^|\/)\.gitee\/.+\.ya?ml$/i, + /(?:^|\/)\.github\/.+\.ya?ml$/i, + /(?:^|\/)\.idea\/.+\.xml$/i, + /(?:^|\/)\.editorconfig$/i, + /(?:^|\/)\.gitattributes$/i, + /(?:^|\/)\.gitignore$/i, + /(?:^|\/).+\.kts$/i, + /(?:^|\/).+\.md$/i, + /(?:^|\/)LICENSE(?:_.*)?$/i, + /(?:^|\/)gradlew(?:\.bat|\.ps1)?$/i, + /(?:^|\/)gradle\/wrapper\/gradle-wrapper\.properties$/i, + /(?:^|\/)lang\/.+\.properties$/i, + /(?:^|\/)l10n\/.+\.properties$/i, + /(?:^|\/)checkstyle\.xml$/i, + /(?:^|\/).+\.(?:png|jpe?g|gif|ico|svg|json|ttf|woff2?|webp|avif|jxl|heic|mp4|webm|mp3|ogg|wav|txt|toml)$/i + ]; + + async function processPR(issueNumber) { + const files = await github.paginate(github.rest.pulls.listFiles, { + owner, repo, pull_number: issueNumber, per_page: 100, + }); + + const countedFiles = []; + const ignoredFiles = []; + let totalChanges = 0; + + for (const file of files) { + const ignored = ignoredFilePatterns.some(pattern => pattern.test(file.filename)); + if (ignored) { + ignoredFiles.push(file.filename); + continue; + } + + const fileChanges = file.changes ?? ((file.additions || 0) + (file.deletions || 0)); + totalChanges += fileChanges; + countedFiles.push(`${file.filename} (${fileChanges})`); + } + + let targetLabel; + if (totalChanges <= 9) targetLabel = "1+"; + else if (totalChanges <= 39) targetLabel = "10+"; + else if (totalChanges <= 99) targetLabel = "40+"; + else if (totalChanges <= 499) targetLabel = "100+"; + else if (totalChanges <= 999) targetLabel = "500+"; + else if (totalChanges <= 1999) targetLabel = "1000+"; + else if (totalChanges <= 4999) targetLabel = "2000+"; + else targetLabel = "5000+"; + + const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, { + owner, repo, issue_number: issueNumber, per_page: 100, + }); + + const sizeLabels = currentLabels.map(label => label.name).filter(name => /^\d+\+$/.test(name)); + + for (const name of sizeLabels) { + if (name !== targetLabel) { + await github.rest.issues.removeLabel({ + owner, repo, issue_number: issueNumber, name, + }).catch(() => { }); + } + } + + if (!currentLabels.some(label => label.name === targetLabel)) { + await github.rest.issues.addLabels({ + owner, repo, issue_number: issueNumber, labels: [targetLabel], + }); + } + + core.info(`Pull request #${issueNumber} has ${totalChanges} counted changed lines, applied ${targetLabel}.`); + core.info(`Ignored files: ${ignoredFiles.length ? ignoredFiles.join(", ") : "none"}`); + core.info(`Counted files: ${countedFiles.length ? countedFiles.join(", ") : "none"}`); + } + + if (context.eventName === "workflow_dispatch") { + core.info("Manual trigger detected. Fetching all open pull requests..."); + + const openPRs = await github.paginate(github.rest.pulls.list, { + owner, repo, state: "open", per_page: 100, + }); + + const unlabelledPRs = openPRs.filter(pr => { + return !pr.labels.some(label => /^\d+\+$/.test(label.name)); + }); + + core.info(`Found ${openPRs.length} open pull request(s), ${unlabelledPRs.length} of them lack a size label.`); + + for (const pr of unlabelledPRs) { + core.info(`----------------------------------------`); + core.info(`Processing unlabelled pull request #${pr.number}: ${pr.title}`); + await processPR(pr.number); + } + } else { + const pr = context.payload.pull_request; + if (!pr) { + core.info("No pull request found in payload, skipping."); + return; + } + await processPR(pr.number); + } +} diff --git a/.github/workflows/pr-size-label.yml b/.github/workflows/pr-size-label.yml index a36d2375ed..4fc035d419 100644 --- a/.github/workflows/pr-size-label.yml +++ b/.github/workflows/pr-size-label.yml @@ -3,145 +3,29 @@ name: PR Size Label on: pull_request_target: types: - - opened - - reopened - - synchronize + - opened + - reopened + - synchronize workflow_dispatch: - permissions: contents: read issues: write pull-requests: write jobs: - Label: + label: runs-on: ubuntu-latest - steps: - - name: Label PR by filtered changed lines - uses: actions/github-script@v7 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const owner = context.repo.owner - const repo = context.repo.repo - const ignoredFilePatterns = [ - /(?:^|\/)\.cnb\/.+\.ya?ml$/i, - /(?:^|\/)\.gemini\/.+\.ya?ml$/i, - /(?:^|\/)\.gitee\/.+\.ya?ml$/i, - /(?:^|\/)\.github\/.+\.ya?ml$/i, - /(?:^|\/)\.idea\/.+\.xml$/i, - /(?:^|\/)\.editorconfig$/i, - /(?:^|\/)\.gitattributes$/i, - /(?:^|\/)\.gitignore$/i, - /(?:^|\/).+\.kts$/i, - /(?:^|\/).+\.md$/i, - /(?:^|\/)LICENSE(?:_.*)?$/i, - /(?:^|\/)gradlew(?:\.bat|\.ps1)?$/i, - /(?:^|\/)gradle\/wrapper\/gradle-wrapper\.properties$/i, - /(?:^|\/)lang\/.+\.properties$/i, - /(?:^|\/)l10n\/.+\.properties$/i, - /(?:^|\/)checkstyle\.xml$/i, - /(?:^|\/).+\.(?:png|jpe?g|gif|ico|svg|json|ttf|woff2?|webp|avif|jxl|heic|mp4|webm|mp3|ogg|wav|txt|toml)$/i - ] - - async function processPR(issue_number) { - const files = await github.paginate(github.rest.pulls.listFiles, { - owner, - repo, - pull_number: issue_number, - per_page: 100, - }) - - const countedFiles = [] - const ignoredFiles = [] - let totalChanges = 0 - - for (const file of files) { - const ignored = ignoredFilePatterns.some(pattern => pattern.test(file.filename)) - if (ignored) { - ignoredFiles.push(file.filename) - continue - } - - const fileChanges = file.changes ?? ((file.additions || 0) + (file.deletions || 0)) - totalChanges += fileChanges - countedFiles.push(`${file.filename} (${fileChanges})`) - } - - let targetLabel - if (totalChanges <= 9) targetLabel = '1+' - else if (totalChanges <= 39) targetLabel = '10+' - else if (totalChanges <= 99) targetLabel = '40+' - else if (totalChanges <= 499) targetLabel = '100+' - else if (totalChanges <= 999) targetLabel = '500+' - else if (totalChanges <= 1999) targetLabel = '1000+' - else if (totalChanges <= 4999) targetLabel = '2000+' - else targetLabel = '5000+' - - const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, { - owner, - repo, - issue_number, - per_page: 100, - }) - - const sizeLabels = currentLabels - .map(label => label.name) - .filter(name => /^\d+\+$/.test(name)) - - for (const name of sizeLabels) { - if (name !== targetLabel) { - await github.rest.issues.removeLabel({ - owner, - repo, - issue_number, - name, - }).catch(() => {}) - } - } - - if (!currentLabels.some(label => label.name === targetLabel)) { - await github.rest.issues.addLabels({ - owner, - repo, - issue_number, - labels: [targetLabel], - }) - } - - core.info(`Pull request #${issue_number} has ${totalChanges} counted changed lines, applied ${targetLabel}.`) - core.info(`Ignored files: ${ignoredFiles.length ? ignoredFiles.join(', ') : 'none'}`) - core.info(`Counted files: ${countedFiles.length ? countedFiles.join(', ') : 'none'}`) - } - - if (context.eventName === 'workflow_dispatch') { - core.info('Manual trigger detected. Fetching all open pull requests...') - - const openPRs = await github.paginate(github.rest.pulls.list, { - owner, - repo, - state: 'open', - per_page: 100, - }) - - const unlabelledPRs = openPRs.filter(pr => { - return !pr.labels.some(label => /^\d+\+$/.test(label.name)) - }) - - core.info(`Found ${openPRs.length} open pull request(s), ${unlabelledPRs.length} of them lack a size label.`) - - for (const pr of unlabelledPRs) { - core.info(`----------------------------------------`) - core.info(`Processing unlabelled pull request #${pr.number}: ${pr.title}`) - await processPR(pr.number) - } - } else { - const pr = context.payload.pull_request - if (!pr) { - core.info('No pull request found in payload, skipping.') - return - } - await processPR(pr.number) - } \ No newline at end of file + - name: Checkout scripts + uses: actions/checkout@v7 + with: + sparse-checkout: | + .github/scripts + sparse-checkout-cone-mode: true + - name: Label PR by filtered changed lines + uses: actions/github-script@v9 + with: + script: | + const script = require("./.github/scripts/pr-size-label.js"); + await script({ github, context, core });