Skip to content

Commit 0f8acf3

Browse files
committed
refactor: validate update target inputs and sync nightly tag source
1 parent e51a34e commit 0f8acf3

3 files changed

Lines changed: 66 additions & 19 deletions

File tree

.github/workflows/release.yml

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,37 @@ on:
2020
permissions:
2121
contents: write
2222

23-
env:
24-
NIGHTLY_TAG: nightly
25-
2623
jobs:
2724
resolve-release-context:
2825
name: Resolve Release Context
2926
runs-on: ubuntu-24.04
3027
outputs:
3128
checkout_ref: ${{ steps.checkout-ref.outputs.ref }}
3229
tag: ${{ steps.tag.outputs.tag }}
30+
nightly_tag: ${{ steps.nightly-tag.outputs.nightly_tag }}
3331
steps:
32+
- name: Checkout repository for constants
33+
uses: actions/checkout@v6
34+
with:
35+
fetch-depth: 1
36+
37+
- name: Resolve nightly tag
38+
id: nightly-tag
39+
shell: bash
40+
run: |
41+
nightly_tag="$(python3 -c 'from astrbot.core.release_constants import NIGHTLY_TAG; print(NIGHTLY_TAG)')"
42+
if [ -z "$nightly_tag" ]; then
43+
echo "Failed to resolve NIGHTLY_TAG from astrbot.core.release_constants." >&2
44+
exit 1
45+
fi
46+
echo "nightly_tag=$nightly_tag" >> "$GITHUB_OUTPUT"
47+
3448
- name: Resolve checkout ref
3549
id: checkout-ref
3650
shell: bash
3751
run: |
3852
event_name="${{ github.event_name }}"
53+
nightly_tag="${{ steps.nightly-tag.outputs.nightly_tag }}"
3954
input_tag=""
4055
input_ref=""
4156
if [ "$event_name" = "workflow_dispatch" ]; then
@@ -47,7 +62,7 @@ jobs:
4762
default_branch="master"
4863
fi
4964
50-
if [ "$event_name" = "schedule" ] || { [ "$event_name" = "workflow_dispatch" ] && [ "$input_tag" = "$NIGHTLY_TAG" ]; }; then
65+
if [ "$event_name" = "schedule" ] || { [ "$event_name" = "workflow_dispatch" ] && [ "$input_tag" = "$nightly_tag" ]; }; then
5166
ref="refs/heads/${default_branch}"
5267
elif [ -n "$input_ref" ]; then
5368
ref="$input_ref"
@@ -57,7 +72,7 @@ jobs:
5772
5873
echo "ref=$ref" >> "$GITHUB_OUTPUT"
5974
60-
- name: Checkout repository
75+
- name: Checkout repository at release ref
6176
uses: actions/checkout@v6
6277
with:
6378
fetch-depth: 0
@@ -68,13 +83,14 @@ jobs:
6883
shell: bash
6984
run: |
7085
event_name="${{ github.event_name }}"
86+
nightly_tag="${{ steps.nightly-tag.outputs.nightly_tag }}"
7187
dispatch_tag=""
7288
if [ "$event_name" = "workflow_dispatch" ]; then
7389
dispatch_tag="$(jq -r '.inputs.tag // ""' "$GITHUB_EVENT_PATH")"
7490
fi
7591
7692
if [ "$event_name" = "schedule" ]; then
77-
tag="$NIGHTLY_TAG"
93+
tag="$nightly_tag"
7894
elif [ "$event_name" = "push" ]; then
7995
tag="${GITHUB_REF_NAME}"
8096
elif [ -n "$dispatch_tag" ]; then
@@ -162,7 +178,7 @@ jobs:
162178
path: dashboard/AstrBot-${{ needs.resolve-release-context.outputs.tag }}-dashboard.zip
163179

164180
- name: Upload dashboard package to Cloudflare R2
165-
if: ${{ env.R2_ACCOUNT_ID != '' && env.R2_ACCESS_KEY_ID != '' && env.R2_SECRET_ACCESS_KEY != '' && needs.resolve-release-context.outputs.tag != env.NIGHTLY_TAG }}
181+
if: ${{ env.R2_ACCOUNT_ID != '' && env.R2_ACCESS_KEY_ID != '' && env.R2_SECRET_ACCESS_KEY != '' && needs.resolve-release-context.outputs.tag != needs.resolve-release-context.outputs.nightly_tag }}
166182
env:
167183
R2_BUCKET_NAME: "astrbot"
168184
R2_OBJECT_NAME: "astrbot-webui-latest.zip"
@@ -191,8 +207,8 @@ jobs:
191207
name: Publish GitHub Release
192208
runs-on: ubuntu-24.04
193209
concurrency:
194-
group: ${{ needs.resolve-release-context.outputs.tag == env.NIGHTLY_TAG && 'nightly-release' || format('release-{0}', needs.resolve-release-context.outputs.tag) }}
195-
cancel-in-progress: ${{ needs.resolve-release-context.outputs.tag == env.NIGHTLY_TAG }}
210+
group: ${{ needs.resolve-release-context.outputs.tag == needs.resolve-release-context.outputs.nightly_tag && 'nightly-release' || format('release-{0}', needs.resolve-release-context.outputs.tag) }}
211+
cancel-in-progress: ${{ needs.resolve-release-context.outputs.tag == needs.resolve-release-context.outputs.nightly_tag }}
196212
needs:
197213
- resolve-release-context
198214
- verify-core
@@ -209,7 +225,8 @@ jobs:
209225
shell: bash
210226
run: |
211227
tag="${{ needs.resolve-release-context.outputs.tag }}"
212-
if [ "$tag" = "$NIGHTLY_TAG" ]; then
228+
nightly_tag="${{ needs.resolve-release-context.outputs.nightly_tag }}"
229+
if [ "$tag" = "$nightly_tag" ]; then
213230
if ! short_sha="$(git rev-parse --short=8 HEAD 2>/tmp/git_rev_parse_error.log)"; then
214231
echo "Failed to resolve HEAD short SHA for nightly title." >&2
215232
cat /tmp/git_rev_parse_error.log >&2 || true
@@ -219,7 +236,7 @@ jobs:
219236
if [ -z "$base_version" ]; then
220237
base_version="v0.0.0"
221238
fi
222-
title="${base_version}-${NIGHTLY_TAG}-${short_sha}"
239+
title="${base_version}-${nightly_tag}-${short_sha}"
223240
else
224241
base_version="$tag"
225242
title="$tag"
@@ -228,18 +245,19 @@ jobs:
228245
echo "base_version=$base_version" >> "$GITHUB_OUTPUT"
229246
230247
- name: Force-update nightly tag
231-
if: ${{ needs.resolve-release-context.outputs.tag == env.NIGHTLY_TAG }}
248+
if: ${{ needs.resolve-release-context.outputs.tag == needs.resolve-release-context.outputs.nightly_tag }}
232249
env:
233250
GH_TOKEN: ${{ github.token }}
234251
shell: bash
235252
run: |
253+
nightly_tag="${{ needs.resolve-release-context.outputs.nightly_tag }}"
236254
if ! current_sha="$(git rev-parse HEAD 2>/tmp/git_rev_parse_error.log)"; then
237255
echo "Failed to resolve HEAD SHA before updating nightly tag." >&2
238256
cat /tmp/git_rev_parse_error.log >&2 || true
239257
exit 1
240258
fi
241-
git tag -f "$NIGHTLY_TAG" "${current_sha}"
242-
git push --force origin "refs/tags/${NIGHTLY_TAG}"
259+
git tag -f "$nightly_tag" "${current_sha}"
260+
git push --force origin "refs/tags/${nightly_tag}"
243261
244262
- name: Download dashboard artifact
245263
uses: actions/download-artifact@v8
@@ -253,7 +271,8 @@ jobs:
253271
shell: bash
254272
run: |
255273
tag="${{ needs.resolve-release-context.outputs.tag }}"
256-
if [ "$tag" = "$NIGHTLY_TAG" ]; then
274+
nightly_tag="${{ needs.resolve-release-context.outputs.nightly_tag }}"
275+
if [ "$tag" = "$nightly_tag" ]; then
257276
note_file="$(mktemp)"
258277
python3 scripts/release/generate_nightly_release_notes.py \
259278
--base-tag "${{ steps.release-meta.outputs.base_version }}" \
@@ -275,14 +294,15 @@ jobs:
275294
run: |
276295
tag="${{ needs.resolve-release-context.outputs.tag }}"
277296
title="${{ steps.release-meta.outputs.title }}"
278-
if [ "$tag" = "$NIGHTLY_TAG" ]; then
297+
nightly_tag="${{ needs.resolve-release-context.outputs.nightly_tag }}"
298+
if [ "$tag" = "$nightly_tag" ]; then
279299
pre_flag="--prerelease"
280300
else
281301
pre_flag=""
282302
fi
283303
if ! gh release view "$tag" >/dev/null 2>&1; then
284304
gh release create "$tag" --title "$title" --notes-file "${{ steps.notes.outputs.file }}" $pre_flag
285-
elif [ "$tag" = "$NIGHTLY_TAG" ]; then
305+
elif [ "$tag" = "$nightly_tag" ]; then
286306
gh release edit "$tag" --title "$title" --notes-file "${{ steps.notes.outputs.file }}" --prerelease
287307
fi
288308
@@ -310,7 +330,7 @@ jobs:
310330
311331
publish-pypi:
312332
name: Publish PyPI
313-
if: ${{ needs.resolve-release-context.outputs.tag != env.NIGHTLY_TAG }}
333+
if: ${{ needs.resolve-release-context.outputs.tag != needs.resolve-release-context.outputs.nightly_tag }}
314334
runs-on: ubuntu-24.04
315335
needs:
316336
- resolve-release-context

astrbot/core/updator.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,21 @@ async def _resolve_update_target(
208208
latest: bool,
209209
version: str | None,
210210
) -> tuple[str, str]:
211+
"""Resolve target version and download URL.
212+
213+
Supported combinations:
214+
- latest=True, version="" => latest stable release
215+
- latest=False, version="nightly" => nightly release
216+
- latest=False, version="v*" => explicit tag release
217+
- latest=False, version="<40-char commit>" => commit archive
218+
"""
211219
version_str = str(version).strip() if version is not None else ""
212220

221+
if latest and version_str:
222+
raise Exception(
223+
"latest=True 时不能同时指定 version,请将 latest 设为 False。"
224+
)
225+
213226
if (
214227
not latest
215228
and version_str
@@ -220,7 +233,7 @@ async def _resolve_update_target(
220233
raise Exception("commit hash 长度不正确,应为 40")
221234
return version_str, f"{self.GITHUB_ARCHIVE_BASE}/{version_str}.zip"
222235

223-
include_nightly = version_str.lower() == self.NIGHTLY_TAG
236+
include_nightly = (not latest) and version_str.lower() == self.NIGHTLY_TAG
224237
releases = await self._fetch_all_releases(include_nightly=include_nightly)
225238

226239
if latest:

tests/unit/test_updator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ async def mock_get_releases():
302302
assert file_url == "https://example.com/stable.zip"
303303

304304

305+
@pytest.mark.asyncio
306+
async def test_resolve_update_target_rejects_version_when_latest_true():
307+
updator = AstrBotUpdator()
308+
309+
with pytest.raises(
310+
Exception,
311+
match="latest=True 时不能同时指定 version,请将 latest 设为 False。",
312+
):
313+
await updator._resolve_update_target(
314+
latest=True,
315+
version="nightly",
316+
)
317+
318+
305319
@pytest.mark.asyncio
306320
async def test_get_nightly_release_returns_none_for_expected_fetch_error(monkeypatch):
307321
updator = AstrBotUpdator()

0 commit comments

Comments
 (0)