返回 last30days-skill
changelog-guard.yml
根目录 / .github / workflows / changelog-guard.yml
1 name: Changelog guard
2
3 # Non-release PRs must not edit CHANGELOG.md or bump lockstep version strings.
4 # Content edits to SKILL.md / pyproject.toml / uv.lock are fine.
5 # Release PRs (label: release) are exempt. Engine changes need a changelog
6 # fragment unless labeled skip-changelog. Dependabot PRs skip the fragment
7 # requirement automatically (author dependabot[bot]); the label is optional.
8
9 on:
10 pull_request:
11 types: [opened, synchronize, reopened, labeled, unlabeled]
12
13 permissions: {}
14
15 jobs:
16 guard:
17 runs-on: ubuntu-latest
18 permissions:
19 contents: read
20 pull-requests: read
21 steps:
22 - name: Checkout
23 uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
24 with:
25 fetch-depth: 0
26 persist-credentials: false
27
28 - name: Enforce changelog / version lockstep rules
29 env:
30 GH_TOKEN: ${{ github.token }}
31 PR_NUMBER: ${{ github.event.pull_request.number }}
32 BASE_REF: ${{ github.event.pull_request.base.ref }}
33 HEAD_SHA: ${{ github.event.pull_request.head.sha }}
34 PR_AUTHOR: ${{ github.event.pull_request.user.login }}
35 run: |
36 set -euo pipefail
37
38 # Compare files and versions from the same merge base, even when the
39 # event's base SHA predates a release merged into the PR branch.
40 # fetch-depth: 0 populates refs/remotes/origin without another fetch.
41 BASE_REMOTE_REF="refs/remotes/origin/${BASE_REF}"
42 if ! git rev-parse --verify --quiet "${BASE_REMOTE_REF}" >/dev/null; then
43 echo "::error::Cannot resolve ${BASE_REMOTE_REF}. Checkout must use fetch-depth: 0."
44 exit 1
45 fi
46 BASE_SHA="$(git merge-base "${BASE_REMOTE_REF}" "${HEAD_SHA}")"
47 echo "Base ${BASE_SHA} (merge-base with origin/${BASE_REF})"
48
49 LABELS="$(gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/labels" --jq '.[].name')"
50 IS_RELEASE=0
51 SKIP_CHANGELOG=0
52 if printf '%s\n' "${LABELS}" | grep -qx 'release'; then
53 IS_RELEASE=1
54 fi
55 if printf '%s\n' "${LABELS}" | grep -qx 'skip-changelog'; then
56 SKIP_CHANGELOG=1
57 fi
58 # Dependabot cannot add skip-changelog itself (labels: in
59 # dependabot.yml replaces default ecosystem labels, and a missing
60 # repo label is silently dropped). Author is available on opened.
61 if [ "${PR_AUTHOR}" = "dependabot[bot]" ]; then
62 SKIP_CHANGELOG=1
63 echo "PR author is dependabot[bot] — changelog fragment not required."
64 fi
65
66 mapfile -t CHANGED < <(git diff --name-only "${BASE_SHA}...${HEAD_SHA}")
67
68 changed_changelog=0
69 for path in "${CHANGED[@]}"; do
70 if [ "${path}" = "CHANGELOG.md" ]; then
71 changed_changelog=1
72 fi
73 done
74
75 if [ "${IS_RELEASE}" -eq 1 ]; then
76 echo "PR has label 'release' — version/CHANGELOG edits allowed."
77 exit 0
78 fi
79
80 if [ "${changed_changelog}" -eq 1 ]; then
81 # One-time towncrier adoption: replacing ## [Unreleased] with the
82 # start marker / notice is allowed. Adding release-note bullets
83 # (+### sections) is not.
84 cl_diff="$(git diff "${BASE_SHA}...${HEAD_SHA}" -- CHANGELOG.md || true)"
85 if printf '%s\n' "${cl_diff}" | grep -q 'towncrier release notes start' \
86 && ! printf '%s\n' "${cl_diff}" | grep -qE '^\+### '; then
87 echo "Allowing towncrier bootstrap CHANGELOG.md header change."
88 changed_changelog=0
89 fi
90 fi
91
92 if [ "${changed_changelog}" -eq 1 ]; then
93 echo "::error::Do not edit CHANGELOG.md in feature PRs."
94 echo "Add changelog.d/<n>.<type>.md instead (see changelog.d/README.md)."
95 echo "Release PRs created via Actions → Prepare release use the 'release' label."
96 exit 1
97 fi
98
99 # Keep version parsing in .github/scripts/ — a prior inline
100 # python3 -c block used column-0 source and made Actions refuse
101 # to parse this workflow (every run failed with empty jobs).
102 version_at() {
103 local ref="$1"
104 local path="$2"
105 # Fail closed: do not swallow helper/parse errors with || true.
106 # Callers only invoke this after git cat-file confirms the blob.
107 git show "${ref}:${path}" \
108 | python3 .github/scripts/read_manifest_version.py "${path}"
109 }
110
111 VERSION_PATHS=(
112 pyproject.toml
113 uv.lock
114 skills/last30days/SKILL.md
115 .claude-plugin/plugin.json
116 .claude-plugin/marketplace.json
117 .codex-plugin/plugin.json
118 .grok-plugin/plugin.json
119 .grok-plugin/marketplace.json
120 gemini-extension.json
121 mcp/manifest.json
122 )
123
124 bumps=()
125 for path in "${VERSION_PATHS[@]}"; do
126 # Only compare when the file exists on both sides.
127 if ! git cat-file -e "${BASE_SHA}:${path}" 2>/dev/null; then
128 continue
129 fi
130 if ! git cat-file -e "${HEAD_SHA}:${path}" 2>/dev/null; then
131 continue
132 fi
133 base_v="$(version_at "${BASE_SHA}" "${path}")"
134 head_v="$(version_at "${HEAD_SHA}" "${path}")"
135 if [ -n "${base_v}" ] && [ -n "${head_v}" ] && [ "${base_v}" != "${head_v}" ]; then
136 bumps+=("${path}: ${base_v} → ${head_v}")
137 fi
138 done
139
140 if [ "${#bumps[@]}" -gt 0 ]; then
141 echo "::error::Non-release PRs must not bump lockstep version strings."
142 printf ' - %s\n' "${bumps[@]}"
143 echo "Run Actions → Prepare release to cut a version bump PR."
144 exit 1
145 fi
146
147 has_fragment=0
148 for path in "${CHANGED[@]}"; do
149 case "${path}" in
150 changelog.d/*.md)
151 base="$(basename "${path}")"
152 if [ "${base}" != "README.md" ]; then
153 has_fragment=1
154 fi
155 ;;
156 esac
157 done
158
159 touches_engine=0
160 for path in "${CHANGED[@]}"; do
161 case "${path}" in
162 skills/last30days/scripts/*|skills/last30days/SKILL.md|skills/last30days/references/*|mcp/*)
163 touches_engine=1
164 ;;
165 esac
166 done
167
168 if [ "${touches_engine}" -eq 1 ] && [ "${has_fragment}" -eq 0 ] && [ "${SKIP_CHANGELOG}" -eq 0 ]; then
169 echo "::error::Engine/skill changes need a changelog.d fragment (or the skip-changelog label)."
170 echo "See changelog.d/README.md"
171 exit 1
172 fi
173
174 echo "Changelog guard passed."
175
175 lines YAML