Repository navigation
Expand file tree
/
Copy pathcheck-release-notes.sh
More file actions
executable file
·226 lines (204 loc) · 8.16 KB
/
Copy pathcheck-release-notes.sh
File metadata and controls
executable file
·226 lines (204 loc) · 8.16 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
#!/usr/bin/env bash
# Pre-commit hook to check that release note fragments are added when code changes.
# Works in two modes:
# - Local commit: uses git diff --cached to get staged files
# - CI (pre-commit.ci / GitHub Actions): uses git diff against main branch
set -euo pipefail
# Maps code folders to their corresponding unreleased fragment directories.
# byLLM and scale are folded into jac/jaclang/ (jaclang.byllm / jaclang.scale),
# so their changes are covered by the jac/jaclang/ -> jaclang mapping below.
declare -A FOLDER_TO_FRAGMENTS=(
["jac/jaclang/"]="release_notes/unreleased/jaclang/"
)
# Determine changed files based on context
if [ -n "${CI:-}" ] || [ -n "${PRE_COMMIT_FROM_REF:-}" ]; then
# CI mode: compare against main branch
if [ -n "${PRE_COMMIT_FROM_REF:-}" ] && [ -n "${PRE_COMMIT_TO_REF:-}" ]; then
CHANGED_FILES=$(git diff --name-only "$PRE_COMMIT_FROM_REF"..."$PRE_COMMIT_TO_REF" 2>/dev/null || true)
else
# Fallback: compare against origin/main
MERGE_BASE=$(git merge-base origin/main HEAD 2>/dev/null || echo "")
if [ -z "$MERGE_BASE" ]; then
exit 0
fi
CHANGED_FILES=$(git diff --name-only "$MERGE_BASE"...HEAD 2>/dev/null || true)
fi
else
# Local mode: check staged files
CHANGED_FILES=$(git diff --cached --name-only 2>/dev/null || true)
fi
if [ -z "$CHANGED_FILES" ]; then
exit 0
fi
# Release PRs created by the release script legitimately edit protected files - skip all checks
if [[ "${PR_TITLE:-}" == release:* ]] || [[ "${PR_AUTHOR:-}" == "github-actions[bot]" ]]; then
exit 0
fi
# Check if any release notes .md files were directly modified
PROTECTED_FILES=(
"jac/jaclang/cli/docs/community/release_notes/jaclang.md"
)
DIRECTLY_MODIFIED=()
while IFS= read -r file; do
[ -z "$file" ] && continue
for protected in "${PROTECTED_FILES[@]}"; do
if [[ "$file" == "$protected" ]]; then
DIRECTLY_MODIFIED+=("$file")
fi
done
done <<< "$CHANGED_FILES"
if [ ${#DIRECTLY_MODIFIED[@]} -gt 0 ]; then
echo ""
echo "=========================================="
echo "ERROR: Do not edit release notes files directly!"
echo "=========================================="
echo ""
echo "The following files were modified directly:"
echo ""
for item in "${DIRECTLY_MODIFIED[@]}"; do
echo " - $item"
done
echo ""
echo "Release notes are managed via fragment files."
echo "Add a fragment at: release_notes/unreleased/<package>/<PR#>.<category>.md"
echo ""
exit 1
fi
# Fragment path with the PR number captured in group 1.
FRAGMENT_REGEX='release_notes/unreleased/[^/]+/([0-9]+)\.(feature|bugfix|breaking)\.md$'
# Categories the release notes no longer carry. A PR that is only an internal
# refactor or a docs edit has nothing to tell a user at release time, so it
# takes the skip label instead of filing a fragment.
REJECTED_REGEX='release_notes/unreleased/[^/]+/([0-9]+)\.(refactor|docs)\.md$'
CHANGED_FRAGMENTS=()
REJECTED_FRAGMENTS=()
while IFS= read -r file; do
[ -z "$file" ] && continue
# Deleted fragments (cleanup of stale entries) are not "this PR's fragment"
[ -f "$file" ] || continue
[[ "$file" =~ $FRAGMENT_REGEX ]] && CHANGED_FRAGMENTS+=("$file")
[[ "$file" =~ $REJECTED_REGEX ]] && REJECTED_FRAGMENTS+=("$file")
done <<< "$CHANGED_FILES"
if [ ${#REJECTED_FRAGMENTS[@]} -gt 0 ]; then
echo ""
echo "=========================================="
echo "ERROR: 'refactor' and 'docs' release notes are not accepted!"
echo "=========================================="
echo ""
echo "This PR adds or edits fragments in a retired category:"
echo ""
for item in "${REJECTED_FRAGMENTS[@]}"; do
echo " - $item"
done
echo ""
echo "Release notes carry user-facing changes only. The accepted"
echo "categories are: feature, bugfix, breaking."
echo ""
echo "If the change IS user-facing, rename the fragment to the category"
echo "that fits:"
echo " release_notes/unreleased/<package>/<PR#>.<feature|bugfix|breaking>.md"
echo ""
echo "If it is a pure internal refactor or a docs-only edit, do not file a"
echo "fragment at all: add the 'skip-release-notes-check' label to your PR."
echo ""
exit 1
fi
# A PR may only add/edit a fragment named after its own number; skipped locally
# where PR_NUMBER is unknown.
PR_NUMBER="${PR_NUMBER:-}"
if [ -n "$PR_NUMBER" ]; then
WRONG_NUMBER_FRAGMENTS=()
for file in "${CHANGED_FRAGMENTS[@]}"; do
[[ "$file" =~ $FRAGMENT_REGEX ]] || continue
if [ "${BASH_REMATCH[1]}" != "$PR_NUMBER" ]; then
WRONG_NUMBER_FRAGMENTS+=("$file")
fi
done
if [ ${#WRONG_NUMBER_FRAGMENTS[@]} -gt 0 ]; then
echo ""
echo "=========================================="
echo "ERROR: Release note fragment does not belong to this PR!"
echo "=========================================="
echo ""
echo "This PR (#${PR_NUMBER}) changes fragment files whose number is not ${PR_NUMBER}:"
echo ""
for item in "${WRONG_NUMBER_FRAGMENTS[@]}"; do
echo " - $item"
done
echo ""
echo "You may only add or edit a fragment named after your own PR number:"
echo " release_notes/unreleased/<package>/${PR_NUMBER}.<category>.md"
echo ""
echo "Do not reuse a different number and do not modify other contributors' fragments."
echo ""
echo "To skip this check, add the 'skip-release-notes-check' label to your PR."
echo ""
exit 1
fi
fi
MISSING_NOTES=()
for folder in "${!FOLDER_TO_FRAGMENTS[@]}"; do
fragments_dir="${FOLDER_TO_FRAGMENTS[$folder]}"
folder_changed=false
fragment_added=false
while IFS= read -r file; do
[ -z "$file" ] && continue
if [[ "$file" == "${folder}"* ]] && [[ "$file" != */tests/* ]]; then
folder_changed=true
fi
if [[ "$file" == "${fragments_dir}"* ]] && [[ "$file" =~ /([0-9]+)\.(feature|bugfix|breaking)\.md$ ]]; then
if [ -z "$PR_NUMBER" ] || [ "${BASH_REMATCH[1]}" == "$PR_NUMBER" ]; then
fragment_added=true
fi
fi
done <<< "$CHANGED_FILES"
if $folder_changed && ! $fragment_added; then
MISSING_NOTES+=("${folder} -> ${fragments_dir}<PR#>.<feature|bugfix|breaking>.md")
fi
done
if [ ${#MISSING_NOTES[@]} -gt 0 ]; then
echo ""
echo "=========================================="
echo "ERROR: Release note fragment not added!"
echo "=========================================="
echo ""
echo "The following folders were modified but no release note fragment was added:"
echo ""
for item in "${MISSING_NOTES[@]}"; do
echo " - $item"
done
echo ""
echo "Please add a release note fragment file."
echo "Example: release_notes/unreleased/<package>/1234.bugfix.md"
echo " release_notes/unreleased/<package>/1234.breaking.md"
echo ""
echo "Fragment content should be a single bullet point, e.g.:"
echo ' - **Fix: Brief title**: Description of the change.'
echo ""
echo "To skip this check, add the 'skip-release-notes-check' label to your PR."
echo ""
exit 1
fi
# Validate content format of newly added/modified fragment files
MALFORMED_FRAGMENTS=()
for file in "${CHANGED_FRAGMENTS[@]}"; do
[ -f "$file" ] || continue
# Reject plain paragraphs and headings; entries must be bullet points.
grep -qE '^[^[:space:]-]|^-[[:space:]]+#{1,6}[[:space:]]' "$file" 2>/dev/null && \
MALFORMED_FRAGMENTS+=("$file: all entries must be bullet points starting with '- ' (no headings or plain paragraphs)")
done
if [ ${#MALFORMED_FRAGMENTS[@]} -gt 0 ]; then
echo ""
echo "=========================================="
echo "ERROR: Malformed release note fragment(s)!"
echo "=========================================="
echo ""
printf ' - %s\n' "${MALFORMED_FRAGMENTS[@]}"
echo ""
echo "Each fragment must be a single bullet point:"
echo ' - **Category: Brief title**: Description of the change.'
echo ""
echo "To skip this check, add the 'skip-release-notes-check' label to your PR."
echo ""
exit 1
fi