Repository navigation
228 lines (212 loc) Β· 9.44 KB
/
Copy pathlarge-pr.yml
File metadata and controls
228 lines (212 loc) Β· 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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: <!-- large-pr-notice -->
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 -