From 588b81dae446fa71c81068985ff75d6809b23810 Mon Sep 17 00:00:00 2001 From: csh Date: Tue, 19 May 2026 09:50:24 +0800 Subject: [PATCH] :bug: fix(ci): inline gitea workflow bootstrap remove the pre-checkout dependency on .gitea/ci/prepare_repo.sh from the root workflows and inline the bootstrap logic instead. add a regression test for workflow bootstrap ordering and register it in the tests README. --- .gitea/ci/prepare_repo.sh | 51 ----------------------- .gitea/workflows/standards-check.yml | 56 ++++++++++++++++++++++---- .gitea/workflows/test.yml | 56 ++++++++++++++++++++++---- tests/README.md | 1 + tests/test_gitea_workflow_bootstrap.py | 22 ++++++++++ 5 files changed, 121 insertions(+), 65 deletions(-) delete mode 100644 .gitea/ci/prepare_repo.sh create mode 100644 tests/test_gitea_workflow_bootstrap.py diff --git a/.gitea/ci/prepare_repo.sh b/.gitea/ci/prepare_repo.sh deleted file mode 100644 index df4a9c99..00000000 --- a/.gitea/ci/prepare_repo.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env bash -# Clones or updates the repo to WORKSPACE_DIR/ and checks out the -# target commit. Sets REPO_DIR in $GITHUB_ENV on success. -# -# Required env vars (provided by GitHub Actions context): -# WORKSPACE_DIR, GITHUB_SERVER_URL, GITHUB_REPOSITORY, github.sha, -# github.ref, github.ref_name, github.event.repository.name -# Optional: -# WORKFLOW secret (used as OAuth token for private repos) -set -euo pipefail - -REPO_NAME="${REPO_NAME}" -REPO_DIR="${WORKSPACE_DIR}/${REPO_NAME}" -TOKEN="${TOKEN:-}" - -if [ -n "$TOKEN" ]; then - REPO_URL="https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git" -else - REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" -fi - -if [ -d "$REPO_DIR" ]; then - if [ -d "$REPO_DIR/.git" ]; then - cd "$REPO_DIR" - git clean -fdx - git reset --hard - git fetch --all --tags --force --prune --prune-tags - else - rm -rf "$REPO_DIR" - fi -fi - -if [ ! -d "$REPO_DIR/.git" ]; then - mkdir -p "$WORKSPACE_DIR" - git clone "$REPO_URL" "$REPO_DIR" - cd "$REPO_DIR" -fi - -if git -C "$REPO_DIR" cat-file -e "${TARGET_SHA}^{commit}" 2>/dev/null; then - git -C "$REPO_DIR" checkout -f "$TARGET_SHA" -else - if [ -n "${TARGET_REF:-}" ]; then - git -C "$REPO_DIR" fetch origin "$TARGET_REF" - git -C "$REPO_DIR" checkout -f FETCH_HEAD - else - git -C "$REPO_DIR" checkout -f "$TARGET_REF_NAME" - fi -fi - -git config --global --add safe.directory "$REPO_DIR" -echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV" diff --git a/.gitea/workflows/standards-check.yml b/.gitea/workflows/standards-check.yml index ec17dd3a..dc1e778b 100644 --- a/.gitea/workflows/standards-check.yml +++ b/.gitea/workflows/standards-check.yml @@ -23,13 +23,55 @@ jobs: steps: - name: 📥 准备仓库 - env: - REPO_NAME: ${{ github.event.repository.name }} - TOKEN: ${{ secrets.WORKFLOW }} - TARGET_SHA: ${{ github.sha }} - TARGET_REF: ${{ github.ref }} - TARGET_REF_NAME: ${{ github.ref_name }} - run: bash .gitea/ci/prepare_repo.sh + run: | + set -euo pipefail + + echo "========================================" + echo "📥 准备仓库到 WORKSPACE_DIR" + echo "========================================" + + REPO_NAME="${{ github.event.repository.name }}" + REPO_DIR="${WORKSPACE_DIR}/${REPO_NAME}" + TOKEN="${{ secrets.WORKFLOW }}" + TARGET_SHA="${{ github.sha }}" + TARGET_REF="${{ github.ref }}" + TARGET_REF_NAME="${{ github.ref_name }}" + + if [ -n "$TOKEN" ]; then + REPO_URL="https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git" + else + REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + fi + + if [ -d "$REPO_DIR" ]; then + if [ -d "$REPO_DIR/.git" ]; then + cd "$REPO_DIR" + git clean -fdx + git reset --hard + git fetch --all --tags --force --prune --prune-tags + else + rm -rf "$REPO_DIR" + fi + fi + + if [ ! -d "$REPO_DIR/.git" ]; then + mkdir -p "$WORKSPACE_DIR" + git clone "$REPO_URL" "$REPO_DIR" + fi + + if git -C "$REPO_DIR" cat-file -e "${TARGET_SHA}^{commit}" 2>/dev/null; then + git -C "$REPO_DIR" checkout -f "$TARGET_SHA" + else + if [ -n "$TARGET_REF" ]; then + git -C "$REPO_DIR" fetch origin "$TARGET_REF" + git -C "$REPO_DIR" checkout -f FETCH_HEAD + else + git -C "$REPO_DIR" checkout -f "$TARGET_REF_NAME" + fi + fi + + git config --global --add safe.directory "$REPO_DIR" + echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV" - name: 🧪 Lint commit message / PR title run: | cd "$REPO_DIR" diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index d1c5188f..ccb83158 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -30,13 +30,55 @@ jobs: steps: - name: 📥 准备仓库 - env: - REPO_NAME: ${{ github.event.repository.name }} - TOKEN: ${{ secrets.WORKFLOW }} - TARGET_SHA: ${{ github.sha }} - TARGET_REF: ${{ github.ref }} - TARGET_REF_NAME: ${{ github.ref_name }} - run: bash .gitea/ci/prepare_repo.sh + run: | + set -euo pipefail + + echo "========================================" + echo "📥 准备仓库到 WORKSPACE_DIR" + echo "========================================" + + REPO_NAME="${{ github.event.repository.name }}" + REPO_DIR="${WORKSPACE_DIR}/${REPO_NAME}" + TOKEN="${{ secrets.WORKFLOW }}" + TARGET_SHA="${{ github.sha }}" + TARGET_REF="${{ github.ref }}" + TARGET_REF_NAME="${{ github.ref_name }}" + + if [ -n "$TOKEN" ]; then + REPO_URL="https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git" + else + REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + fi + + if [ -d "$REPO_DIR" ]; then + if [ -d "$REPO_DIR/.git" ]; then + cd "$REPO_DIR" + git clean -fdx + git reset --hard + git fetch --all --tags --force --prune --prune-tags + else + rm -rf "$REPO_DIR" + fi + fi + + if [ ! -d "$REPO_DIR/.git" ]; then + mkdir -p "$WORKSPACE_DIR" + git clone "$REPO_URL" "$REPO_DIR" + fi + + if git -C "$REPO_DIR" cat-file -e "${TARGET_SHA}^{commit}" 2>/dev/null; then + git -C "$REPO_DIR" checkout -f "$TARGET_SHA" + else + if [ -n "$TARGET_REF" ]; then + git -C "$REPO_DIR" fetch origin "$TARGET_REF" + git -C "$REPO_DIR" checkout -f FETCH_HEAD + else + git -C "$REPO_DIR" checkout -f "$TARGET_REF_NAME" + fi + fi + + git config --global --add safe.directory "$REPO_DIR" + echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV" - name: 🔧 安装测试依赖 run: | diff --git a/tests/README.md b/tests/README.md index b0dc7777..9e8f886e 100644 --- a/tests/README.md +++ b/tests/README.md @@ -10,6 +10,7 @@ tests/ ├── cli/ # Python CLI 测试(unittest) │ └── test_playbook_cli.py # playbook.py 基础功能测试 ├── test_format_md_action.py # format_md 动作测试 +├── test_gitea_workflow_bootstrap.py # Gitea workflow 自举顺序回归测试 ├── test_firstparty_skills_quality.py # first-party skills 元数据与结构质量测试 ├── test_gitattributes_modes.py # gitattr_mode 行为测试 ├── test_no_backup_flags.py # no_backup 行为测试 diff --git a/tests/test_gitea_workflow_bootstrap.py b/tests/test_gitea_workflow_bootstrap.py new file mode 100644 index 00000000..5ad34a28 --- /dev/null +++ b/tests/test_gitea_workflow_bootstrap.py @@ -0,0 +1,22 @@ +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +TEST_WORKFLOW = ROOT / ".gitea" / "workflows" / "test.yml" +STANDARDS_WORKFLOW = ROOT / ".gitea" / "workflows" / "standards-check.yml" + + +class GiteaWorkflowBootstrapTests(unittest.TestCase): + def test_repo_workflows_do_not_call_repo_local_prepare_script_before_checkout(self): + for workflow in (TEST_WORKFLOW, STANDARDS_WORKFLOW): + text = workflow.read_text(encoding="utf-8") + with self.subTest(workflow=workflow.name): + self.assertNotIn("bash .gitea/ci/prepare_repo.sh", text) + self.assertIn('REPO_DIR="${WORKSPACE_DIR}/${REPO_NAME}"', text) + self.assertIn('git clone "$REPO_URL" "$REPO_DIR"', text) + self.assertIn('echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV"', text) + + +if __name__ == "__main__": + unittest.main()