Skip to content

Commit

Permalink
Merge pull request #107 from AztecProtocol/testing-updates
Browse files Browse the repository at this point in the history
test
  • Loading branch information
catmcgee authored Jan 9, 2024
2 parents 938be0c + 04528ed commit fb1d28e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/scripts/get_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Check if GITHUB_TOKEN is set
if [ -n "$GITHUB_TOKEN" ]; then
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
else
AUTH_HEADER=""
fi

# Fetch the release data from GitHub API
RELEASES_JSON=$(curl -s -H "$AUTH_HEADER" "https://api.github.com/repos/AztecProtocol/aztec-packages/releases")

# Use jq to parse JSON and get the latest tag
LATEST_TAG=$(echo "$RELEASES_JSON" | jq -r '.[] | select(.tag_name | contains("aztec-packages")) | .tag_name' | head -1)

echo "$LATEST_TAG" # Output the latest tag
109 changes: 109 additions & 0 deletions .github/scripts/update_contract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/bin/bash

# Function to print an error message and exit
error_exit() {
echo "Error: $1" 1>&2
exit 1
}

# Accept version tag as an argument
version_tag="$1"
if [ -z "$version_tag" ]; then
error_exit "No version tag provided."
fi

# Base directories to search through
base_dirs=("../tutorials" "../workshops")

# Repository details
repo_url="https://github.com/AztecProtocol/aztec-packages.git"
contracts_path="yarn-project/noir-contracts/contracts"

# Dynamic Temporary Directory Based on Environment
if [ "$GITHUB_ACTIONS" == "true" ]; then
tmp_dir="$GITHUB_WORKSPACE/tmp"
else
tmp_dir="tmp"
fi

# Clone the repository into the determined tmp folder once at the beginning
if ! git clone "$repo_url" "$tmp_dir"; then
error_exit "Failed to clone the repository."
fi

if ! cd "$tmp_dir" || ! git checkout "$version_tag"; then
error_exit "Failed to checkout the specified version."
fi
cd ..

# Loop through each base directory
for base_dir in "${base_dirs[@]}"; do
echo "Processing $base_dir..."

# Find directories containing Nargo.toml and loop through them
while IFS= read -r nargo_file_path; do
echo "Processing file: $nargo_file_path"

# Check if the Nargo.toml file exists
if [ ! -f "$nargo_file_path" ]; then
echo "Warning: File not found: $nargo_file_path"
continue
fi

# Update the tag in the Nargo.toml file
while IFS= read -r line; do
if [[ $line == *tag=* ]]; then
dependency_name=$(echo $line | grep -oP '(?<=\").+?(?=\")' | head -1)
sed -i "s|\($dependency_name.*tag=\"\)[^\"]*|\1$version_tag|" $nargo_file_path
echo "Updated tag for $dependency_name to $version_tag"
fi
done < <(
sed -n '/^\[dependencies\]/,/^$/p' $nargo_file_path | grep -v '^\[dependencies\]' | awk NF
)
# Extract the directory path
project_dir=$(dirname "$nargo_file_path")
echo "Found project: $project_dir"

# Extract the value of the 'name' field
name_value=$(grep "^name\s*=" "$nargo_file_path" | cut -d '"' -f 2)

# Check if name_value is not empty
if [ -z "$name_value" ]; then
echo "Warning: Name field not found or empty in the TOML file."
continue
else
echo "The value of the 'name' field is: $name_value"
fi

# Check if the directory exists in the cloned repo
if [ -d "$tmp_dir/$contracts_path/$name_value" ]; then
echo "Directory found: $name_value"

# Define copy location
copy_location="$project_dir/$name_value"
mkdir -p "$copy_location"

# Copy the contracts
if ! cp -r "$tmp_dir/$contracts_path/$name_value/src/"* "$copy_location/"; then
echo "Warning: Failed to copy files to $copy_location"
continue
fi

echo "Copied the contracts to $copy_location"

# Remove docs comments from the files
find "$copy_location" -type f -name "*.nr" | while read file; do
if ! sed -i '' '/[ \t]*\/\/ docs:.*/d' "$file"; then
echo "Warning: Failed to remove comments from $file"
else
echo "Comments removed from $file"
fi
done
else
echo "Warning: Directory not found: $name_value"
fi
done < <(find "$base_dir" -name "Nargo.toml")
done

# Remove temporary files after processing is complete
rm -rf "$tmp_dir"
40 changes: 40 additions & 0 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Update Contracts

on:
schedule:
# Runs at 05:00 UTC every day
- cron: "0 5 * * *"
workflow_dispatch:
inputs:
input1:
description: "Just run it."
required: false

permissions: write-all

jobs:
update_contract:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Get version
id: versions_step
run: |
version_tag=$(node ./.github/scripts/fetchRelease.js)
echo "::set-output name=version_tag::$version_tag"
echo "Output from Node.js script: $version_tag"
- name: Check and update tutorials
run: sudo chown $USER ./src && bash ./.github/scripts/update_contract.sh ${{ steps.versions_step.outputs.version_tag }}

- name: Commit and push changes
run: |
git config --global user.email "[email protected]"
git config --global user.name "catmcgee"
git add .
git commit -m "Github Action: Update contract" -a || echo "No changes to commit"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit fb1d28e

Please sign in to comment.