From 5f71d49883650dbbfc133729cbf81b8115c820ca Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 27 Oct 2025 16:02:40 +1100 Subject: [PATCH] extracted the previous version extraction logic --- .github/scripts/generate_release_notes.sh | 9 +-- .github/scripts/get_previous_version.sh | 74 +++++++++++++++++++++++ .github/scripts/revert_release.sh | 35 ++--------- 3 files changed, 83 insertions(+), 35 deletions(-) create mode 100755 .github/scripts/get_previous_version.sh diff --git a/.github/scripts/generate_release_notes.sh b/.github/scripts/generate_release_notes.sh index 285efd7..e1837d4 100755 --- a/.github/scripts/generate_release_notes.sh +++ b/.github/scripts/generate_release_notes.sh @@ -77,12 +77,13 @@ fi echo "🤖 Generating release notes using Gemini AI..." # Get previous version from history of src/version.py -LAST_VERSION=$(git show HEAD^:src/version.py | sed -n 's/^__version__ = "\([^"]*\)"/\1/p') -echo "Last version from git history of src/version.py: $LAST_VERSION" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PREV_VERSION=$("$SCRIPT_DIR/get_previous_version.sh" "$VERSION" 2>&1) +echo "Previous version from git history of src/version.py: $PREV_VERSION" # Get commit history since last version tag -echo "Looking for last version tag: v$LAST_VERSION" -PREV_TAG="v$LAST_VERSION" +echo "Looking for previous version tag: v$PREV_VERSION" +PREV_TAG="v$PREV_VERSION" if ! git rev-parse "$PREV_TAG" >/dev/null 2>&1; then echo "❌ Error: Could not find previous tag for $PREV_TAG" diff --git a/.github/scripts/get_previous_version.sh b/.github/scripts/get_previous_version.sh new file mode 100755 index 0000000..d7a6c9f --- /dev/null +++ b/.github/scripts/get_previous_version.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +# get_previous_version.sh - Extract the previous version from git history +# Usage: get_previous_version.sh +# +# Arguments: +# version: The current semver version (e.g., 1.2.3 or 2.0.0-rc.1) +# +# Output: +# Prints the previous version to stdout +# Returns exit code 0 on success, non-zero on failure +# +# This script: +# 1. Finds the commit that introduced the specified version +# 2. Gets the parent commit of that version change +# 3. Extracts the version from the parent commit +# 4. Outputs the previous version + +set -euo pipefail + +# Display usage information +usage() { + echo "Usage: $0 " >&2 + echo "" >&2 + echo "Extracts the previous version from git history before the specified version." >&2 + echo "" >&2 + echo "Arguments:" >&2 + echo " version: The current semver version (e.g., 1.2.3 or 2.0.0-rc.1)" >&2 + echo "" >&2 + echo "Examples:" >&2 + echo " $0 1.2.3 # Get version before 1.2.3" >&2 + echo " $0 2.0.0-rc.1 # Get version before 2.0.0-rc.1" >&2 + exit 1 +} + +# Check if version argument is provided +if [ "$#" -ne 1 ]; then + echo "Error: Version argument required" >&2 + usage +fi + +VERSION="$1" + +# Get the git history of version.py, find the commit that introduced $VERSION +CURRENT_VERSION_COMMIT=$(git log --all --format="%H" --follow -S "__version__ = \"$VERSION\"" -- src/version.py | head -1) + +if [ -z "$CURRENT_VERSION_COMMIT" ]; then + echo "Error: Could not find commit that set version to $VERSION" >&2 + exit 1 +fi + +# Get the previous commit for version.py +PREVIOUS_VERSION_COMMIT=$(git log --all --format="%H" --follow "$CURRENT_VERSION_COMMIT^..HEAD" -- src/version.py | grep -A1 "$CURRENT_VERSION_COMMIT" | tail -1 || true) + +# If we can't find a previous commit in that way, try getting the parent commit and extracting version from there +if [ -z "$PREVIOUS_VERSION_COMMIT" ] || [ "$PREVIOUS_VERSION_COMMIT" = "$CURRENT_VERSION_COMMIT" ]; then + PREVIOUS_VERSION_COMMIT=$(git rev-parse "$CURRENT_VERSION_COMMIT^" 2>/dev/null || true) +fi + +if [ -z "$PREVIOUS_VERSION_COMMIT" ]; then + echo "Error: Could not find previous commit for version.py" >&2 + exit 1 +fi + +# Extract version from that commit +PREVIOUS_VERSION=$(git show "$PREVIOUS_VERSION_COMMIT:src/version.py" | grep -oP '__version__ = "\K[^"]+' || true) + +if [ -z "$PREVIOUS_VERSION" ]; then + echo "Error: Could not extract previous version from commit ${PREVIOUS_VERSION_COMMIT:0:7}" >&2 + exit 1 +fi + +# Output the previous version to stdout +echo "$PREVIOUS_VERSION" diff --git a/.github/scripts/revert_release.sh b/.github/scripts/revert_release.sh index b67bd2c..60316e9 100755 --- a/.github/scripts/revert_release.sh +++ b/.github/scripts/revert_release.sh @@ -155,37 +155,10 @@ fi # Step 4: Extract previous version from git history echo -e "${YELLOW}[4/7] Extracting previous version from git history...${NC}" -# Get the git history of version.py, find the commit before the version we're reverting -# We need to find the commit that changed version to $VERSION, then get the version from the commit before that -CURRENT_VERSION_COMMIT=$(git log --all --format="%H" --follow -S "__version__ = \"$VERSION\"" -- src/version.py | head -1) - -if [ -z "$CURRENT_VERSION_COMMIT" ]; then - echo -e "${RED}Error: Could not find commit that set version to $VERSION${NC}" - exit 1 -fi - -echo " Found version $VERSION in commit: ${CURRENT_VERSION_COMMIT:0:7}" - -# Get the previous commit for version.py -PREVIOUS_VERSION_COMMIT=$(git log --all --format="%H" --follow "$CURRENT_VERSION_COMMIT^..HEAD" -- src/version.py | grep -A1 "$CURRENT_VERSION_COMMIT" | tail -1 || true) - -# If we can't find a previous commit in that way, try getting the parent commit and extracting version from there -if [ -z "$PREVIOUS_VERSION_COMMIT" ] || [ "$PREVIOUS_VERSION_COMMIT" = "$CURRENT_VERSION_COMMIT" ]; then - PREVIOUS_VERSION_COMMIT=$(git rev-parse "$CURRENT_VERSION_COMMIT^" 2>/dev/null || true) -fi - -if [ -z "$PREVIOUS_VERSION_COMMIT" ]; then - echo -e "${RED}Error: Could not find previous commit for version.py${NC}" - exit 1 -fi - -echo " Found previous commit: ${PREVIOUS_VERSION_COMMIT:0:7}" - -# Extract version from that commit -PREVIOUS_VERSION=$(git show "$PREVIOUS_VERSION_COMMIT:src/version.py" | grep -oP '__version__ = "\K[^"]+' || true) - -if [ -z "$PREVIOUS_VERSION" ]; then - echo -e "${RED}Error: Could not extract previous version from commit ${PREVIOUS_VERSION_COMMIT:0:7}${NC}" +# Use the get_previous_version.sh script to extract the previous version +if ! PREVIOUS_VERSION=$("$SCRIPT_DIR/get_previous_version.sh" "$VERSION" 2>&1); then + echo -e "${RED}Error: Failed to extract previous version${NC}" + echo -e "${RED}$PREVIOUS_VERSION${NC}" exit 1 fi