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 9bad411 commit b99eb5a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 59 deletions.
108 changes: 60 additions & 48 deletions .github/scripts/update_idls.sh
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
50 changes: 39 additions & 11 deletions .github/workflows/idl-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
commit-message: update idl
title: Update IDLs
branch: update-idls

0 comments on commit b99eb5a

Please sign in to comment.