Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(release): 接入 GitGud 原生验收与 R2 制品发布

longmeidaoa747b6fc680c
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 000000000..133e70aab
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,182 @@
+workflow:
+ rules:
+ - if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/ && $CI_COMMIT_REF_PROTECTED == "true"'
+ - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
+ - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
+ when: never
+ - if: '$CI_COMMIT_BRANCH'
+
+stages: [verify, package, validate, media, build, smoke, publish]
+
+default:
+ tags: [docker]
+
+variables:
+ GIT_DEPTH: "0"
+ PYTHONUTF8: "1"
+ PYTHONIOENCODING: utf-8
+ UV_LINK_MODE: copy
+ UV_CACHE_DIR: "$CI_PROJECT_DIR/.uv-cache"
+ npm_config_cache: "$CI_PROJECT_DIR/.npm"
+ npm_config_fetch_retries: "5"
+ npm_config_fetch_retry_mintimeout: "10000"
+ npm_config_fetch_retry_maxtimeout: "60000"
+ PEACH_DOWNLOAD_BACKEND: r2
+
+verify-linux:
+ stage: verify
+ image: node:24-bookworm
+ rules:
+ - if: '$CI_COMMIT_BRANCH || $CI_PIPELINE_SOURCE == "merge_request_event"'
+ cache:
+ key:
+ files: [uv.lock, frontend/package-lock.json]
+ paths: [.uv-cache/, .npm/]
+ before_script:
+ - apt-get update
+ - apt-get install -y --no-install-recommends python3-pip chromium ffmpeg
+ - python3 -m pip install --break-system-packages uv==0.13.3
+ - uv sync --locked --all-extras --python 3.14
+ - npm ci --ignore-scripts --prefer-offline
+ - npm --prefix frontend ci --prefer-offline
+ - export PEACH_E2E_CHROME=/usr/bin/chromium
+ - test -x "$PEACH_E2E_CHROME"
+ - ffmpeg -version
+ script:
+ - npm run check:vendor
+ - npm --prefix frontend run build
+ - test -z "$(git status --porcelain --untracked-files=all -- web/dist)"
+ - uv run --locked python scripts/npm_audit.py
+ - uv run --locked python scripts/npm_audit.py --prefix frontend
+ - ./scripts/test.sh full --fresh
+ artifacts:
+ when: always
+ expire_in: 14 days
+ paths:
+ - build/agent-verification/browser/*.tap
+ - build/agent-verification/browser/serve.log
+ - build/agent-verification/browser/setup-serve.log
+
+verify-windows:
+ stage: verify
+ tags: [peach-windows]
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ script:
+ - '& ./scripts/ci_windows.ps1 verify'
+ - if ($LASTEXITCODE -ne 0) { throw 'Windows 全量回归未通过' }
+
+verify-macos:
+ stage: verify
+ tags: [peach-macos]
+ rules:
+ - if: '$CI_COMMIT_BRANCH == "master"'
+ script:
+ - bash scripts/ci_macos.sh verify arm64
+
+wheel-smoke:
+ stage: package
+ image: ghcr.io/astral-sh/uv:0.13.3-python3.14-trixie-slim
+ rules:
+ - if: '$CI_COMMIT_BRANCH || $CI_PIPELINE_SOURCE == "merge_request_event"'
+ before_script:
+ - apt-get update
+ - apt-get install -y --no-install-recommends git
+ script:
+ - uv lock --check
+ - uv build --wheel --out-dir build/wheelhouse
+ - uv venv .wheel-smoke --python 3.14
+ - uv pip install --python .wheel-smoke/bin/python build/wheelhouse/*.whl
+ - uv pip check --python .wheel-smoke/bin/python
+ - |
+ cd "$(mktemp -d)"
+ "$CI_PROJECT_DIR/.wheel-smoke/bin/python" "$CI_PROJECT_DIR/scripts/smoke_wheel.py"
+ cd "$CI_PROJECT_DIR"
+ sha256sum build/wheelhouse/*.whl > build/wheelhouse/SHA256SUMS.txt
+ artifacts:
+ expire_in: 14 days
+ paths: [build/wheelhouse/]
+
+.release-linux:
+ image: node:24-bookworm
+ rules:
+ - if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/ && $CI_COMMIT_REF_PROTECTED == "true"'
+ before_script:
+ - apt-get update
+ - apt-get install -y --no-install-recommends python3 ca-certificates git
+ - python3 scripts/fetch_release_tools.py
+ - export PATH="$CI_PROJECT_DIR/build/release-tools:$PATH"
+ - export GLAB_ENABLE_CI_AUTOLOGIN=true
+
+validate-release:
+ extends: .release-linux
+ stage: validate
+ script:
+ - python3 scripts/release_tag.py --gitlab-host "$CI_SERVER_HOST" --repo "$CI_PROJECT_PATH" --verify-sha "$CI_COMMIT_SHA"
+ - python3 -c 'import os; from pathlib import Path; from scripts.version_bump import read_version; assert os.environ["CI_COMMIT_TAG"] == "v" + read_version(Path.cwd())'
+ - python3 scripts/changelog.py --notes "${CI_COMMIT_TAG#v}" --to build/release-notes.md
+
+windows-media:
+ extends: .release-linux
+ stage: media
+ script:
+ - apt-get install -y build-essential gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix mingw-w64-x86-64-dev nasm pkg-config clang meson ninja-build curl xz-utils
+ - bash scripts/build_ffmpeg_windows.sh build/ffmpeg-windows
+ - bash scripts/build_untrunc_windows.sh build/untrunc-windows
+ artifacts:
+ expire_in: 14 days
+ paths:
+ - build/ffmpeg-windows/ffmpeg/
+ - build/ffmpeg-windows/ffmpeg-corresponding-source.zip
+ - build/untrunc-windows/untrunc/
+ - build/untrunc-windows/untrunc-corresponding-source.zip
+
+build-windows:
+ stage: build
+ tags: [peach-windows]
+ rules: !reference [.release-linux, rules]
+ needs: [windows-media]
+ script:
+ - '& ./scripts/ci_windows.ps1 build'
+ - if ($LASTEXITCODE -ne 0) { throw 'Windows 构建失败' }
+ artifacts:
+ expire_in: 14 days
+ paths: [release/]
+
+build-macos:
+ stage: build
+ tags: [peach-macos]
+ rules: !reference [.release-linux, rules]
+ needs: [validate-release]
+ script:
+ - bash scripts/ci_macos.sh build arm64
+ - bash scripts/ci_macos.sh build x86_64
+ artifacts:
+ expire_in: 14 days
+ paths: [dist/macos.noindex/]
+
+smoke-windows:
+ stage: smoke
+ tags: [peach-windows]
+ rules: !reference [.release-linux, rules]
+ needs: [build-windows]
+ variables:
+ GIT_STRATEGY: empty
+ script:
+ - '& ./release/ci_windows.ps1 smoke'
+ - if ($LASTEXITCODE -ne 0) { throw 'Windows 独立制品验收失败' }
+
+publish-release:
+ extends: .release-linux
+ stage: publish
+ needs: [build-windows, smoke-windows, build-macos]
+ resource_group: peach-release-publication
+ environment:
+ name: release
+ url: https://downloads.peach.video
+ variables:
+ PEACH_R2_BUCKET: peach-releases
+ PEACH_R2_PUBLIC_BASE: https://downloads.peach.video
+ script:
+ - cp dist/macos.noindex/arm64/*.dmg dist/macos.noindex/x86_64/*.dmg release/
+ - python3 scripts/publish_gitlab_release.py
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md
index 2f95a7d80..2a21a9a78 100644
--- a/docs/OPERATIONS.md
+++ b/docs/OPERATIONS.md
@@ -45,6 +45,20 @@ MACOSX_DEPLOYMENT_TARGET=11.0 uv sync --locked --extra build --extra macos
版本标签触发 Release 工作流,在 Apple Silicon 与 Intel 原生 runner 分别构建,再由干净 runner 下载并挂载 DMG,验证签名、架构和独立运行。两种 DMG 与 Windows 安装包全部通过后,才一同上传到 GitHub 预发布,统一生成 SHA-256 清单。手动运行工作流只验收候选制品,不创建 Release;推送普通提交不会触发安装包构建。发版检查见[测试与依赖](TESTING.md#每次发版检查),打标签统一使用 `scripts/release_tag.py`。
+### GitLab 发布与下载后端
+
+GitLab 兼容平台使用 `.gitlab-ci.yml`。唯一版本入口支持 `--gitlab-host` 指定主机,默认入口使用 GitHub。主线发布判据要求同一提交最新的 push 流水线成功,且 Linux、Windows、macOS 和 wheel 验收任务全部成功;缺少原生系统任务时拒绝打标签。CLI 传输复用官方 glab,不在仓库或命令参数里存访问令牌。
+
+平台共享 Runner 的系统由管理员提供。注册页面列出 Windows 和 macOS,表示可以接入这些系统上的 Runner;不能据此判断平台提供相应云主机。临时原生 Runner 只接入受保护主线和版本标签,使用项目专属标签,验收后停止并注销。Mac 的 Intel 构建需要真实 x86_64 Python 和依赖;在 Apple Silicon 上通过 Rosetta 验收时,应明确记录未覆盖 Intel 真机。
+
+版本标签须受保护。标签流水线重新核对提交与版本,构建固定来源的媒体工具、Windows 完整包与拆分包、macOS 双架构 DMG,并进行独立制品验收。只有全部成功后才创建 GitLab Release。`PEACH_DOWNLOAD_BACKEND` 默认 `gitgud`,通过 Generic Package Registry 保存制品;不把会过期的 job artifacts 当作永久下载地址。
+
+可选的 `r2` 后端使用独立 Standard 桶与 HTTPS 自定义域名。`PEACH_R2_ACCESS_KEY_ID`、`PEACH_R2_SECRET_ACCESS_KEY`、`PEACH_R2_ENDPOINT` 放入受保护且隐藏的 CI 变量,环境作用域为 `release`;`PEACH_R2_BUCKET` 和 `PEACH_R2_PUBLIC_BASE` 指定发布目标。S3 传输复用 rclone,版本文件禁止覆盖;每份文件必须从公开入口完整下载并核对大小及 SHA-256 后,才更新公开版本索引和 Release 链接。
+
+R2 公网下载流量免费,存储与请求按账户共享额度计费。预算告警只通知,不会停止服务;R2 后端不提供零费用硬封顶。要求绝不产生按量费用的部署应使用不绑定按量计费的下载服务,不启用 R2 自动发布。
+
+上传前列举本桶现有文件并计算新增制品的总容量;默认超过 8 GB 时停止上传,保留已有文件。`PEACH_R2_MAX_BYTES` 可明确调整该桶预算;此检查不能限制同账户其他桶,也不代替请求量计费保护。版本文件设置长期缓存,公开版本索引只缓存一分钟。
+
Mac 与 Windows 托盘的首层只显示简短状态与提醒数量,展开状态子菜单按行查看服务和媒体来源。长地址与路径会缩略,子菜单中的「查看完整配置」保留完整设置入口;提醒消失后会随下一轮健康检查更新。
单机设置默认关闭账本复制,不需要 Windows、SMB、LaunchAgent 或系统端口转发。各台电脑各自保存数据;共享馆藏仍按下文的[双机复制](#两台电脑之间的复制)配置,离线修改不能自动合并。已有数据目录不会自动导入。
diff --git a/docs/REUSE.md b/docs/REUSE.md
index ee7034647..beaa98344 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -1,5 +1,11 @@
# 复用清单
+## GitLab 与 R2 发布
+
+GitLab API、CI 认证和 Release 创建复用官方 [glab 1.124.0](https://gitlab.com/gitlab-org/cli)(MIT,维护中);GitLab CI 的发布写入使用短期 `CI_JOB_TOKEN` 与官方 CI 自动登录。R2 的 S3 上传复用 [rclone 1.75.2](https://rclone.org/)(MIT,维护中),使用单桶对象读写凭据;版本文件不可覆盖,并由独立 HTTPS 下载校验 SHA-256。两者都是构建期 CLI,不进入应用运行时依赖。
+
+`fetch_release_tools.py` 从固定版本官方发行站读取 SHA-256 清单,仅提取指定二进制。Python 标准库负责制品清单、版本与提交判据,不另实现 GitLab 认证、S3 签名或多段上传协议。Windows 与 macOS 的 CI 入口复用既有测试、构建和独立制品验收脚本。下载后端及费用边界见[运维说明](OPERATIONS.md#gitlab-发布与下载后端)。
+
## 真实样本界面预览
用户验收预览复用项目记忆登记的真实账本小样本和已有缓存。启动入口强制调用 `scripts/check_ui_preview.py`,使用标准库 SQLite 的只读连接核对作品、实体、关联和来源清单,产出样本内容摘要;通用契约见 `docs/UI_PREVIEW.md`。核验与图像查看分别记录,隔离自动化测试夹具保留在测试范围内。没有新增依赖或生产数据写入。
diff --git a/scripts/ci_windows.ps1 b/scripts/ci_windows.ps1
new file mode 100644
index 000000000..29a23fd2f
--- /dev/null
+++ b/scripts/ci_windows.ps1
@@ -0,0 +1,88 @@
+param([ValidateSet('verify', 'build', 'smoke')][string]$Mode = 'verify')
+$ErrorActionPreference = 'Stop'
+
+function Assert-Exit([string]$Message) {
+ if ($LASTEXITCODE -ne 0) { throw $Message }
+}
+
+if ($Mode -eq 'verify' -or $Mode -eq 'build') {
+ uv sync --locked --all-extras --python 3.14
+ Assert-Exit '锁定依赖安装失败'
+ uv pip check --python .venv/Scripts/python.exe
+ Assert-Exit '依赖检查失败'
+ npm ci --ignore-scripts --prefer-offline
+ Assert-Exit '根目录 npm 安装失败'
+ npm --prefix frontend ci --prefer-offline
+ Assert-Exit '前端依赖安装失败'
+}
+
+if ($Mode -eq 'verify') {
+ $env:PEACH_E2E_CHROME = Join-Path $env:ProgramFiles 'Google/Chrome/Application/chrome.exe'
+ & ./scripts/test.ps1 -Scope full -Fresh
+ Assert-Exit 'Windows 全量回归未通过'
+ exit 0
+}
+
+if ($Mode -eq 'build') {
+ $LocalCompiler = Join-Path $env:LOCALAPPDATA 'Programs/Inno Setup 6/ISCC.exe'
+ if (-not $env:PEACH_ISCC -and (Test-Path -LiteralPath $LocalCompiler)) { $env:PEACH_ISCC = $LocalCompiler }
+ $Version = (& .venv/Scripts/python.exe -c 'from peach import __version__; print(__version__)').Trim()
+ Assert-Exit '无法读取发布版本'
+ if ($env:CI_COMMIT_TAG -ne "v$Version") { throw 'Tag 与版本不一致' }
+ $Changes = git status --porcelain --untracked-files=all
+ Assert-Exit '无法核对提交状态'
+ if ($Changes) { throw '构建必须使用干净提交' }
+ & ./scripts/fetch_cloudflared.ps1 -OutputPath build/cloudflared.exe
+ Assert-Exit 'cloudflared 校验失败'
+ & ./scripts/test.ps1 -Scope packaging -Focus test_desktop_installer
+ Assert-Exit '安装目录保护回归失败'
+ & ./scripts/build_windows.ps1 -Installer -CloudflaredPath build/cloudflared.exe -FFmpegPath build/ffmpeg-windows/ffmpeg -UntruncPath build/untrunc-windows/untrunc
+ Assert-Exit 'Windows 构建失败'
+ New-Item -ItemType Directory -Path release -Force | Out-Null
+ & .venv/Scripts/python.exe scripts/package_windows_release.py --source dist/Peach --output release --version $Version --lite-installer
+ Assert-Exit '发行包拆分失败'
+ Copy-Item -LiteralPath "dist/Peach-$Version-windows-x64-setup.exe" -Destination release/
+ Copy-Item -LiteralPath dist/ffmpeg-corresponding-source.zip, dist/untrunc-corresponding-source.zip -Destination release/
+ Copy-Item -LiteralPath scripts/smoke_desktop.py, scripts/smoke_update_tasks.py, scripts/smoke_split_update.py, scripts/ci_windows.ps1 -Destination release/
+ & .venv/Scripts/python.exe scripts/changelog.py --notes $Version --to release/RELEASE_NOTES.md
+ Assert-Exit '缺少发布说明'
+ Add-Content -LiteralPath release/RELEASE_NOTES.md -Value (Get-Content -LiteralPath release/DOWNLOADS.md -Raw -Encoding utf8) -Encoding utf8
+ $Version | Set-Content -LiteralPath release/version.txt -Encoding utf8
+ exit 0
+}
+
+$Version = (Get-Content -LiteralPath release/version.txt -Raw -Encoding utf8).Trim()
+$Packages = (Resolve-Path release).Path
+$Evidence = Join-Path ([IO.Path]::GetTempPath()) ('peach-release-smoke-' + [guid]::NewGuid().ToString('N'))
+New-Item -ItemType Directory -Path $Evidence | Out-Null
+$Python = (Get-Command python.exe).Source
+$Key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{02D9748A-78F6-4866-AC75-918177194DAB}_is1'
+$Menu = Join-Path $env:APPDATA 'Microsoft/Windows/Start Menu/Programs/Peach.lnk'
+if ((Test-Path -LiteralPath $Key) -or (Test-Path -LiteralPath $Menu)) { throw '现有 Peach 安装登记需要保留,不能覆盖安装验收' }
+Push-Location $Evidence
+try {
+ Expand-Archive -LiteralPath "$Packages/Peach-$Version-windows-x64.zip" -DestinationPath candidate
+ & $Python "$Packages/smoke_desktop.py" candidate/Peach/Peach.exe
+ Assert-Exit '完整包运行验收失败'
+ & $Python "$Packages/smoke_split_update.py" --root $Packages --version $Version --destination split-candidate --prepare-with-app
+ Assert-Exit '拆分包重建失败'
+ & $Python "$Packages/smoke_desktop.py" split-candidate/Peach/Peach.exe
+ Assert-Exit '拆分包运行验收失败'
+ foreach ($Suffix in @('-setup.exe', '-lite-setup.exe')) {
+ $Installed = Join-Path $Evidence ('安装验收/自定义目录' + $Suffix)
+ $Setup = Start-Process -FilePath "$Packages/Peach-$Version-windows-x64$Suffix" -ArgumentList @('/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', "/DIR=`"$Installed`"") -WindowStyle Hidden -Wait -PassThru
+ if ($Setup.ExitCode -ne 0) { throw "安装包退出码 $($Setup.ExitCode)" }
+ if (-not (Test-Path -LiteralPath $Key) -or -not (Test-Path -LiteralPath $Menu)) { throw '缺少安装登记或开始菜单项' }
+ & $Python "$Packages/smoke_desktop.py" (Join-Path $Installed 'Peach.exe')
+ Assert-Exit '安装包运行验收失败'
+ Start-Process -FilePath (Join-Path $Installed 'unins000.exe') -ArgumentList @('/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART') -WindowStyle Hidden -Wait
+ $Deadline = (Get-Date).AddSeconds(120)
+ while ((Test-Path -LiteralPath $Installed) -and (Get-Date) -lt $Deadline) { Start-Sleep -Seconds 1 }
+ if ((Test-Path -LiteralPath $Installed) -or (Test-Path -LiteralPath $Key) -or (Test-Path -LiteralPath $Menu)) { throw '卸载验收未通过' }
+ }
+ & $Python "$Packages/smoke_update_tasks.py" candidate/Peach/Peach.exe --installer "$Packages/Peach-$Version-windows-x64-setup.exe" --apply
+ Assert-Exit '任务保护与安装恢复验收失败'
+} finally {
+ Pop-Location
+ Write-Host "独立制品验收目录:$Evidence"
+}
diff --git a/scripts/fetch_release_tools.py b/scripts/fetch_release_tools.py
new file mode 100644
index 000000000..16413efdc
--- /dev/null
+++ b/scripts/fetch_release_tools.py
@@ -0,0 +1,48 @@
+"""从官方发行站下载固定版本 CLI,校验 SHA-256 后提取指定二进制。"""
+from __future__ import annotations
+
+import hashlib
+import io
+from pathlib import Path
+import re
+import tarfile
+from urllib.request import Request, urlopen
+import zipfile
+
+
+TOOLS = (
+ ("glab", "glab_1.124.0_linux_amd64.tar.gz", "https://gitlab.com/gitlab-org/cli/-/releases/v1.124.0/downloads", "checksums.txt", "bin/glab"),
+ ("rclone", "rclone-v1.75.2-linux-amd64.zip", "https://downloads.rclone.org/v1.75.2", "SHA256SUMS", "rclone-v1.75.2-linux-amd64/rclone"),
+)
+
+
+def download(url: str) -> bytes:
+ with urlopen(Request(url, headers={"User-Agent": "Peach-release-tools"}), timeout=120) as response:
+ return response.read()
+
+
+def install(destination: Path) -> None:
+ destination.mkdir(parents=True, exist_ok=True)
+ for name, archive, base, checksums, member in TOOLS:
+ document = download(f"{base}/{checksums}").decode("utf-8")
+ expected = re.search(rf"^([0-9a-fA-F]{{64}})\s+\*?{re.escape(archive)}$", document, re.MULTILINE)
+ content = download(f"{base}/{archive}")
+ if expected is None or hashlib.sha256(content).hexdigest() != expected[1].lower():
+ raise ValueError(f"官方 CLI 的 SHA-256 不符:{name}")
+ if archive.endswith(".zip"):
+ with zipfile.ZipFile(io.BytesIO(content)) as package:
+ binary = package.read(member)
+ else:
+ with tarfile.open(fileobj=io.BytesIO(content), mode="r:gz") as package:
+ source = package.extractfile(member)
+ if source is None:
+ raise ValueError(f"官方 CLI 归档缺少二进制:{name}")
+ binary = source.read()
+ target = destination / name
+ target.write_bytes(binary)
+ target.chmod(0o755)
+ print(f"已校验固定版本 CLI:{name}")
+
+
+if __name__ == "__main__":
+ install(Path("build/release-tools").resolve())
diff --git a/scripts/publish_gitlab_release.py b/scripts/publish_gitlab_release.py
new file mode 100644
index 000000000..2636426b3
--- /dev/null
+++ b/scripts/publish_gitlab_release.py
@@ -0,0 +1,55 @@
+"""将已经完整下载校验的 R2 制品链接登记到 GitLab Release。"""
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path
+import re
+import subprocess
+import sys
+from urllib.request import Request, urlopen
+
+
+def publish() -> None:
+ tag = os.environ["CI_COMMIT_TAG"]
+ sha = os.environ["CI_COMMIT_SHA"]
+ if not re.fullmatch(r"v\d+\.\d+\.\d+", tag) or not re.fullmatch(r"[0-9a-f]{40}", sha):
+ raise ValueError("Release 必须使用明确的版本标签与完整提交 SHA")
+ actual = subprocess.check_output(["git", "rev-parse", f"refs/tags/{tag}^{{commit}}"], text=True).strip()
+ if actual != sha:
+ raise ValueError("标签不指向当前已验收的提交")
+ notes = Path("release/RELEASE_NOTES.md")
+ if not notes.is_file() or not notes.stat().st_size:
+ raise ValueError("缺少发行说明")
+ command = [os.environ.get("PEACH_GLAB", "glab"), "release", "create", tag,
+ "--repo", os.environ["CI_PROJECT_URL"], "--ref", sha,
+ "--name", f"Peach {tag[1:]} · Windows / macOS 测试版",
+ "--notes-file", str(notes), "--no-close-milestone"]
+ backend = os.environ.get("PEACH_DOWNLOAD_BACKEND", "gitgud")
+ if backend == "r2":
+ subprocess.run([sys.executable, "scripts/release_r2.py", "--root", "release", "--version", tag[1:], "--apply"], check=True)
+ base = os.environ["PEACH_R2_PUBLIC_BASE"].rstrip("/")
+ with urlopen(Request(f"{base}/{tag}/release.json", headers={"User-Agent": "Peach-release-verification"}), timeout=60) as response:
+ release = json.load(response)
+ if (release["tag_name"] != tag or release.get("commit_sha") != sha or not release["assets"]
+ or any(item["state"] != "uploaded" or not item["browser_download_url"].startswith(f"{base}/{tag}/")
+ for item in release["assets"])):
+ raise ValueError("R2 发布清单尚未就绪")
+ links = [{"name": item["name"], "url": item["browser_download_url"], "link_type": "package",
+ "direct_asset_path": "/" + item["name"]} for item in release["assets"]]
+ command.extend(["--assets-links", json.dumps(links)])
+ elif backend == "gitgud":
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+ from scripts.release_r2 import inventory
+ files = inventory(Path("release"), tag[1:])
+ sums = Path("release/SHA256SUMS.txt")
+ sums.write_text("".join(f"{item['sha256']} {item['name']}\n" for item in files), encoding="utf-8")
+ command.extend([*(str(Path("release") / item["name"]) for item in files), str(sums),
+ "--use-package-registry", "--package-name", "peach-release"])
+ else:
+ raise ValueError("未知的下载后端")
+ subprocess.run(command, check=True)
+
+
+if __name__ == "__main__":
+ publish()
diff --git a/scripts/release_forge.py b/scripts/release_forge.py
new file mode 100644
index 000000000..1fe0547de
--- /dev/null
+++ b/scripts/release_forge.py
@@ -0,0 +1,89 @@
+"""GitLab 发布的主线与流水线判据;认证和 HTTP 传输复用 glab。"""
+from __future__ import annotations
+
+import json
+import os
+import re
+import shutil
+from urllib.parse import quote, urlencode
+
+
+REQUIRED_TEST_JOBS = frozenset({"verify-linux", "verify-windows", "verify-macos", "wheel-smoke"})
+TERMINAL_STATUSES = frozenset({"success", "failed", "canceled", "skipped"})
+
+
+class GitLabForge:
+ def __init__(self, host: str, command):
+ if not re.fullmatch(r"[a-zA-Z0-9.-]+(?::[0-9]+)?", host):
+ raise ValueError("GitLab 主机必须是域名,可附端口")
+ self.host = host
+ self.command = command
+
+ def api(self, repo: str, endpoint: str):
+ executable = os.environ.get("PEACH_GLAB") or shutil.which("glab")
+ if not executable:
+ raise ValueError("GitLab 发布需要官方 glab;可用 PEACH_GLAB 指定其路径")
+ return json.loads(self.command(executable, "api", "--hostname", self.host,
+ "--method", "GET", f"projects/{quote(repo, safe='')}/{endpoint}"))
+
+ def remote_url(self, repo: str) -> str:
+ # 使用已配置的 Git 认证;PAT 和 Runner token 不进入命令参数。
+ return f"https://{self.host}/{repo}.git"
+
+ def release_url(self, repo: str, tag: str) -> str:
+ return f"https://{self.host}/{repo}/-/releases/{quote(tag, safe='')}"
+
+ def master_sha(self, repo: str) -> str:
+ return self.api(repo, "repository/branches/master")["commit"]["id"]
+
+ def require_free_tag(self, repo: str, tag: str) -> None:
+ query = urlencode({"search": f"^{re.escape(tag)}$", "per_page": 100})
+ if any(item["name"] == tag for item in self.api(repo, f"repository/tags?{query}")):
+ raise ValueError(f"{tag} 已存在,不覆盖;发布新版本请先推进 __version__")
+
+ def require_ancestor(self, repo: str, sha: str) -> None:
+ remote = self.master_sha(repo)
+ # Git 对提交图作精确判定;比较文件差异不能证明祖先关系。
+ self.command("git", "fetch", "--no-tags", self.remote_url(repo), "refs/heads/master")
+ fetched = self.command("git", "rev-parse", "FETCH_HEAD")
+ if fetched != remote:
+ raise ValueError("检查期间 GitLab master 已更新,请重新执行")
+ if self.command("git", "merge-base", remote, sha) != sha:
+ raise ValueError("发布提交不属于远端 master 历史")
+
+ def test_runs(self, repo: str, sha: str) -> list[dict]:
+ query = urlencode({"sha": sha, "ref": "master", "source": "push", "per_page": 100})
+ pipelines = self.api(repo, f"pipelines?{query}")
+ matching = [item for item in pipelines if item["sha"] == sha
+ and item["ref"] == "master" and item["source"] == "push"]
+ if not matching:
+ return []
+ pipeline = max(matching, key=lambda item: item["id"])
+ status = pipeline["status"]
+ if status == "success":
+ self.require_test_jobs(repo, pipeline["id"], sha)
+ # 统一 CI 结果字段供唯一发布入口消费,保留真实流水线 ID 与链接。
+ return [{"id": pipeline["id"], "head_sha": sha, "head_branch": "master", "event": "push",
+ "status": "completed" if status in TERMINAL_STATUSES else "in_progress",
+ "conclusion": "success" if status == "success" else status,
+ "html_url": pipeline["web_url"]}]
+
+ def require_test_jobs(self, repo: str, pipeline: int, sha: str) -> None:
+ jobs = []
+ for page in range(1, 11):
+ batch = self.api(repo, f"pipelines/{pipeline}/jobs?per_page=100&page={page}")
+ jobs.extend(batch)
+ if len(batch) < 100:
+ break
+ else:
+ raise ValueError("流水线任务超过可核对上限,发布检查未完成")
+ selected = {}
+ for job in jobs:
+ if job["name"] not in selected or job["id"] > selected[job["name"]]["id"]:
+ selected[job["name"]] = job
+ bad = [name for name in sorted(REQUIRED_TEST_JOBS)
+ if name not in selected or selected[name]["status"] != "success"
+ or selected[name].get("allow_failure")
+ or selected[name].get("commit", {}).get("id") != sha]
+ if bad:
+ raise ValueError("GitLab 发布验证缺少同一提交的必需成功任务:" + "、".join(bad))
diff --git a/scripts/release_r2.py b/scripts/release_r2.py
new file mode 100644
index 000000000..4995af76f
--- /dev/null
+++ b/scripts/release_r2.py
@@ -0,0 +1,161 @@
+"""将通过验收的发行包上传到 R2,并发布可公开校验的下载清单。"""
+from __future__ import annotations
+
+import argparse
+import hashlib
+import json
+import os
+from pathlib import Path
+import re
+import shutil
+import subprocess
+import tempfile
+from urllib.error import HTTPError
+from urllib.parse import quote, urlsplit
+from urllib.request import Request, urlopen
+
+
+def inventory(root: Path, version: str) -> list[dict]:
+ if not re.fullmatch(r"\d+\.\d+\.\d+", version):
+ raise ValueError("发布版本必须是三段数字")
+ paths = sorted(path for path in root.iterdir() if path.is_file()
+ and path.suffix in {".zip", ".exe", ".dmg"})
+ required = {f"Peach-{version}-windows-x64{suffix}" for suffix in (".zip", "-setup.exe", "-lite-setup.exe", "-update.zip")}
+ required.update(f"Peach-{version}-macos-{arch}.dmg" for arch in ("arm64", "x86_64"))
+ if not required.issubset(path.name for path in paths):
+ raise ValueError("发布制品缺少 Windows 完整包、轻量包或 macOS 双架构 DMG")
+ items = []
+ for path in paths:
+ if path.is_symlink() or not re.fullmatch(r"[A-Za-z0-9_.-]+", path.name) or not path.stat().st_size:
+ raise ValueError(f"无效的发布制品:{path.name}")
+ with path.open("rb") as source:
+ checksum = hashlib.file_digest(source, "sha256").hexdigest()
+ items.append({"name": path.name, "size": path.stat().st_size, "sha256": checksum})
+ return items
+
+
+def verify_download(url: str, item: dict) -> None:
+ checksum = hashlib.sha256()
+ size = 0
+ with urlopen(Request(url, headers={"User-Agent": "Peach-release-verification"}), timeout=120) as response:
+ if response.status != 200:
+ raise ValueError(f"公开下载未返回完整文件:{item['name']}")
+ while chunk := response.read(1024 * 1024):
+ size += len(chunk)
+ checksum.update(chunk)
+ if size != item["size"] or checksum.hexdigest() != item["sha256"]:
+ raise ValueError(f"公开下载的大小或 SHA-256 不符:{item['name']}")
+
+
+def rclone_environment() -> dict:
+ environment = os.environ.copy()
+ for key in ("PEACH_R2_ACCESS_KEY_ID", "PEACH_R2_SECRET_ACCESS_KEY", "PEACH_R2_ENDPOINT", "PEACH_R2_BUCKET", "PEACH_R2_PUBLIC_BASE"):
+ if not environment.get(key):
+ raise ValueError(f"缺少发布变量:{key}")
+ endpoint = urlsplit(environment["PEACH_R2_ENDPOINT"])
+ public = urlsplit(environment["PEACH_R2_PUBLIC_BASE"])
+ if endpoint.scheme != "https" or not endpoint.hostname or not endpoint.hostname.endswith(".r2.cloudflarestorage.com"):
+ raise ValueError("上传端点必须是 Cloudflare R2 HTTPS 地址")
+ if public.scheme != "https" or not public.hostname or public.username or public.password:
+ raise ValueError("公开下载入口必须是无凭据的 HTTPS 地址")
+ if not re.fullmatch(r"[a-z0-9][a-z0-9-]{1,61}[a-z0-9]", environment["PEACH_R2_BUCKET"]):
+ raise ValueError("无效的 R2 桶名")
+ environment.update({"RCLONE_CONFIG_PEACH_TYPE": "s3", "RCLONE_CONFIG_PEACH_PROVIDER": "Cloudflare",
+ "RCLONE_CONFIG_PEACH_ACCESS_KEY_ID": environment["PEACH_R2_ACCESS_KEY_ID"],
+ "RCLONE_CONFIG_PEACH_SECRET_ACCESS_KEY": environment["PEACH_R2_SECRET_ACCESS_KEY"],
+ "RCLONE_CONFIG_PEACH_ENDPOINT": environment["PEACH_R2_ENDPOINT"],
+ "RCLONE_CONFIG_PEACH_REGION": "auto", "RCLONE_CONFIG_PEACH_NO_CHECK_BUCKET": "true"})
+ return environment
+
+
+def require_capacity(existing: list[dict], items: list[dict], tag: str, limit: int) -> int:
+ """保留已有版本,超出本桶发布预算时拒绝上传。"""
+ current = {item["Path"]: item["Size"] for item in existing if not item.get("IsDir")}
+ projected = sum(current.values()) + sum(item["size"] for item in items
+ if f"{tag}/{item['name']}" not in current) + 1_000_000
+ if limit <= 0 or projected > limit:
+ raise ValueError(f"发布桶预计占用 {projected} 字节,超过上限 {limit};保留已有制品并停止上传")
+ return projected
+
+
+def publish(root: Path, version: str, *, apply: bool = False) -> dict:
+ items = inventory(root, version)
+ result = {"version": version, "files": items, "applied": apply}
+ if not apply:
+ return result
+ environment = rclone_environment()
+ published_at = environment.get("CI_COMMIT_TIMESTAMP")
+ if not published_at:
+ raise ValueError("发布需要 CI_COMMIT_TIMESTAMP,以便相同提交可重复校验")
+ sha = environment.get("CI_COMMIT_SHA", "")
+ if not re.fullmatch(r"[0-9a-f]{40}", sha):
+ raise ValueError("发布需要 CI_COMMIT_SHA")
+ executable = environment.get("PEACH_RCLONE") or shutil.which("rclone")
+ if not executable:
+ raise ValueError("发布需要官方 rclone")
+ tag = "v" + version
+ prefix = f"peach:{environment['PEACH_R2_BUCKET']}/{tag}"
+ base = environment["PEACH_R2_PUBLIC_BASE"].rstrip("/")
+
+ def upload(path: Path, target: str, *, immutable: bool = True) -> None:
+ command = [executable, "copyto", str(path), target, "--checksum", "--config", str(configuration),
+ "--header-upload", "Cache-Control: public, max-age=" + ("31536000, immutable" if immutable else "60")]
+ if immutable:
+ command.append("--immutable")
+ subprocess.run(command, env=environment, check=True)
+
+ with tempfile.TemporaryDirectory(prefix="peach-release-publish-") as directory:
+ temporary = Path(directory)
+ configuration = temporary / "rclone.conf"
+ configuration.touch()
+ listing = subprocess.check_output([executable, "lsjson", f"peach:{environment['PEACH_R2_BUCKET']}",
+ "--recursive", "--files-only", "--config", str(configuration)], env=environment, text=True)
+ result["projected_bucket_bytes"] = require_capacity(json.loads(listing), items, tag,
+ int(environment.get("PEACH_R2_MAX_BYTES", "8000000000")))
+ sums = temporary / "SHA256SUMS.txt"
+ sums.write_text("".join(f"{item['sha256']} {item['name']}\n" for item in items), encoding="utf-8")
+ with sums.open("rb") as source:
+ manifest = {"name": sums.name, "size": sums.stat().st_size,
+ "sha256": hashlib.file_digest(source, "sha256").hexdigest()}
+ for item in [*items, manifest]:
+ upload(sums if item is manifest else root / item["name"], f"{prefix}/{item['name']}")
+ item["browser_download_url"] = f"{base}/{tag}/{quote(item['name'])}"
+ verify_download(item["browser_download_url"], item)
+ item["state"] = "uploaded"
+ release = {"tag_name": tag, "commit_sha": sha, "name": f"Peach {version}", "draft": False, "prerelease": True,
+ "published_at": published_at, "assets": [*items, manifest]}
+ release_file = temporary / "release.json"
+ release_file.write_text(json.dumps(release, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
+ upload(release_file, f"{prefix}/release.json")
+ try:
+ with urlopen(base + "/releases.json", timeout=60) as response:
+ releases = json.load(response)
+ except HTTPError as error:
+ if error.code != 404:
+ raise
+ releases = []
+ if not isinstance(releases, list):
+ raise ValueError("公开版本索引格式无效")
+ index = temporary / "releases.json"
+ index.write_text(json.dumps([release, *(item for item in releases if item["tag_name"] != tag)][:30],
+ ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
+ upload(index, f"peach:{environment['PEACH_R2_BUCKET']}/releases.json", immutable=False)
+ return result
+
+
+def main(argv=None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--root", type=Path, required=True)
+ parser.add_argument("--version", required=True)
+ parser.add_argument("--apply", action="store_true")
+ args = parser.parse_args(argv)
+ try:
+ print(json.dumps(publish(args.root.resolve(), args.version, apply=args.apply), ensure_ascii=False, indent=2))
+ except (OSError, ValueError, subprocess.CalledProcessError) as error:
+ print(f"R2 发布未完成:{error}")
+ return 1
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/scripts/release_review.py b/scripts/release_review.py
index 6e23c282b..1196ebd29 100644
--- a/scripts/release_review.py
+++ b/scripts/release_review.py
@@ -8,6 +8,7 @@ from pathlib import Path
import shutil
import subprocess
import sys
+from urllib.request import Request, urlopen
if not __package__:
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
@@ -26,7 +27,26 @@ def run(args: list[str], root: Path, *, allowed: tuple[int, ...] = (0,)) -> str:
return result.stdout.strip()
-def collect(root: Path, repo: str) -> dict:
+def public_github_api(endpoint: str):
+ """公开上游元数据使用匿名官方 API,不携带本机账号凭据。"""
+ request = Request(f"https://api.github.com/{endpoint}",
+ headers={"Accept": "application/vnd.github+json", "User-Agent": "Peach-release-review"})
+ with urlopen(request, timeout=60) as response:
+ return json.load(response)
+
+
+def public_dependabot(repo: str) -> list[dict]:
+ found = []
+ for page in range(1, 11):
+ batch = public_github_api(f"repos/{repo}/pulls?state=open&per_page=100&page={page}")
+ found.extend({"number": item["number"], "title": item["title"], "url": item["html_url"]}
+ for item in batch if item.get("user", {}).get("login") == "dependabot[bot]")
+ if len(batch) < 100:
+ return found
+ raise ValueError("公开上游 PR 超过可核对上限,检查未完成")
+
+
+def collect(root: Path, repo: str, *, public_github: bool = False) -> dict:
registry = references.load_registry(root / "docs/reference-sources.json")
problems = references.validate_registry(root, registry)
if problems:
@@ -53,6 +73,8 @@ def collect(root: Path, repo: str) -> dict:
f"repos/{repo}/pulls?state=open&per_page=100"], (0,)),
):
def inspect_dependency(args=args, allowed=allowed, label=label):
+ if label == "dependabot" and public_github:
+ return public_dependabot(repo)
output = run(args, root, allowed=allowed)
if label == "dependabot":
return [{"number": item["number"], "title": item["title"], "url": item["html_url"]}
@@ -82,8 +104,9 @@ def collect(root: Path, repo: str) -> dict:
web = pin["repository"].removesuffix(".git")
if web.startswith("https://github.com/"):
upstream = web.removeprefix("https://github.com/")
- check(pin["id"] + "-release", lambda upstream=upstream: json.loads(run(
- ["gh", "api", f"repos/{upstream}/releases/latest"], root)))
+ check(pin["id"] + "-release", lambda upstream=upstream:
+ public_github_api(f"repos/{upstream}/releases/latest") if public_github else json.loads(run(
+ ["gh", "api", f"repos/{upstream}/releases/latest"], root)))
for source in references.selected_sources(registry, None):
def inspect(source=source):
result = references.inspect_source(root, source, revision_resolver=resolve)
@@ -92,8 +115,8 @@ def collect(root: Path, repo: str) -> dict:
return report
-def review(root: Path, repo: str) -> dict:
- report = collect(root, repo)
+def review(root: Path, repo: str, *, public_github: bool = False) -> dict:
+ report = collect(root, repo, public_github=True) if public_github else collect(root, repo)
path = root / "build/release-review.json"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
diff --git a/scripts/release_tag.py b/scripts/release_tag.py
index 8f62299d4..03084fd59 100644
--- a/scripts/release_tag.py
+++ b/scripts/release_tag.py
@@ -28,6 +28,7 @@ if not __package__:
from . import changelog, version_bump
ROOT = Path(__file__).resolve().parents[1]
+GITLAB = None
#: 主检出的 master 上该落提交的只有本脚本与 `agent_worktree.py integrate`。它们调用 git 时
#: 带上这个配置,`scripts/githooks/pre-commit` 据此放行,别的提交与手工 merge 一律拒收。
@@ -56,6 +57,18 @@ def api(repo: str, endpoint: str):
return json.loads(command("gh", "api", f"repos/{repo}/{endpoint}"))
+def master_sha(repo: str) -> str:
+ return GITLAB.master_sha(repo) if GITLAB else api(repo, "git/ref/heads/master")["object"]["sha"]
+
+
+def remote_url(repo: str) -> str:
+ return GITLAB.remote_url(repo) if GITLAB else f"https://github.com/{repo}.git"
+
+
+def release_url(repo: str, tag: str) -> str:
+ return GITLAB.release_url(repo, tag) if GITLAB else f"https://github.com/{repo}/releases/tag/{tag}"
+
+
def latest_run(runs: list[dict], sha: str) -> dict | None:
"""这个提交在 master 上最新的一次 Test;没有记录时是 None。"""
matching = [run for run in runs if run.get("head_sha") == sha
@@ -75,6 +88,8 @@ def require_success(runs: list[dict], sha: str) -> dict:
def test_runs(repo: str, sha: str) -> list[dict]:
+ if GITLAB:
+ return GITLAB.test_runs(repo, sha)
endpoint = f"actions/workflows/test.yml/runs?head_sha={sha}&event=push&per_page=100"
return api(repo, endpoint)["workflow_runs"]
@@ -100,9 +115,12 @@ def await_test(repo: str, sha: str, *, timeout: float = TEST_TIMEOUT, poll: floa
def verify(repo: str, sha: str) -> dict:
- comparison = api(repo, f"compare/master...{sha}")
- if comparison["status"] not in {"identical", "behind"}:
- raise ValueError("发布提交不属于远端 master 历史")
+ if GITLAB:
+ GITLAB.require_ancestor(repo, sha)
+ else:
+ comparison = api(repo, f"compare/master...{sha}")
+ if comparison["status"] not in {"identical", "behind"}:
+ raise ValueError("发布提交不属于远端 master 历史")
checked = require_success(test_runs(repo, sha), sha)
return {"sha": sha, "test": checked["html_url"]}
@@ -115,6 +133,9 @@ def _require_clean_master() -> None:
def _require_free_tag(repo: str, tag: str) -> None:
+ if GITLAB:
+ GITLAB.require_free_tag(repo, tag)
+ return
# matching-refs 返回空列表代表不存在;网络或权限错误不能被当成空列表。
if any(ref["ref"] == f"refs/tags/{tag}" for ref in api(repo, f"git/matching-refs/tags/{tag}")):
raise ValueError(f"{tag} 已存在,不覆盖;发布新版本请先推进 __version__")
@@ -134,7 +155,8 @@ def prepare(repo: str, part: str, *, apply: bool) -> dict:
if not apply:
return result
from .release_review import review
- result["upstream_review"] = review(ROOT, repo)
+ result["upstream_review"] = (review(ROOT, repo, public_github=True)
+ if GITLAB else review(ROOT, repo))
version_bump.write_version(ROOT, str(planned["bump"]))
changelog.release(ROOT, version, repo=repo, spec=str(planned["range"]))
# 那一节原样带在输出里:确认措辞的人不该再去翻文件才知道自己在确认什么。
@@ -190,7 +212,7 @@ def ship(repo: str, *, apply: bool, timeout: float = TEST_TIMEOUT) -> dict:
"""
version, tag, pending = _staged_release(repo)
head = command("git", "rev-parse", "HEAD")
- remote = api(repo, "git/ref/heads/master")["object"]["sha"]
+ remote = master_sha(repo)
result = {"version": version, "tag": tag, "repo": repo, "applied": apply,
"commit": bool(pending), "push": bool(pending) or remote != head}
if not apply:
@@ -200,20 +222,20 @@ def ship(repo: str, *, apply: bool, timeout: float = TEST_TIMEOUT) -> dict:
command("git", "-c", f"{MASTER_WRITER}=release", "commit",
"-m", f"chore(release): 版本 {version}")
sha = command("git", "rev-parse", "HEAD")
- if api(repo, "git/ref/heads/master")["object"]["sha"] != sha:
- command("git", "push", f"https://github.com/{repo}.git", "refs/heads/master")
+ if master_sha(repo) != sha:
+ command("git", "push", remote_url(repo), "refs/heads/master")
checked = await_test(repo, sha, timeout=timeout)
command("git", "tag", "-a", tag, sha, "-m", f"Peach {tag} Windows 测试版")
- command("git", "push", f"https://github.com/{repo}.git", f"refs/tags/{tag}")
+ command("git", "push", remote_url(repo), f"refs/tags/{tag}")
return {**result, "sha": sha, "test": checked["html_url"],
- "release": f"https://github.com/{repo}/releases/tag/{tag}"}
+ "release": release_url(repo, tag)}
def plan(repo: str) -> dict:
_require_clean_master()
sha = command("git", "rev-parse", "HEAD")
- if api(repo, "git/ref/heads/master")["object"]["sha"] != sha:
- raise ValueError("本地 HEAD 与 GitHub master 不一致,请先完成同步")
+ if master_sha(repo) != sha:
+ raise ValueError("本地 HEAD 与发布平台 master 不一致,请先完成同步")
version = version_bump.read_version(ROOT)
tag = "v" + version
document = (ROOT / changelog.CHANGELOG).read_text(encoding="utf-8")
@@ -250,9 +272,11 @@ def plan_commit(repo: str, sha: str) -> dict:
def main(argv=None) -> int:
+ global GITLAB
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--repo", default="peach-mitao/peach")
+ parser.add_argument("--gitlab-host", help="使用此 GitLab 主机发布,例如 gitgud.io;默认使用 GitHub")
parser.add_argument("--apply", action="store_true",
help="真的动手:带 --bump 时写版本号与变更日志,带 --ship 时走完发布,"
"都不带时创建并推送标签")
@@ -267,6 +291,8 @@ def main(argv=None) -> int:
mode.add_argument("--release-sha", help="为完整 SHA 指定的已通过 CI 的主线提交补打标签")
args = parser.parse_args(argv)
try:
+ from .release_forge import GitLabForge
+ GITLAB = GitLabForge(args.gitlab_host, command) if args.gitlab_host else None
if args.verify_sha:
print(json.dumps(verify(args.repo, args.verify_sha), ensure_ascii=False, indent=2))
return 0
@@ -284,18 +310,20 @@ def main(argv=None) -> int:
# 固定提交在动手前复核归属、CI、版本说明与标签占用。
if args.release_sha is not None:
result = plan_commit(args.repo, args.release_sha)
- elif api(args.repo, "git/ref/heads/master")["object"]["sha"] != result["sha"]:
+ elif master_sha(args.repo) != result["sha"]:
raise ValueError("检查期间 master 已更新,请重新执行")
command("git", "tag", "-a", result["tag"], result["sha"],
"-m", f"Peach {result['tag']} Windows 测试版")
- command("git", "push", f"https://github.com/{args.repo}.git",
+ command("git", "push", remote_url(args.repo),
f"refs/tags/{result['tag']}")
print(json.dumps({**result, "applied": args.apply}, ensure_ascii=False, indent=2))
return 0
- except (ValueError, version_bump.VersionError, subprocess.CalledProcessError) as exc:
+ except (OSError, ValueError, version_bump.VersionError, subprocess.CalledProcessError) as exc:
detail = exc.stderr if isinstance(exc, subprocess.CalledProcessError) else str(exc)
print(f"发布检查未通过:{detail}")
return 1
+ finally:
+ GITLAB = None
if __name__ == "__main__":
diff --git a/tests/support/conditions.py b/tests/support/conditions.py
index 72c624fd3..61109d421 100644
--- a/tests/support/conditions.py
+++ b/tests/support/conditions.py
@@ -13,7 +13,7 @@ def missing_prerequisite(reason: str) -> NoReturn:
本机没装时显式跳过,免得整个测试域红掉让人绕过入口。CI 的 runner 由工作流负责
装齐,缺了就是工作流写错:跳过会让用例在 CI 里永远不执行,所以那里判失败。
"""
- if os.environ.get("GITHUB_ACTIONS") == "true":
+ if any(os.environ.get(name) == "true" for name in ("GITHUB_ACTIONS", "GITLAB_CI")):
raise AssertionError(f"CI 缺少前置条件,工作流应当装好:{reason}")
raise unittest.SkipTest(reason)
diff --git a/tests/test_reference_updates.py b/tests/test_reference_updates.py
index 1dbc0f724..fef4050b0 100644
--- a/tests/test_reference_updates.py
+++ b/tests/test_reference_updates.py
@@ -19,6 +19,16 @@ def load_checker():
class ReleaseReviewTests(unittest.TestCase):
+ def test_public_review_paginates_dependabot_without_account_credentials(self):
+ from scripts import release_review as review
+ item = {"number": 3, "title": "更新", "html_url": "url", "user": {"login": "dependabot[bot]"}}
+ with mock.patch.object(review, "public_github_api", side_effect=[[item] * 100, []]) as api:
+ self.assertEqual(len(review.public_dependabot("owner/repo")), 100)
+ self.assertEqual(api.call_count, 2)
+ with mock.patch.object(review, "public_github_api", side_effect=OSError("network unavailable")), \
+ self.assertRaises(OSError):
+ review.public_dependabot("owner/repo")
+
def test_network_failure_keeps_later_checks_and_records_missing_evidence(self):
from scripts import release_review as review
registry = {"pins": [], "sources": [
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index 760290278..24939cba9 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -4694,6 +4694,124 @@ class ScriptingConventionTests(unittest.TestCase):
self.assertFalse(scripting.host_under("", ("x.com",)))
+class R2ReleaseTests(unittest.TestCase):
+ def test_capacity_keeps_existing_versions_and_stops_before_upload(self):
+ module = load_script("release_r2")
+ existing = [{"Path": "v1.0.0/old.zip", "Size": 2_000_000}, {"Path": "v1.2.3/app.zip", "Size": 3_000_000}]
+ files = [{"name": "app.zip", "size": 3_000_000}, {"name": "new.dmg", "size": 4_000_000}]
+ self.assertEqual(module.require_capacity(existing, files, "v1.2.3", 10_000_000), 10_000_000)
+ with self.assertRaisesRegex(ValueError, "停止上传"):
+ module.require_capacity(existing, files, "v1.2.3", 9_999_999)
+
+ def test_public_download_checks_all_bytes_and_checksum(self):
+ module = load_script("release_r2")
+ data = b"Peach public artifact"
+ response = mock.MagicMock()
+ response.__enter__.return_value = response
+ response.status = 200
+ response.read.side_effect = [data[:5], data[5:], b""]
+ expected = {"name": "Peach.zip", "size": len(data), "sha256": hashlib.sha256(data).hexdigest()}
+ with mock.patch.object(module, "urlopen", return_value=response):
+ module.verify_download("https://downloads.example/Peach.zip", expected)
+ response.read.side_effect = [data[:-1], b""]
+ with mock.patch.object(module, "urlopen", return_value=response), self.assertRaisesRegex(ValueError, "SHA-256"):
+ module.verify_download("https://downloads.example/Peach.zip", expected)
+
+ def test_inventory_requires_complete_platforms_and_does_not_upload_helpers(self):
+ module = load_script("release_r2")
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary).resolve()
+ names = [f"Peach-1.2.3-windows-x64{suffix}" for suffix in (".zip", "-setup.exe", "-lite-setup.exe", "-update.zip")]
+ names += [f"Peach-1.2.3-macos-{arch}.dmg" for arch in ("arm64", "x86_64")]
+ for name in names:
+ (root / name).write_bytes(name.encode())
+ (root / "private-config.toml").write_text("private", encoding="utf-8")
+ plan = module.publish(root, "1.2.3")
+ self.assertFalse(plan["applied"])
+ self.assertEqual({item["name"] for item in plan["files"]}, set(names))
+ (root / names[-1]).unlink()
+ with self.assertRaisesRegex(ValueError, "双架构"):
+ module.inventory(root, "1.2.3")
+
+ def test_credentials_only_enter_rclone_environment_and_https_is_required(self):
+ module = load_script("release_r2")
+ environment = {"PEACH_R2_ACCESS_KEY_ID": "access", "PEACH_R2_SECRET_ACCESS_KEY": "secret",
+ "PEACH_R2_ENDPOINT": "https://account.r2.cloudflarestorage.com",
+ "PEACH_R2_BUCKET": "peach-releases", "PEACH_R2_PUBLIC_BASE": "https://downloads.example"}
+ with mock.patch.dict(os.environ, environment, clear=True):
+ result = module.rclone_environment()
+ self.assertEqual(result["RCLONE_CONFIG_PEACH_SECRET_ACCESS_KEY"], "secret")
+ self.assertEqual(result["RCLONE_CONFIG_PEACH_NO_CHECK_BUCKET"], "true")
+ for key in ("PEACH_R2_ENDPOINT", "PEACH_R2_PUBLIC_BASE"):
+ with mock.patch.dict(os.environ, {**environment, key: "http://example"}, clear=True), self.assertRaises(ValueError):
+ module.rclone_environment()
+
+
+class GitLabReleaseTests(unittest.TestCase):
+ SHA = "1" * 40
+
+ def forge(self):
+ from scripts.release_forge import GitLabForge
+ return GitLabForge("gitgud.io", mock.Mock())
+
+ def pipeline(self, **changes):
+ return {"id": 7, "sha": self.SHA, "ref": "master", "source": "push",
+ "status": "success", "web_url": "https://gitgud.io/pipeline/7", **changes}
+
+ def jobs(self):
+ from scripts.release_forge import REQUIRED_TEST_JOBS
+ return [{"id": index, "name": name, "status": "success", "allow_failure": False,
+ "commit": {"id": self.SHA}} for index, name in enumerate(sorted(REQUIRED_TEST_JOBS), 1)]
+
+ def test_release_requires_native_system_jobs_for_the_same_commit(self):
+ forge = self.forge()
+ for change in ({"status": "skipped"}, {"status": "failed"}, {"allow_failure": True},
+ {"commit": {"id": "other"}}):
+ jobs = self.jobs()
+ jobs[0] = {**jobs[0], **change}
+ with self.subTest(change=change), mock.patch.object(forge, "api", side_effect=[
+ [self.pipeline()], jobs
+ ]), self.assertRaisesRegex(ValueError, "必需成功任务"):
+ forge.test_runs("owner/repo", self.SHA)
+ with mock.patch.object(forge, "api", side_effect=[[self.pipeline()], self.jobs()]) as api:
+ checked = forge.test_runs("owner/repo", self.SHA)
+ self.assertEqual(checked[0]["html_url"], "https://gitgud.io/pipeline/7")
+ self.assertEqual(checked[0]["conclusion"], "success")
+ self.assertEqual(api.call_count, 2)
+
+ def test_latest_push_pipeline_is_required_and_failed_retries_block_release(self):
+ forge = self.forge()
+ pipelines = [self.pipeline(), self.pipeline(id=8, status="failed"),
+ self.pipeline(id=9, ref="feature"), self.pipeline(id=10, source="web")]
+ with mock.patch.object(forge, "api", return_value=pipelines):
+ checked = forge.test_runs("owner/repo", self.SHA)
+ self.assertEqual(checked[0]["id"], 8)
+ self.assertEqual(checked[0]["conclusion"], "failed")
+
+ def test_missing_native_jobs_cannot_be_replaced_by_linux_success(self):
+ forge = self.forge()
+ jobs = [job for job in self.jobs() if job["name"] != "verify-macos"]
+ with mock.patch.object(forge, "api", side_effect=[[self.pipeline()], jobs]), \
+ self.assertRaisesRegex(ValueError, "verify-macos"):
+ forge.test_runs("owner/repo", self.SHA)
+
+ def test_mainline_membership_uses_the_git_commit_graph(self):
+ forge = self.forge()
+ forge.command.side_effect = ["", self.SHA, "ancestor"]
+ with mock.patch.object(forge, "master_sha", return_value=self.SHA), \
+ self.assertRaisesRegex(ValueError, "不属于"):
+ forge.require_ancestor("owner/repo", "divergent")
+ self.assertEqual(forge.remote_url("owner/repo"), "https://gitgud.io/owner/repo.git")
+
+ def test_glab_is_scoped_to_the_selected_host_without_inline_credentials(self):
+ forge = self.forge()
+ forge.command.return_value = '{"commit":{"id":"sha"}}'
+ with mock.patch.dict(os.environ, {"PEACH_GLAB": "glab"}):
+ self.assertEqual(forge.master_sha("owner/repo"), "sha")
+ forge.command.assert_called_once_with("glab", "api", "--hostname", "gitgud.io", "--method", "GET",
+ "projects/owner%2Frepo/repository/branches/master")
+
+
class ReleaseTagTests(unittest.TestCase):
@classmethod
def setUpClass(cls):