From 2ce110dffeac47137212cdbf1b7c56f97354fc68 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:25:51 -0500 Subject: [PATCH] fix: scope labeler trigger to reduce rate limiting and fix bugfix typo (#5020) --- .github/labeler.yml | 35 --------------- .github/workflows/pr_enforce_labels.yml | 2 +- .github/workflows/pull-request-target.yml | 52 +++++++++++++++++++++-- 3 files changed, 49 insertions(+), 40 deletions(-) delete mode 100644 .github/labeler.yml diff --git a/.github/labeler.yml b/.github/labeler.yml deleted file mode 100644 index c3c2fa6cf..000000000 --- a/.github/labeler.yml +++ /dev/null @@ -1,35 +0,0 @@ -# Auto Labeler rulse using https://github.com/actions/labeler -# - -# 'fix' in title/branch -> bug -# 'feat' in title/branch -> enhancement -# 'repo' in title/branch OR changes to ~/.github/ -> repo -# 'bug_fallthrough' for everything else except auto -# -# - [ ] need to look at title. waiting on https://github.com/actions/labeler/pull/866 - -# Add 'enhancement' label to any PR where the head branch name contains `feat` -enhancement: - - head-branch: [feat, Feat, FEAT] - - # Add 'repo' label to any PR where the head branch name contains `repo` - # or files in the .github dir -repo: -- any: - - head-branch: [repo, Repo, REPO, ci, CI] - - changed-files: - - any-glob-to-any-file: .github - - # Add 'bug' label to any PR where the head branch name contains `fix` or `bug` as the prefix. -bugfix: - - head-branch: [^fix, ^bug, ^Fix, ^FIX, ^Bug, ^BUG] - -# Add `refactor` label to any PR where the head branch name contains `refactor` or `Refactor` as the prefix. -refactor: - - head-branch: [^refactor, ^Refactor] - -# our fallback - bug except repo, feat, or automated pipelines -# bug_fallthrough: -# - all: -# - head-branch: ['^((?!feat).)*$', '^((?!repo).)*$', '^((?!renovate).)*$', '^((?!scheduled).)*$'] - diff --git a/.github/workflows/pr_enforce_labels.yml b/.github/workflows/pr_enforce_labels.yml index 9911dd612..59763f38c 100644 --- a/.github/workflows/pr_enforce_labels.yml +++ b/.github/workflows/pr_enforce_labels.yml @@ -18,7 +18,7 @@ jobs: script: | // Extract labels from the payload directly to avoid extra API calls const latestLabels = context.payload.pull_request.labels.map(label => label.name); - const requiredLabels = ['bugpost', 'enhancement', 'automation', 'dependencies', 'repo', 'release', 'refactor']; + const requiredLabels = ['bugfix', 'enhancement', 'automation', 'dependencies', 'repo', 'release', 'refactor']; console.log('Labels from payload:', latestLabels); const hasRequiredLabel = latestLabels.some(label => requiredLabels.includes(label)); if (!hasRequiredLabel) { diff --git a/.github/workflows/pull-request-target.yml b/.github/workflows/pull-request-target.yml index 7dfe1674b..e03f9eb25 100644 --- a/.github/workflows/pull-request-target.yml +++ b/.github/workflows/pull-request-target.yml @@ -1,7 +1,8 @@ name: "Pull Request Labeler" on: -- pull_request_target -# Do not execute arbitary code on this workflow. + pull_request_target: + types: [opened, synchronize] +# Do not execute arbitrary code on this workflow. # See warnings at https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target jobs: @@ -11,5 +12,48 @@ jobs: pull-requests: write runs-on: ubuntu-24.04-arm steps: - - id: label-the-PR - uses: actions/labeler@v6 \ No newline at end of file + - name: Auto-label PR + uses: actions/github-script@v8 + with: + script: | + const branch = context.payload.pull_request.head.ref; + const labels = new Set(); + + // enhancement: branch contains feat + if (/feat/i.test(branch)) labels.add('enhancement'); + + // bugfix: branch starts with fix or bug + if (/^(fix|bug)/i.test(branch)) labels.add('bugfix'); + + // refactor: branch starts with refactor + if (/^refactor/i.test(branch)) labels.add('refactor'); + + // repo: branch contains repo or ci + if (/repo|ci/i.test(branch)) { + labels.add('repo'); + } else { + // Also label 'repo' if .github files were changed (needs one API call) + try { + const files = await github.paginate( + github.rest.pulls.listFiles, + { owner: context.repo.owner, repo: context.repo.repo, pull_number: context.payload.pull_request.number, per_page: 100 }, + (res) => res.data.map(f => f.filename) + ); + if (files.some(f => f.startsWith('.github/'))) labels.add('repo'); + } catch (e) { + core.warning(`Could not list PR files (rate limited?): ${e.message}`); + } + } + + if (labels.size > 0) { + const labelArray = [...labels]; + core.info(`Applying labels: ${labelArray.join(', ')}`); + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + labels: labelArray, + }); + } else { + core.info('No labels matched for this PR.'); + }