-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters