-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (154 loc) · 6.41 KB
/
pr-version-check.yml
File metadata and controls
168 lines (154 loc) · 6.41 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
name: PR Version Check
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
pull-requests: write
contents: read
jobs:
version-check:
name: Check Version Bump
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
- name: Install mise
uses: jdx/mise-action@v2
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Determine version bump
id: version
run: |
set +e
CURRENT_VERSION=$(node -p "require('./package.json').version")
# Set up tracking branch so release-it doesn't complain about upstream
git branch --set-upstream-to=origin/${{ github.event.pull_request.head.ref }} 2>/dev/null || true
NEXT_VERSION=$(bunx release-it --release-version --ci 2>&1)
EXIT_CODE=$?
set -e
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
if [ $EXIT_CODE -ne 0 ] || [ -z "$NEXT_VERSION" ]; then
echo "error=true" >> $GITHUB_OUTPUT
echo "error_message<<EOF" >> $GITHUB_OUTPUT
echo "$NEXT_VERSION" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "error=false" >> $GITHUB_OUTPUT
echo "next_version=$NEXT_VERSION" >> $GITHUB_OUTPUT
# Generate changelog preview
CHANGELOG=$(bunx release-it --changelog --ci 2>/dev/null || echo "")
echo "changelog<<CHANGELOG_EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "CHANGELOG_EOF" >> $GITHUB_OUTPUT
# Determine bump type by comparing versions
CURRENT_MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1)
CURRENT_MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2)
CURRENT_PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3)
NEXT_MAJOR=$(echo "$NEXT_VERSION" | cut -d. -f1)
NEXT_MINOR=$(echo "$NEXT_VERSION" | cut -d. -f2)
NEXT_PATCH=$(echo "$NEXT_VERSION" | cut -d. -f3)
if [ "$NEXT_MAJOR" != "$CURRENT_MAJOR" ]; then
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [ "$NEXT_MINOR" != "$CURRENT_MINOR" ]; then
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [ "$NEXT_PATCH" != "$CURRENT_PATCH" ]; then
echo "bump_type=patch" >> $GITHUB_OUTPUT
else
echo "bump_type=none" >> $GITHUB_OUTPUT
fi
fi
- name: Comment on PR
uses: actions/github-script@v7
env:
VERSION_CURRENT: ${{ steps.version.outputs.current_version }}
VERSION_NEXT: ${{ steps.version.outputs.next_version }}
VERSION_ERROR: ${{ steps.version.outputs.error }}
VERSION_BUMP_TYPE: ${{ steps.version.outputs.bump_type }}
VERSION_ERROR_MESSAGE: ${{ steps.version.outputs.error_message }}
VERSION_CHANGELOG: ${{ steps.version.outputs.changelog }}
with:
script: |
const currentVersion = process.env.VERSION_CURRENT;
const hasError = process.env.VERSION_ERROR === 'true';
const nextVersion = process.env.VERSION_NEXT;
const bumpType = process.env.VERSION_BUMP_TYPE;
const errorMessage = process.env.VERSION_ERROR_MESSAGE || '';
const changelog = process.env.VERSION_CHANGELOG || '';
let body;
if (hasError) {
body = [
'## Release Version Check',
'',
'> **Error determining version bump**',
'',
`Current version: \`${currentVersion}\``,
'',
'Error details:',
'```',
errorMessage,
'```',
'',
'This PR may not trigger an automatic release when merged. Ensure commits follow [Conventional Commits](https://www.conventionalcommits.org/) format.',
].join('\n');
} else if (bumpType === 'none' || !nextVersion) {
body = [
'## Release Version Check',
'',
`> **No version bump detected**`,
'',
`Current version: \`${currentVersion}\``,
'',
'This PR will **not** trigger an automatic release when merged.',
'To trigger a release, use [Conventional Commits](https://www.conventionalcommits.org/) prefixes like `feat:`, `fix:`, etc.',
].join('\n');
} else {
const bumpEmoji = { major: '🔴', minor: '🟡', patch: '🟢' }[bumpType] || '📦';
const parts = [
'## Release Version Check',
'',
`> ${bumpEmoji} **${bumpType.toUpperCase()}** version bump`,
'',
`| | Version |`,
`|---|---|`,
`| Current | \`${currentVersion}\` |`,
`| Next | \`${nextVersion}\` |`,
'',
'This version will be released automatically when this PR is merged to `main`.',
];
if (changelog && changelog.trim()) {
parts.push(
'',
'<details>',
'<summary>Changelog preview</summary>',
'',
changelog.trim(),
'',
'</details>',
);
}
body = parts.join('\n');
}
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
});
const existing = comments.find(c => c.body.includes('Release Version Check'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body,
});
}