Skip to content

Commit

Permalink
automatically update idls
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery committed Jan 30, 2024
1 parent c583da6 commit 94dc585
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .github/scripts/idl-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

OWNER=lthiery
REPO=helium-program-library

function get_tags() {
PROGRAM=$1
PAGE=$2
URL="https://api.github.com/repos/${OWNER}/${REPO}/tags?page=$PAGE"
# 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))')
# check if curl command failed
status_code=$?
if [ $status_code -ne 0 ]; then
echo "Curl failed"
exit 1
fi

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() {
PROGRAM=$1
TAG=$2
echo "updating idl $PROGRAM to $TAG"
# download the json idl file from the tags assets
URL="https://github.com/${OWNER}/${REPO}/releases/download/${TAG}/${PROGRAM}.json"

curl -L \
-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"
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
}

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"
}

P=$1
# replace underscores with hyphens
PREFIX=program-${P//_/-}
PAGE=1
while true; do
echo "prefix=$PREFIX page=$PAGE"
TAG=$(get_tags "$PREFIX" "$PAGE")
# the first page of tags with the tag will be the latest
# since the tags endpoint sorts alphabetically
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
break
fi
# if tag is not found, increment page
PAGE=$((PAGE+1))
done
53 changes: 53 additions & 0 deletions .github/workflows/idl-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Update IDL
on:
schedule:
# every 5 minutes
- cron: '*/5 * * * *'

jobs:
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@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
env:
GITHUB_TOKEN: ${{ github.token }}
run: .github/scripts/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:
commit-message: update idl
title: Update IDLs
branch: update-idls
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Rust](https://github.com/lthiery/helium-anchor-gen/actions/workflows/rust.yml/badge.svg)](https://github.com/lthiery/helium-anchor-gen/actions/workflows/rust.yml)

# helium-anchor-gen

Generates a Rust CPI client for the Helium Program Library. This is intended to avoid the strict dependencies declared in the solana-program-library that gets inherited by the helium-program-library.
Expand Down

0 comments on commit 94dc585

Please sign in to comment.