From a3f9d2691b22913f93e34e42b476566ae7106e26 Mon Sep 17 00:00:00 2001 From: Andy Pixley <3723676+pixman20@users.noreply.github.com> Date: Tue, 21 Oct 2025 13:07:32 -0400 Subject: [PATCH] [BRE-1264] Adding support for TOML and a local test file (#461) --- .github/workflows/test-version-bump.yml | 16 ++++ version-bump/Dockerfile | 2 +- version-bump/README.md | 34 +++++++- version-bump/main.py | 13 +++ version-bump/test-local.sh | 110 ++++++++++++++++++++++++ version-bump/tests/fixtures/Cargo.toml | 9 ++ version-bump/tests/fixtures/Chart.yaml | 6 ++ 7 files changed, 185 insertions(+), 5 deletions(-) create mode 100755 version-bump/test-local.sh create mode 100644 version-bump/tests/fixtures/Cargo.toml create mode 100644 version-bump/tests/fixtures/Chart.yaml diff --git a/.github/workflows/test-version-bump.yml b/.github/workflows/test-version-bump.yml index dd667bc5..b21b798a 100644 --- a/.github/workflows/test-version-bump.yml +++ b/.github/workflows/test-version-bump.yml @@ -60,6 +60,20 @@ jobs: version: ${{ env.version_number }} file_path: "./version-bump/tests/fixtures/test.csproj" + - name: Bump TOML Test + id: test_toml + uses: ./version-bump + with: + version: ${{ env.version_number }} + file_path: "./version-bump/tests/fixtures/Cargo.toml" + + - name: Bump Chart.yaml Test + id: test_chart + uses: ./version-bump + with: + version: ${{ env.version_number }} + file_path: "./version-bump/tests/fixtures/Chart.yaml" + - name: Validate Outputs run: | echo "${{ steps.test_json.outputs.status }}" @@ -67,4 +81,6 @@ jobs: echo "${{ steps.test_xml.outputs.status }}" echo "${{ steps.test_props.outputs.status }}" echo "${{ steps.test_csproj.outputs.status }}" + echo "${{ steps.test_toml.outputs.status }}" + echo "${{ steps.test_chart.outputs.status }}" git diff diff --git a/version-bump/Dockerfile b/version-bump/Dockerfile index 37522c4d..453f4a2b 100644 --- a/version-bump/Dockerfile +++ b/version-bump/Dockerfile @@ -1,7 +1,7 @@ FROM python:3-slim AS builder WORKDIR /app -RUN pip3 install --no-cache-dir pyyaml --target=. +RUN pip3 install --no-cache-dir pyyaml tomlkit --target=. ADD ./main.py . FROM gcr.io/distroless/python3-debian12 diff --git a/version-bump/README.md b/version-bump/README.md index 217aef10..5f379e87 100644 --- a/version-bump/README.md +++ b/version-bump/README.md @@ -1,14 +1,40 @@ # Version Bump Action -A Github Action that will replace versions in JSON, PLIST, YAML, and XML files. -Specifically created for interacting with AndroidManifest, iOS development plists, Helm Charts, and Node package JSON files. +A Github Action that will replace versions in JSON, PLIST, YAML, TOML, and XML files. +Specifically created for interacting with AndroidManifest, iOS development plists, Helm Charts, Rust Cargo manifests, and Node package JSON files. + +**Supported file types:** +- JSON: `package.json`, `package-lock.json` (updates `version` and `packages[""].version`) +- PLIST: iOS Info.plist files +- XML: AndroidManifest.xml, .NET project files (.csproj, .props) +- YAML: Helm Chart.yaml files +- TOML: Cargo.toml files (updates `[package].version` only) ## Usage ```yml - name: Bump Android Version -uses: ./version-bump -with: + uses: ./version-bump + with: version: ${{ inputs.version_number }} file_path: "./AndroidManifest.xml" ``` + +## Local Testing + +To build and test the Docker action locally, use the `test-local.sh` script: + +```bash +# Test with default version (2123.4.5) +./test-local.sh + +# Test with a specific version +./test-local.sh 2024.1.0 +``` + +This script will: + +1. Build the Docker image locally +2. Run the action against all test fixtures in `tests/fixtures/` in a copied directory +3. Verify that version updates were applied correctly +4. Display a summary of changes diff --git a/version-bump/main.py b/version-bump/main.py index bf65e0bc..78d00691 100644 --- a/version-bump/main.py +++ b/version-bump/main.py @@ -4,6 +4,7 @@ import plistlib import re import xml.etree.ElementTree as ET import yaml +import tomlkit def get_file_type(file_path): @@ -79,6 +80,16 @@ def update_yaml(version, file_path): with open(file_path, "w") as f: yaml.dump(doc, f) +def update_toml(version, file_path): + with open(file_path, "r") as f: + doc = tomlkit.load(f) + + # Update version in [package] section + if "package" in doc and "version" in doc["package"]: + doc["package"]["version"] = version + + with open(file_path, "w") as f: + tomlkit.dump(doc, f) if __name__ == "__main__": version = os.getenv("INPUT_VERSION") @@ -102,6 +113,8 @@ if __name__ == "__main__": update_plist(version, file_path) elif file_name == "Chart.yaml" or file_name == "Chart.yml": update_yaml(version, file_path) + elif file_type == ".toml": + update_toml(version, file_path) else: raise Exception("No file was recognized as a supported format.") diff --git a/version-bump/test-local.sh b/version-bump/test-local.sh new file mode 100755 index 00000000..de653c99 --- /dev/null +++ b/version-bump/test-local.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +# test-local.sh - Build and test the version-bump Docker action locally +# This script builds the Docker image and runs it against all test fixtures + +set -e + +# Color output (only if terminal supports it) +if [[ -t 1 ]]; then + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[1;33m' + NC='\033[0m' +else + RED='' + GREEN='' + YELLOW='' + NC='' +fi + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Default version to use for testing +VERSION="${1:-2123.4.5}" + +echo -e "${YELLOW}=== Version Bump Docker Action Test ===${NC}" +echo "Testing with version: $VERSION" +echo "" + +# Build the Docker image +DOCKER_TAG="version-bump:test" +echo -e "${YELLOW}Building Docker image...${NC}" +docker build -t "$DOCKER_TAG" . +echo -e "${GREEN}✓ Docker image built successfully${NC}" +echo "" + +# Create a temporary directory for test files +TEST_DIR="$SCRIPT_DIR/tmp-$(date +%s)" +mkdir -p "$TEST_DIR" +echo "Using temporary directory: $TEST_DIR" + +# Copy test fixtures to temp directory +ORIGINAL_TEST_DIR="$SCRIPT_DIR/tests/fixtures" +cp -r "$ORIGINAL_TEST_DIR/"* "$TEST_DIR/" + +# Function to run a test +run_test() { + local file_name="$1" + local file_path="$TEST_DIR/$file_name" + + echo -e "${YELLOW}Testing: $file_name${NC}" + + # Create a temporary file for GitHub output + GITHUB_OUTPUT_NAME=".github_output" + GITHUB_OUTPUT="$TEST_DIR/$GITHUB_OUTPUT_NAME" + CONTAINER_WORKSPACE="/workspace" + + # Run the Docker container + docker run --rm \ + -v "$TEST_DIR:$CONTAINER_WORKSPACE" \ + -e INPUT_VERSION="$VERSION" \ + -e INPUT_FILE_PATH="$CONTAINER_WORKSPACE/$file_name" \ + -e GITHUB_OUTPUT="$CONTAINER_WORKSPACE/$GITHUB_OUTPUT_NAME" \ + "$DOCKER_TAG" + + # Check if the file was modified + if grep -q "$VERSION" "$file_path"; then + echo -e "${GREEN}✓ $file_name updated successfully${NC}" + + # Show the changes + echo " Changes in $file_name:" + grep -n "$VERSION" "$file_path" | head -3 | sed 's/^/ /' + else + echo -e "${RED}✗ $file_name was not updated${NC}" + exit 1 + fi + + # Clean up + echo " GITHUB_OUTPUT:" + echo " $(cat "$GITHUB_OUTPUT")" + rm -f "$GITHUB_OUTPUT" + echo "" +} + +# Run tests for each fixture file +echo -e "${YELLOW}=== Running Tests ===${NC}" +echo "" + +run_test "package-lock.json" +run_test "Info.plist" +run_test "AndroidManifest.xml" +run_test "dir.build.props" +run_test "test.csproj" +run_test "Cargo.toml" +run_test "Chart.yaml" + +# Show git diff style output +echo -e "${YELLOW}=== File Changes Summary ===${NC}" +for file in "$TEST_DIR"/*; do + if [[ -f "$file" && $(basename "$file") != ".gitignore" ]]; then + echo -e "${YELLOW}$(basename "$file"):${NC}" + grep -n "$VERSION" "$file" | head -2 | sed 's/^/ /' + fi +done + +echo "" +echo -e "${GREEN}=== All Tests Passed! ===${NC}" +echo -e "${GREEN}The Docker image works correctly with version: $VERSION${NC}" diff --git a/version-bump/tests/fixtures/Cargo.toml b/version-bump/tests/fixtures/Cargo.toml new file mode 100644 index 00000000..88f8fc70 --- /dev/null +++ b/version-bump/tests/fixtures/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "some-project" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[profile.release] +strip = true diff --git a/version-bump/tests/fixtures/Chart.yaml b/version-bump/tests/fixtures/Chart.yaml new file mode 100644 index 00000000..14ac836b --- /dev/null +++ b/version-bump/tests/fixtures/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: example-chart +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 +appVersion: "1.16.0"