From 4e776c8051dfe94b270a4ac82067e179cf6bdea2 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 2 Dec 2026 03:26:34 +0000 Subject: [PATCH] meta: add large-pr detection workflow A workflow that detect PRs falling under the large-pull-request policy and automatically applies the label and posts a notification when appropriate to do so. Signed-off-by: James M Snell Assisted-by: Claude --- .github/workflows/large-pr.yml | 228 ++++++++++++++++++++++++ doc/contributing/large-pull-requests.md | 13 ++ 2 files changed, 241 insertions(+) create mode 100644 .github/workflows/large-pr.yml diff --git a/.github/workflows/large-pr.yml b/.github/workflows/large-pr.yml new file mode 100644 index 000000000000..cfe33cd72bec --- /dev/null +++ b/.github/workflows/large-pr.yml @@ -0,0 +1,228 @@ +# This action requires the following secrets to be set on the repository: +# GH_USER_TOKEN: GitHub user token, used because the author association in +# the event payload may not reflect private organization +# membership; only privileged API calls return it reliably. +# +# Applies the `large-pr` label to pull requests covered by +# doc/contributing/large-pull-requests.md and posts a one-time notice when the +# author is not a collaborator. The label is never removed automatically, and +# it is not re-applied once a collaborator has removed it (e.g. for excluded +# categories such as test-only refactoring). + +name: Large pull requests + +on: + pull_request_target: + types: [opened, reopened, synchronize, labeled] + +permissions: {} + +env: + LABEL: large-pr + # Keep in sync with doc/contributing/large-pull-requests.md. + THRESHOLD: 5000 + +jobs: + author: + name: Resolve author status + # Automated pull requests (routine dependency updates, WPT imports, etc.) + # are excluded by the policy. These bot accounts are regular user + # accounts, so the `Bot` type check does not cover them. + if: >- + github.repository == 'nodejs/node' && + github.event.pull_request.user.type != 'Bot' && + github.event.pull_request.user.login != 'nodejs-github-bot' && + github.event.pull_request.user.login != 'npm-cli-bot' && + (github.event.action != 'labeled' || github.event.label.name == 'large-pr') + runs-on: ubuntu-slim + outputs: + is_collaborator: ${{ steps.resolve.outputs.is_collaborator }} + steps: + - name: Check author association + id: resolve + env: + EVENT_ASSOCIATION: ${{ github.event.pull_request.author_association }} + GH_TOKEN: ${{ secrets.GH_USER_TOKEN }} + NUMBER: ${{ github.event.pull_request.number }} + run: | + association="$EVENT_ASSOCIATION" + case "$association" in + COLLABORATOR|MEMBER|OWNER) ;; + *) + association=$(gh api "/repos/$GITHUB_REPOSITORY/pulls/$NUMBER" \ + --jq '.author_association') + ;; + esac + echo "Author association: $association" + + case "$association" in + CONTRIBUTOR|FIRST_TIMER|FIRST_TIME_CONTRIBUTOR|NONE) + echo 'is_collaborator=false' >> "$GITHUB_OUTPUT" + ;; + COLLABORATOR|MANNEQUIN|MEMBER|OWNER) + echo 'is_collaborator=true' >> "$GITHUB_OUTPUT" + ;; + *) + echo "Unexpected author association: $association" >&2 + exit 1 + ;; + esac + + classify: + name: Identify large pull request + needs: author + # Job-level concurrency so that skipped runs for unrelated `labeled` + # events do not cancel an in-progress classification. + concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + runs-on: ubuntu-slim + permissions: + pull-requests: write + outputs: + has_label: ${{ steps.classify.outputs.has_label }} + reason: ${{ steps.classify.outputs.reason }} + steps: + - name: Classify and label + id: classify + env: + GH_TOKEN: ${{ github.token }} + IS_COLLABORATOR: ${{ needs.author.outputs.is_collaborator }} + NUMBER: ${{ github.event.pull_request.number }} + run: | + # Values in the event payload can be stale on `synchronize`, so ask + # the API for the current state. + { + read -r additions deletions changed_files has_label + IFS= read -r title + } < <( + gh api "repos/$GITHUB_REPOSITORY/pulls/$NUMBER" \ + --jq '"\(.additions) \(.deletions) \(.changed_files) \(any(.labels[]; .name == env.LABEL))\n\(.title)"' + ) + net=$((additions - deletions)) + echo "Title: $title" + echo "Net change: $net (+$additions/-$deletions) in $changed_files files" + + # The API lists at most 3000 files, sorted by path. + files=$(mktemp) + gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/files" \ + --jq '.[] | "\(.status)\t\(.filename)\t\(.additions - .deletions)"' > "$files" + listed=$(wc -l < "$files") + truncated=false + if (( listed < changed_files )); then + truncated=true + echo "Only $listed of $changed_files files are listed." + fi + + # Lines in excluded categories do not count toward the threshold; + # any other changes in the same pull request still do. Test-only + # refactoring cannot be detected, so collaborators remove the label. + # Excluded lines are subtracted only for listed files, so a + # truncated listing errs toward labeling. + read -r wpt_net deps_net < <( + awk -F '\t' '$2 ~ /^test\/fixtures\/wpt\// { w += $3 } + $2 ~ /^deps\// { d += $3 } + END { print w + 0, d + 0 }' "$files" + ) + excluded_net=$wpt_net + excluded_desc='imported WPT files' + routine_update='^deps: (update|upgrade|patch) [^ ]+ to [^ ]+' + routine=false + if [[ "$IS_COLLABORATOR" == 'true' && "$title" =~ $routine_update ]]; then + routine=true + excluded_net=$((excluded_net + deps_net)) + excluded_desc='imported WPT files and the dependency update' + fi + counted=$((net - excluded_net)) + echo "Counted net change: $counted (excluding $excluded_net in $excluded_desc)" + + if [[ "$excluded_net" -ne 0 ]]; then + counted_desc="$counted lines of net change, not counting $excluded_desc" + else + counted_desc="$counted lines of net change" + fi + + reason='' + if [[ "$routine" == 'true' && "$truncated" == 'true' ]]; then + # Paths after `deps/` are not listed, so the rest of the change + # cannot be measured (e.g. V8 updates). + echo 'Routine dependency update too large to measure; not labeling.' + elif (( counted > THRESHOLD )); then + reason="it has $counted_desc, above the $THRESHOLD-line threshold" + else + # A new public `node:foo` or `node:foo/bar` module is always large. + new_modules=$( + awk -F '\t' '$1 == "added" && $2 !~ /^lib\/internal\// && + $2 ~ /^lib\/[^\/]+(\/[^\/]+)?\.js$/ { print $2 }' "$files" | + sed -E 's|^lib/(.*)\.js$|`node:\1`|' | + paste -sd, - | sed 's/,/, /g' + ) + if [[ -n "$new_modules" ]]; then + echo "New public modules: $new_modules" + reason="it adds a new public module ($new_modules)" + fi + fi + + if [[ -n "$reason" && "$has_label" != 'true' ]]; then + removed=$( + gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$NUMBER/events" \ + --jq '.[] | select(.event == "unlabeled" and .label.name == env.LABEL) | .id' + ) + if [[ -n "$removed" ]]; then + echo "The $LABEL label was previously removed; not re-applying." + else + gh pr edit "$NUMBER" --repo "$GITHUB_REPOSITORY" --add-label "$LABEL" + has_label=true + fi + fi + + echo "has_label=$has_label" >> "$GITHUB_OUTPUT" + echo "reason=$reason" >> "$GITHUB_OUTPUT" + + notice: + name: Notify non-collaborator + needs: [author, classify] + if: >- + needs.author.outputs.is_collaborator == 'false' && + needs.classify.outputs.has_label == 'true' + concurrency: + group: ${{ github.workflow }}-notice-${{ github.event.pull_request.number }} + runs-on: ubuntu-slim + permissions: + pull-requests: write + steps: + - name: Post large pull request notice + env: + GH_TOKEN: ${{ github.token }} + NUMBER: ${{ github.event.pull_request.number }} + REASON: ${{ needs.classify.outputs.reason }} + MARKER: + NOTICE_MESSAGE: >- + Per the + [large pull requests policy](https://github.com/nodejs/node/blob/HEAD/doc/contributing/large-pull-requests.md), + large pull requests may only be opened by + [collaborators](https://github.com/nodejs/node/#current-project-team-members). + A large pull request from a non-collaborator will be closed unless the + change has been discussed in an issue and a collaborator has agreed to + champion the work. If that is the case, please link the issue and + mention the championing collaborator in the pull request description. + Otherwise, please consider whether this change can be split into + smaller pull requests that can each be reviewed independently. + run: | + existing=$( + gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$NUMBER/comments" \ + --jq '.[] | select(.user.login == "github-actions[bot]" and (.body | contains(env.MARKER))) | .id' + ) + if [[ -n "$existing" ]]; then + echo 'Notice already posted.' + exit 0 + fi + + if [[ -n "$REASON" ]]; then + intro="This pull request has been labeled \`$LABEL\` because $REASON." + else + intro="This pull request has been labeled \`$LABEL\`." + fi + + printf '%s\n%s\n\n%s\n' "$MARKER" "$intro" "$NOTICE_MESSAGE" | + gh pr comment "$NUMBER" --repo "$GITHUB_REPOSITORY" --body-file - diff --git a/doc/contributing/large-pull-requests.md b/doc/contributing/large-pull-requests.md index 6660fb0d48f1..7014f1829077 100644 --- a/doc/contributing/large-pull-requests.md +++ b/doc/contributing/large-pull-requests.md @@ -50,6 +50,19 @@ even if they exceed the line threshold: These pull requests already have established review processes and do not benefit from the additional requirements described here. +When a pull request combines an excluded change with other changes, only the +lines of the excluded change are left out of the count. The rest of the pull +request is counted against the threshold as usual. For example, a WPT import +in `test/fixtures/wpt/` combined with a 10-line fix is not large, but the same +import combined with 6000 lines of changes in `lib/` is. + +The `large-pr` label is applied automatically to pull requests that exceed the +line threshold or add a new public module in `lib/`, and a notice is posted +when the author is not a collaborator. Automated pull requests are skipped. +Collaborators may also apply the label manually. If the label is applied to a +pull request in one of the excluded categories, a collaborator can remove it +and it will not be re-applied. + ## Who can open a large pull request Large pull requests may only be opened by existing