Skip to content

Commit

Permalink
chore(github): simplify contract-storage-diff workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nbayindirli committed Feb 13, 2025
1 parent 1e790a0 commit 4c02524
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 40 deletions.
85 changes: 85 additions & 0 deletions .github/scripts/check-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash
set -euo pipefail

main() {
# Get all changed files in the PR
if ! CHANGED_FILES=$(git diff --name-only "$1" "$2" | grep "ensure_.*_storage_schema_is_unchanged.golden"); then
echo "No storage schema changes detected"
exit 0
fi

echo "Found changed storage schema files:"
echo "$CHANGED_FILES"

# Check each changed storage schema file
while IFS= read -r file; do
[ -z "$file" ] && continue
check_migration_file "$file"
done <<< "$CHANGED_FILES"

}

check_migration_file() {
local file="$1"

local contract_dir
contract_dir=$(contract_dir "$file")

local migrate_file
migrate_file=$(migrate_file "$contract_dir" "$file")

local test_file
test_file=$(test_file "$contract_dir" "$migrate_file")

verify_legacy_storage_module "$migrate_file"

echo "✓ Found valid migration file for $file"
}

contract_dir() {
local file="$1"
local contract_dir

contract_dir=$(echo "$file" | grep -o 'contracts/stellar-[^/]*')
[ -z "$contract_dir" ] && print_error "Could not determine contract directory for $file"

echo "$contract_dir"
}

migrate_file() {
local contract_dir="$1"
local file="$2"
local migrate_file

migrate_file=$(find "$contract_dir/src" -name "migrate.rs" -not -path "*/test*" | head -n 1)
[ -z "$migrate_file" ] && print_error "Storage schema change detected in $file but no migrate.rs found under $contract_dir/src/
Please create a migration file at $contract_dir/src/migrate.rs to handle the schema changes"

echo "$migrate_file"
}

test_file() {
local contract_dir="$1"
local migrate_file="$2"
local test_file

test_file=$(find "$contract_dir" -path "*/test*/migrate.rs" -o -path "*/tests/migrate.rs" | head -n 1)
[ -z "$test_file" ] && print_error "migrate.rs found at $migrate_file but no corresponding migrate.rs test file found
Please create a test file at $contract_dir/tests/migrate.rs to test the schema migration"

echo "$test_file"
}

verify_legacy_storage_module() {
local migrate_file="$1"

grep -q "mod legacy_storage" "$migrate_file" || print_error "migrate.rs found at $migrate_file but missing required 'legacy_storage' module declaration
Please add a 'legacy_storage' module to handle the schema migration"
}

print_error() {
echo "Error: $1"
exit 1
}

main "$@";
42 changes: 2 additions & 40 deletions .github/workflows/contract-storage-diff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,6 @@ jobs:
fetch-depth: 0

- name: Check for storage schema changes
shell: bash
run: |
# Get all changed files in the PR
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep "ensure_.*_storage_schema_is_unchanged.golden" || true)
if [ -n "$CHANGED_FILES" ]; then
echo "Found changed storage schema files:"
echo "$CHANGED_FILES"
# Check each changed file
EXIT_CODE=0
while IFS= read -r file; do
if [ -n "$file" ]; then
# Extract contract directory by matching contracts/stellar-* pattern
CONTRACT_DIR=$(echo "$file" | grep -o 'contracts/stellar-[^/]*')
if [ -n "$CONTRACT_DIR" ]; then
# Find any migrate.rs under the contract's src directory, excluding test directories
MIGRATE_FILE=$(find "$CONTRACT_DIR/src" -name "migrate.rs" -not -path "*/test*" | head -n 1)
if [ -z "$MIGRATE_FILE" ]; then
echo "Error: Storage schema change detected in $file but no migrate.rs found under $CONTRACT_DIR/src/"
echo "Please create a migration file at $CONTRACT_DIR/src/migrate.rs to handle the schema changes"
EXIT_CODE=1
elif ! grep -q "mod legacy_storage" "$MIGRATE_FILE"; then
echo "Error: migrate.rs found at $MIGRATE_FILE but missing required 'legacy_storage' module declaration"
echo "Please add a 'legacy_storage' module to handle the schema migration"
EXIT_CODE=1
else
echo "✓ Found valid migration file for $file"
fi
else
echo "Error: Could not determine contract directory for $file"
EXIT_CODE=1
fi
fi
done <<< "$CHANGED_FILES"
exit $EXIT_CODE
else
echo "No storage schema changes detected"
fi
chmod +x .github/scripts/check-migration.sh
.github/scripts/check-migration.sh ${{ github.event.pull_request.base.sha }} ${{ github.sha }}

0 comments on commit 4c02524

Please sign in to comment.