Browse Source

[BRE-1264] Adding support for TOML and a local test file (#461)

pull/462/head
Andy Pixley 2 months ago committed by GitHub
parent
commit
a3f9d2691b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      .github/workflows/test-version-bump.yml
  2. 2
      version-bump/Dockerfile
  3. 30
      version-bump/README.md
  4. 13
      version-bump/main.py
  5. 110
      version-bump/test-local.sh
  6. 9
      version-bump/tests/fixtures/Cargo.toml
  7. 6
      version-bump/tests/fixtures/Chart.yaml

16
.github/workflows/test-version-bump.yml

@ -60,6 +60,20 @@ jobs: @@ -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: @@ -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

2
version-bump/Dockerfile

@ -1,7 +1,7 @@ @@ -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

30
version-bump/README.md

@ -1,7 +1,14 @@ @@ -1,7 +1,14 @@
# 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
@ -12,3 +19,22 @@ with: @@ -12,3 +19,22 @@ 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

13
version-bump/main.py

@ -4,6 +4,7 @@ import plistlib @@ -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): @@ -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__": @@ -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.")

110
version-bump/test-local.sh

@ -0,0 +1,110 @@ @@ -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}"

9
version-bump/tests/fixtures/Cargo.toml vendored

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
[package]
name = "some-project"
version = "0.1.0"
edition = "2021"
[dependencies]
[profile.release]
strip = true

6
version-bump/tests/fixtures/Chart.yaml vendored

@ -0,0 +1,6 @@ @@ -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"
Loading…
Cancel
Save