-
Notifications
You must be signed in to change notification settings - Fork 117
157 lines (137 loc) · 6.92 KB
/
Copy pathpr-label.yml
File metadata and controls
157 lines (137 loc) · 6.92 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
# Automatically add a label to PRs based on the [Label] prefix in the title
#
# Usage:
# - PR title must start with a [Label] prefix in brackets
# - Example: "[BugFix] Fix memory leak" will add the "bug" label
# - Fails if no valid prefix is found
# - Labels are ONLY ADDED, never removed (preserves manual labels)
# - Matching is case-insensitive
#
# Supported prefixes -> label:
# [BE] -> BE
# [BugFix], [Fix] -> bug
# [Feature] -> Feature
# [Doc], [Docs], [Documentation] -> documentation
# [Refactor], [Refactoring] -> Refactor
# [CI] -> CI
# [Test], [Tests] -> Test
# [Compile] -> Compile
# [Performance], [Perf] -> Performance
# [Deprecation], [Deprecated] -> Deprecation
# [Setup] -> setup
# [Distributed], [Dist], [DTensor] -> Distributed
# [Benchmark], [Benchmarks], [Bench] -> Benchmarks
# [Typing], [Type] -> Typing
# [BC-breaking], [BC] -> BC-breaking
# [Formatting], [Format] -> Formatting
# [Quality] -> Quality
# [Release] -> release
# [Versioning] -> versioning
#------------------------------------------------------------
name: PR Label
on:
# Using pull_request_target to have write access for PRs from forks
# This is safe because we only read PR metadata (title), not code from the fork
pull_request_target:
types: [opened, edited, synchronize, reopened]
jobs:
add-label:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Parse and apply label from PR title
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
set -euo pipefail
post_help_comment() {
local reason="$1"
local current_title="$2"
cat > /tmp/pr_comment.md << 'COMMENT_EOF'
## PR Title Label Error
REASON_PLACEHOLDER
**Current title:** `TITLE_PLACEHOLDER`
### Supported Prefixes
Your PR title must start with **exactly** one of these prefixes (case-insensitive):
| Prefix | Label Applied | Example |
|--------|---------------|---------|
| `[BE]` | BE | `[BE] Improve internal plumbing` |
| `[BugFix]` or `[Fix]` | bug | `[BugFix] Fix memory leak in TensorDict` |
| `[Feature]` | Feature | `[Feature] Add new storage backend` |
| `[Doc]` or `[Docs]` | documentation | `[Doc] Update installation guide` |
| `[Refactor]` | Refactor | `[Refactor] Clean up module imports` |
| `[CI]` | CI | `[CI] Fix workflow permissions` |
| `[Test]` or `[Tests]` | Test | `[Test] Add unit tests for nn module` |
| `[Compile]` | Compile | `[Compile] Fix torch.compile issue` |
| `[Performance]` or `[Perf]` | Performance | `[Perf] Optimize tensor operations` |
| `[Deprecation]` | Deprecation | `[Deprecation] Mark old function` |
| `[Setup]` | setup | `[Setup] Update build configuration` |
| `[Distributed]`, `[Dist]`, or `[DTensor]` | Distributed | `[DTensor] Add cross-mesh transfer` |
| `[Benchmark]` or `[Bench]` | Benchmarks | `[Benchmark] Add compile benchmark` |
| `[Typing]` or `[Type]` | Typing | `[Typing] Add type stubs` |
| `[BC-breaking]` or `[BC]` | BC-breaking | `[BC-breaking] Remove deprecated API` |
| `[Formatting]` or `[Format]` | Formatting | `[Format] Fix code style` |
| `[Quality]` | Quality | `[Quality] Improve error messages` |
| `[Release]` | release | `[Release] Publish release artifacts` |
| `[Versioning]` | versioning | `[Versioning] Bump release version` |
**Note:** Matching is case-insensitive. Common variations (singular/plural) are supported.
COMMENT_EOF
sed -i 's/^ *//' /tmp/pr_comment.md
COMMENT_REASON="$reason" COMMENT_TITLE="$current_title" python3 - <<'PY'
import os
from pathlib import Path
path = Path("/tmp/pr_comment.md")
comment = path.read_text()
comment = comment.replace("REASON_PLACEHOLDER", os.environ["COMMENT_REASON"])
comment = comment.replace("TITLE_PLACEHOLDER", os.environ["COMMENT_TITLE"])
path.write_text(comment)
PY
gh pr comment "$PR_NUMBER" --repo "$REPO" --body-file /tmp/pr_comment.md
}
echo "PR Title: $PR_TITLE"
# Check if title starts with [...]
if [[ ! "$PR_TITLE" =~ ^\[([^\]]+)\] ]]; then
echo "::error::PR title must start with [Label]. Got: '$PR_TITLE'"
post_help_comment "PR title must start with a label prefix in brackets (e.g., \`[BugFix]\`)." "$PR_TITLE"
exit 1
fi
# Extract the prefix
PREFIX="${BASH_REMATCH[1]}"
echo "Extracted prefix: $PREFIX"
# Case-insensitive matching: lowercase the prefix once
PREFIX_LOWER="${PREFIX,,}"
# Map prefixes to GitHub label names
case "$PREFIX_LOWER" in
be) LABEL="BE" ;;
bugfix|fix|bug) LABEL="bug" ;;
feature|features) LABEL="Feature" ;;
doc|docs|documentation) LABEL="documentation" ;;
refactor|refactoring) LABEL="Refactor" ;;
ci) LABEL="CI" ;;
test|tests) LABEL="Test" ;;
compile) LABEL="Compile" ;;
performance|perf) LABEL="Performance" ;;
deprecation|deprecated) LABEL="Deprecation" ;;
setup) LABEL="setup" ;;
distributed|dist|dtensor) LABEL="Distributed" ;;
benchmark|benchmarks|bench) LABEL="Benchmarks" ;;
typing|type) LABEL="Typing" ;;
bc-breaking|bc) LABEL="BC-breaking" ;;
formatting|format) LABEL="Formatting" ;;
quality) LABEL="Quality" ;;
release) LABEL="release" ;;
versioning) LABEL="versioning" ;;
*)
echo "::error::Unknown or invalid prefix '[$PREFIX]'."
post_help_comment "Unknown or invalid prefix \`[$PREFIX]\`." "$PR_TITLE"
exit 1
;;
esac
echo "Mapped to label: $LABEL"
# Add the label to the PR (never remove existing labels)
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "$LABEL"
echo "Successfully added label '$LABEL' to PR #$PR_NUMBER"