From b99eb5a8fadd86181cda54eb04a8e72cab7bb9be Mon Sep 17 00:00:00 2001 From: Louis Thiery Date: Tue, 30 Jan 2024 14:36:15 -0800 Subject: [PATCH] automatically update idls --- .github/scripts/update_idls.sh | 108 +++++++++++++++++-------------- .github/workflows/idl-update.yml | 50 ++++++++++---- 2 files changed, 99 insertions(+), 59 deletions(-) diff --git a/.github/scripts/update_idls.sh b/.github/scripts/update_idls.sh index cf713d6..d882909 100755 --- a/.github/scripts/update_idls.sh +++ b/.github/scripts/update_idls.sh @@ -1,35 +1,25 @@ #!/bin/bash -# all programs -PROGRAMS=( -#circuit_breaker -#data_credits -#fanout -#helium_entity_manager -helium_sub_daos -#lazy_distributor -#lazy_transactions -#mobile_entity_manager -#no_emit -#price_oracle -#rewards_oracle -#treasury_management -#voter_stake_registry -) -OWNER=helium +OWNER=lthiery REPO=helium-program-library function get_tags() { PROGRAM=$1 PAGE=$2 URL="https://api.github.com/repos/${OWNER}/${REPO}/tags?page=$PAGE" - curl -s \ + # sort alphabetically, reverse, and filter by program name + LIST=$(curl -s \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${GITHUB_TOKEN}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ "$URL" \ - | jq -r --arg P "$PROGRAM" '.[].name | select(startswith($P))' | head -n 1 + | jq -r --arg P "$PROGRAM" '.[].name | select(startswith($P))') + for ((i=0; i<${#LIST[@]}; i++)); do + LIST[$i]=$(echo "${LIST[$i]}" | sed 's/^\(.*-\)v\(.*\)$/\1\2/') + done + # sort the list and return the top + echo "${LIST[@]}" | tr ' ' '\n' | sort -r | head -n 1 } function update_idl() { @@ -39,41 +29,63 @@ function update_idl() { # download the json idl file from the tags assets URL="https://github.com/${OWNER}/${REPO}/releases/download/${TAG}/${PROGRAM}.json" - echo $URL curl -L \ - -o "idl/${PROGRAM}.json" \ + -o "idl/${PROGRAM}.json.tmp" \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${GITHUB_TOKEN}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ - $URL + "$URL" + status_code=$? + + # if curl failed, exit + if [ $status_code -ne 0 ]; then + echo "Download failed" + exit 1 + fi + # otherwise, rename file + mv "idl/${PROGRAM}.json.tmp" "idl/${PROGRAM}.json" + + # write the new tag to the Cargo.toml + VERSION=$(version_from_tag "$TAG") + PROGRAM_DIR=${PROGRAM//_/-} + sed -i -e "s/^version = \".*\"/version = \"VERSION\"/" "programs/$PROGRAM_DIR/Cargo.toml" + exit 0 +} - # check git for diff +function get_cargo_version() { + PROGRAM=$1 + PROGRAM_DIR=${PROGRAM//_/-} + # get the version from the Cargo.toml file + CARGO_VERSION=$(grep -m 1 "version" "programs/$PROGRAM_DIR/Cargo.toml" | sed -e "s/version = \"//" -e "s/\"//") + echo "$CARGO_VERSION" +} + +function version_from_tag() { + TAG=$1 + # extract the version from the name (eg program-data-credits-0.2.1 -> 0.2.1) + VERSION=$(echo "$TAG" | sed -e "s/^$PREFIX-//") + echo "$VERSION" } -# loop through programs -for P in "${PROGRAMS[@]}" -do - # replace underscores with hyphens - PREFIX=program-${P//_/-} - PAGE=1 - while true; do - TAG=$(get_tags "$PREFIX" "$PAGE") - # if tag is found, break - if [ -n "$TAG" ]; then - echo "tag=$TAG" - # extract the version from the name (eg program-data-credits-0.2.1 -> 0.2.1) - VERSION=$(echo "$TAG" | sed -e "s/^$PREFIX-//") - echo "version=$VERSION" - PROGRAM_DIR=${P//_/-} - CARGO_VERSION=$(grep -m 1 "version" "programs/$PROGRAM_DIR/Cargo.toml" | sed -e "s/version = \"//" -e "s/\"//") - echo "cargo_version=$CARGO_VERSION" - # if VERSION and CARGO_VERSION are different, update the JSON - if [ "$VERSION" != "$CARGO_VERSION" ]; then - update_idl "${P}" "${TAG}" - fi - break +P=$1 +# replace underscores with hyphens +PREFIX=program-${P//_/-} +PAGE=1 +while true; do + echo "prefix=$PREFIX page=$PAGE" + TAG=$(get_tags "$PREFIX" "$PAGE") + if [ -n "$TAG" ]; then + # extract the version from the name (eg program-data-credits-0.2.1 -> 0.2.1) + VERSION=$(version_from_tag "$TAG") + echo "version=$VERSION" + CARGO_VERSION=$(get_cargo_version "$P") + echo "cargo_version=$CARGO_VERSION" + # if VERSION and CARGO_VERSION are different, update the JSON + if [ "$VERSION" != "$CARGO_VERSION" ]; then + update_idl "${P}" "${TAG}" fi - # if tag is not found, increment page - PAGE=$((PAGE+1)) - done + break + fi + # if tag is not found, increment page + PAGE=$((PAGE+1)) done diff --git a/.github/workflows/idl-update.yml b/.github/workflows/idl-update.yml index 1df1666..ecbcbd5 100644 --- a/.github/workflows/idl-update.yml +++ b/.github/workflows/idl-update.yml @@ -3,19 +3,47 @@ on: schedule: - cron: '0 0 * * 0' jobs: - updateFork: + build: runs-on: ubuntu-latest + + strategy: + matrix: + program: [ circuit_breaker, + data_credits, + fanout, + helium_entity_manager, + helium_sub_daos, + lazy_distributor, + lazy_transactions, + mobile_entity_manager, + no_emit, + price_oracle, + rewards_oracle, + treasury_management, + voter_stake_registry + ] + steps: - - uses: actions/checkout@v4 - with: - repository: fork-owner/repo - - name: Reset the default branch with upstream changes - run: | - git remote add upstream https://github.com/owner/repo.git - git fetch upstream main:upstream-main - git reset --hard upstream-main + - uses: actions/checkout@v3 + + # the script replaces the json file with a newly downloaded one + # and updates the version in Cargo.toml + - name: Update IDLs + id: update-idls + run: + .github/workflows/idl-update.sh ${{ matrix.program }} + + # if the idl download occurred, we trigger a build which generates new Rust code + - name: Cargo Build + id: cargo-build + if: steps.update-idls.outputs == 'success' + run: cargo build --verbose + + # creates a pull request with the updated idl - name: Create Pull Request + if: steps.cargo-build.outputs == 'success' uses: peter-evans/create-pull-request@v5 with: - token: ${{ secrets.PAT }} - branch: upstream-changes \ No newline at end of file + commit-message: update idl + title: Update IDLs + branch: update-idls \ No newline at end of file