Skip to content

Commit b644dac

Browse files
authored
CI: extract changeset guard logic into scripts/changeset-guard.sh (#1244)
The changeset guard added in #1205 kept its bash inline in the workflow's run: block. Extract it into scripts/changeset-guard.sh (invoked via run: ./scripts/changeset-guard.sh) so the logic can be linted, tested, and run locally instead of living inside YAML.
1 parent 2fd2656 commit b644dac

2 files changed

Lines changed: 56 additions & 46 deletions

File tree

.github/workflows/changeset-guard.yaml

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,4 @@ jobs:
1616
- name: Require a @gitbook/cli changeset when @gitbook/api is bumped
1717
env:
1818
BASE_SHA: ${{ github.event.pull_request.base.sha }}
19-
run: |
20-
set -euo pipefail
21-
22-
api_re="^['\"]?@gitbook/api['\"]?[[:space:]]*:"
23-
cli_re="^['\"]?@gitbook/cli['\"]?[[:space:]]*:"
24-
25-
# Changeset files introduced by THIS PR (added or modified), excluding the README.
26-
pr_changesets=$(git diff --name-only --diff-filter=AM "$BASE_SHA...HEAD" -- '.changeset/*.md' \
27-
| grep -vi '/README\.md$' || true)
28-
29-
if [ -z "$pr_changesets" ]; then
30-
echo "No changeset changes in this PR — nothing to check."
31-
exit 0
32-
fi
33-
34-
# Does this PR introduce a @gitbook/api bump?
35-
api_bumped=false
36-
while IFS= read -r f; do
37-
[ -n "$f" ] && [ -f "$f" ] || continue
38-
if grep -Eq "$api_re" "$f"; then api_bumped=true; fi
39-
done <<< "$pr_changesets"
40-
41-
if [ "$api_bumped" = false ]; then
42-
echo "This PR does not bump @gitbook/api — nothing to enforce."
43-
exit 0
44-
fi
45-
46-
# Is there ANY pending @gitbook/cli changeset (from this PR or already on the branch)?
47-
# The release batches all pending changesets, so a @gitbook/cli bump anywhere counts.
48-
cli_bumped=false
49-
for f in .changeset/*.md; do
50-
[ -f "$f" ] || continue
51-
case "$f" in */README.md) continue ;; esac
52-
if grep -Eq "$cli_re" "$f"; then cli_bumped=true; fi
53-
done
54-
55-
if [ "$cli_bumped" = false ]; then
56-
echo "::error::This PR bumps @gitbook/api but no pending changeset bumps @gitbook/cli."
57-
echo "The gitbook CLI generates its commands from the API's OpenAPI spec, so an"
58-
echo "@gitbook/api bump should normally also republish the CLI. Add"
59-
echo "\"'@gitbook/cli': patch\" to your changeset (or add a second .changeset/*.md)"
60-
echo "so the CLI is regenerated and released with the new spec."
61-
exit 1
62-
fi
63-
64-
echo "Changeset guard passed (a @gitbook/cli changeset is present)."
19+
run: ./scripts/changeset-guard.sh

scripts/changeset-guard.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
# Require a @gitbook/cli changeset when @gitbook/api is bumped.
4+
#
5+
# The gitbook CLI generates its commands from the API's OpenAPI spec, so an
6+
# @gitbook/api bump should normally also republish the CLI. This guard fails the
7+
# PR if it bumps @gitbook/api without any pending @gitbook/cli changeset.
8+
#
9+
# Expects BASE_SHA in the environment (the PR base commit to diff against).
10+
11+
set -euo pipefail
12+
13+
api_re="^['\"]?@gitbook/api['\"]?[[:space:]]*:"
14+
cli_re="^['\"]?@gitbook/cli['\"]?[[:space:]]*:"
15+
16+
# Changeset files introduced by THIS PR (added or modified), excluding the README.
17+
pr_changesets=$(git diff --name-only --diff-filter=AM "$BASE_SHA...HEAD" -- '.changeset/*.md' \
18+
| grep -vi '/README\.md$' || true)
19+
20+
if [ -z "$pr_changesets" ]; then
21+
echo "No changeset changes in this PR — nothing to check."
22+
exit 0
23+
fi
24+
25+
# Does this PR introduce a @gitbook/api bump?
26+
api_bumped=false
27+
while IFS= read -r f; do
28+
[ -n "$f" ] && [ -f "$f" ] || continue
29+
if grep -Eq "$api_re" "$f"; then api_bumped=true; fi
30+
done <<< "$pr_changesets"
31+
32+
if [ "$api_bumped" = false ]; then
33+
echo "This PR does not bump @gitbook/api — nothing to enforce."
34+
exit 0
35+
fi
36+
37+
# Is there ANY pending @gitbook/cli changeset (from this PR or already on the branch)?
38+
# The release batches all pending changesets, so a @gitbook/cli bump anywhere counts.
39+
cli_bumped=false
40+
for f in .changeset/*.md; do
41+
[ -f "$f" ] || continue
42+
case "$f" in */README.md) continue ;; esac
43+
if grep -Eq "$cli_re" "$f"; then cli_bumped=true; fi
44+
done
45+
46+
if [ "$cli_bumped" = false ]; then
47+
echo "::error::This PR bumps @gitbook/api but no pending changeset bumps @gitbook/cli."
48+
echo "The gitbook CLI generates its commands from the API's OpenAPI spec, so an"
49+
echo "@gitbook/api bump should normally also republish the CLI. Add"
50+
echo "\"'@gitbook/cli': patch\" to your changeset (or add a second .changeset/*.md)"
51+
echo "so the CLI is regenerated and released with the new spec."
52+
exit 1
53+
fi
54+
55+
echo "Changeset guard passed (a @gitbook/cli changeset is present)."

0 commit comments

Comments
 (0)