Repository navigation
190 lines (165 loc) · 7.17 KB
/
Copy pathchangeset-check.yml
File metadata and controls
190 lines (165 loc) · 7.17 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
# Changeset Enforcement Action
#
# This workflow ensures that when developers make changes to package code,
# they include a "changeset" file that describes what changed and how it
# should affect the version number (major/minor/patch).
#
# Changesets are used by the @changesets/cli tool to automatically:
# - Generate changelogs
# - Determine version bumps
# - Coordinate releases across multiple packages in this monorepo
#
# The check can be bypassed by adding the "skip-changeset" label to a PR
# (useful for docs-only changes, CI fixes, etc. that don't need version bumps)
name: Changeset Check
on:
pull_request:
jobs:
changeset-check:
name: Check for Changeset
runs-on: ubuntu-latest
# Skip draft PRs - only run on PRs ready for review
if: github.event.pull_request.draft == false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Need full history to compare against base branch
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
cache: "npm"
- name: Install dependencies
run: npm ci
# Check if PR has the skip-changeset label to bypass this check
- name: Check for opt-out label
id: check-labels
uses: actions/github-script@v7
with:
script: |
const labels = context.payload.pull_request.labels.map(label => label.name);
const hasOptOut = labels.includes('skip-changeset');
core.setOutput('skip-changeset', hasOptOut);
if (hasOptOut) {
core.info('Changeset check skipped due to skip-changeset label');
}
# Use changesets CLI to check if changeset is needed for this PR
- name: Check changeset status
id: changeset-check
if: steps.check-labels.outputs.skip-changeset != 'true'
run: |
# First check what packages have changed
echo "Checking for package changes..."
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
PACKAGE_CHANGES=$(echo "$CHANGED_FILES" | grep -E "(packages/.*/src/|packages/.*/package\.json)" || true)
if [ -z "$PACKAGE_CHANGES" ]; then
echo "status=no-changes" >> $GITHUB_OUTPUT
echo "ℹ️ No package changes detected, changeset not required"
exit 0
fi
echo "📦 Package changes detected:"
echo "$PACKAGE_CHANGES"
# Now check changeset status - capture both output and exit code
echo "Checking changeset status..."
set +e # Don't exit on error
npx changeset status --since=origin/${{ github.base_ref }} --verbose > changeset-output.txt 2>&1
EXIT_CODE=$?
set -e # Re-enable exit on error
echo "Changeset status output:"
cat changeset-output.txt
if [ $EXIT_CODE -eq 0 ]; then
echo "status=valid" >> $GITHUB_OUTPUT
echo "✅ Changeset requirements satisfied"
else
echo "status=missing" >> $GITHUB_OUTPUT
echo "❌ Missing changeset for package changes"
fi
# Clean up old changeset comments to avoid PR pollution
- name: Clean up old comments
uses: actions/github-script@v7
with:
script: |
// Find and delete old changeset comments from this bot
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const botComments = comments.data.filter(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('📦 Changeset Required')
);
for (const comment of botComments) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
console.log(`Deleted old changeset comment: ${comment.id}`);
}
# Add a helpful comment when changeset is missing
- name: Comment on PR - Missing Changeset
if: |
steps.check-labels.outputs.skip-changeset != 'true' &&
steps.changeset-check.outputs.status == 'missing'
uses: actions/github-script@v7
with:
script: |
const comment = `## 📦 Changeset Required
This PR appears to modify package code but doesn't include a changeset.
A changeset helps track version changes and generate release notes.
### To add a changeset:
\`\`\`bash
npm run changeset
\`\`\`
### To skip this check (if no version bump is needed):
Add the \`skip-changeset\` label to this PR`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
# Determine final status and fail the check if needed
- name: Set final status
id: final-status
run: |
if [ "${{ steps.check-labels.outputs.skip-changeset }}" = "true" ]; then
echo "status=skipped" >> $GITHUB_OUTPUT
echo "✅ Changeset check skipped (skip-changeset label detected)"
elif [ "${{ steps.changeset-check.outputs.status }}" = "no-changes" ]; then
echo "status=not-required" >> $GITHUB_OUTPUT
echo "✅ Changeset not required (no package changes)"
elif [ "${{ steps.changeset-check.outputs.status }}" = "valid" ]; then
echo "status=success" >> $GITHUB_OUTPUT
echo "✅ Changeset requirements satisfied"
else
echo "status=missing" >> $GITHUB_OUTPUT
echo "❌ Missing required changeset"
exit 1
fi
# Create a summary for the GitHub Actions UI
- name: Summary
run: |
case "${{ steps.final-status.outputs.status }}" in
"skipped")
echo "### ✅ Changeset Check: Skipped" >> $GITHUB_STEP_SUMMARY
echo "The changeset requirement was bypassed using the skip-changeset label." >> $GITHUB_STEP_SUMMARY
;;
"not-required")
echo "### ✅ Changeset Check: Not Required" >> $GITHUB_STEP_SUMMARY
echo "No package changes detected, so no changeset is needed." >> $GITHUB_STEP_SUMMARY
;;
"success")
echo "### ✅ Changeset Check: Passed" >> $GITHUB_STEP_SUMMARY
echo "Changeset requirements satisfied for this PR." >> $GITHUB_STEP_SUMMARY
;;
"missing")
echo "### ❌ Changeset Check: Failed" >> $GITHUB_STEP_SUMMARY
echo "Package changes detected but no valid changeset found." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**To fix:** Run \`npm run changeset\` or add the \`skip-changeset\` label if no version bump is needed." >> $GITHUB_STEP_SUMMARY
;;
esac