From b05904ba090889bd3becec951ff421dc8f6a417f Mon Sep 17 00:00:00 2001 From: bobokun Date: Thu, 21 Aug 2025 15:49:00 -0400 Subject: [PATCH] ci(version): make VERSION bump PR-aware and CI-safe - detect CI env and handle pull_request by diffing VERSION against base - auto-run develop version update when PR doesn't change VERSION - avoid staged-file checks in CI; keep them for local commits - resolve/fetch base branch for shallow clones to compute diffs - switch release workflow to trigger on push tags (v*) - remove Tauri cargo-check from pre-commit hooks --- .github/workflows/version.yml | 2 +- .pre-commit-config.yaml | 8 --- VERSION | 2 +- scripts/pre-commit/increase_version.sh | 70 ++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml index e61d926..4a25b83 100644 --- a/.github/workflows/version.yml +++ b/.github/workflows/version.yml @@ -1,7 +1,7 @@ name: Version Release on: - create: + push: tags: - v* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1230a8d..5c23801 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,11 +43,3 @@ repos: language: script pass_filenames: false stages: [pre-commit] - - id: cargo-check - name: Cargo check for Tauri desktop app - entry: bash -c "cd desktop/tauri/src-tauri && cargo check" - language: system - pass_filenames: false - files: ^desktop/tauri/src-tauri/.*\.(rs|toml|lock|json)$ - stages: [pre-commit] - types_or: [rust, toml, json] diff --git a/VERSION b/VERSION index 4cd351a..1afd29e 100755 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.5.5-develop8 +4.5.5-develop9 diff --git a/scripts/pre-commit/increase_version.sh b/scripts/pre-commit/increase_version.sh index 4f37c5a..d0c7df7 100755 --- a/scripts/pre-commit/increase_version.sh +++ b/scripts/pre-commit/increase_version.sh @@ -1,7 +1,71 @@ #!/usr/bin/env bash -# Check if there are any changes staged for commit -if [[ -z $(git diff --cached --name-only) ]]; then +# Detect if running in CI (e.g., GitHub Actions or pre-commit.ci) +if [[ -n "$GITHUB_ACTIONS" || -n "$CI" || -n "$PRE_COMMIT_CI" ]]; then + IN_CI=true +else + IN_CI=false +fi +# CI: For pull_request events, check if the PR itself changes VERSION. +# If not, run the develop version updater. This avoids relying on staged files. +if [[ "$IN_CI" == "true" ]]; then + BASE_REF="${GITHUB_BASE_REF:-}" + + # If BASE_REF not provided (e.g., pre-commit.ci), infer remote default branch + if [[ -z "$BASE_REF" ]]; then + DEFAULT_BASE="$(git symbolic-ref -q --short refs/remotes/origin/HEAD 2>/dev/null | sed 's#^origin/##')" + if [[ -z "$DEFAULT_BASE" ]]; then + DEFAULT_BASE="$(git remote show origin 2>/dev/null | sed -n 's/.*HEAD branch: //p' | head -n1)" + fi + BASE_REF="$DEFAULT_BASE" + fi + + # Resolve a usable base ref + CANDIDATES=() + if [[ -n "$BASE_REF" ]]; then + CANDIDATES+=("refs/remotes/origin/$BASE_REF") + CANDIDATES+=("refs/heads/$BASE_REF") + fi + + BASE_RESOLVED="" + for ref in "${CANDIDATES[@]}"; do + if git rev-parse --verify -q "$ref" >/dev/null; then + BASE_RESOLVED="$ref" + break + fi + done + + # Attempt to fetch the remote-tracking base if missing (handles shallow clones) + if [[ -z "$BASE_RESOLVED" && -n "$BASE_REF" ]]; then + git fetch --no-tags --depth=100 origin "refs/heads/$BASE_REF:refs/remotes/origin/$BASE_REF" >/dev/null 2>&1 || true + if git rev-parse --verify -q "refs/remotes/origin/$BASE_REF" >/dev/null; then + BASE_RESOLVED="refs/remotes/origin/$BASE_REF" + elif git rev-parse --verify -q "refs/heads/$BASE_REF" >/dev/null; then + BASE_RESOLVED="refs/heads/$BASE_REF" + fi + fi + + if [[ -z "$BASE_RESOLVED" ]]; then + echo "Warning: Could not resolve PR base ref for '$BASE_REF'." + echo "Hint: ensure the base ref is available (e.g., full fetch)." + echo "Skipping version update because PR base could not be resolved." + exit 0 + fi + + # If diff is quiet, there were no changes to VERSION between base and head. + if git diff --quiet "$BASE_RESOLVED...HEAD" -- VERSION; then + echo "No VERSION bump detected in PR range ($BASE_RESOLVED...HEAD). Updating develop version." + source "$(dirname "$0")/update_develop_version.sh" + else + echo "PR includes a VERSION change. Skipping version update." + fi + exit 0 +fi + +# When running locally during an actual commit, skip if nothing is staged. +# In CI, pre-commit typically runs outside of a commit with no staged files, +# so we must not early-exit there. +if [[ "$IN_CI" != "true" && -z $(git diff --cached --name-only) ]]; then echo "There are no changes staged for commit. Skipping version update." exit 0 fi @@ -16,5 +80,3 @@ elif git diff --name-only | grep -q "VERSION"; then elif ! git show --name-only HEAD | grep -q "VERSION"; then source "$(dirname "$0")/update_develop_version.sh" fi - -source "$(dirname "$0")/update_develop_version.sh"