Repository navigation
79 lines (71 loc) · 2.97 KB
/
Copy pathpr-size-label.yml
File metadata and controls
79 lines (71 loc) · 2.97 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
name: PR size label
# Labels each PR with size/normal|large|very-large from its non-test churn,
# per the Endpoints PR Review Policy. Advisory — it never fails the PR.
#
# SECURITY: this uses `pull_request_target` so GITHUB_TOKEN can write labels on
# PRs opened from forks (a plain `pull_request` token is read-only for forks).
# That means the workflow runs in the BASE repo's context with write access, so
# it MUST NOT check out or execute any PR-provided code. It checks out the base
# repo's copy of scripts/pr_size_check.py and only *fetches* the PR head commit
# to count `git diff` lines. Do not add steps that run code from the PR.
on:
pull_request_target:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
concurrency:
group: pr-size-label-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
size-label:
runs-on: ubuntu-latest
steps:
- name: Checkout base repo (trusted)
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.pull_request.base.sha }}
fetch-depth: 0
- name: Fetch PR head commit (data only, not executed)
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
case "$PR_NUMBER" in
''|*[!0-9]*) echo "unexpected PR number: $PR_NUMBER" >&2; exit 1 ;;
esac
git fetch --no-tags origin "refs/pull/${PR_NUMBER}/head"
- name: Compute size class
id: size
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
python3 scripts/pr_size_check.py \
--base "$BASE_SHA" \
--head FETCH_HEAD \
--github-output
- name: Apply size label
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR: ${{ github.event.pull_request.number }}
CLS: ${{ steps.size.outputs.size_class }}
run: |
if [ -z "$CLS" ]; then
echo "No size class computed (base unresolved / diff skipped); nothing to label."
exit 0
fi
# The size/* labels must be created once out-of-band via
# `gh label create size/{normal,large,very-large}`. Applying is
# best-effort: a missing label or transient API error warns but never
# fails the PR (this check is advisory).
# Strip the other size/* labels only after the add succeeds, so a
# failed add never leaves the PR with no size label at all.
if gh pr edit "$PR" --add-label "size/$CLS"; then
for other in normal large very-large; do
if [ "$other" != "$CLS" ]; then
gh pr edit "$PR" --remove-label "size/$other" 2>/dev/null || true
fi
done
else
echo "::warning::Could not apply label size/$CLS — ensure the size/* labels exist (create them once with 'gh label create')."
fi