Skip to content

Commit

Permalink
fix check runtime for main & releases (#513)
Browse files Browse the repository at this point in the history
* fix check runtime for main & releases

* fix diffs

* conditionl artifact build

* fix syntax

* fix syntax

* testing

* take 2

* take 3

* seun 1 - 0 CI

* needs-benchmarks

* Update .maintain/client_release.sh

* fix release notes
  • Loading branch information
Seun Lanlege authored Jan 20, 2022
1 parent da375ee commit 052ae7f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 136 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Run Benchmark
name: Run Benchmarks

on:
workflow_dispatch:
on:
pull_request:
branches:
- releases
- main

jobs:
benchmark:
Expand All @@ -27,11 +30,12 @@ jobs:
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
id: check_runtime
if: github.event.label.name == 'needs-benchmarks'
run: .maintain/check_runtime.sh

- name: Benchmark Test
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
if: "$${{ env.RUNTIME_CHECK }} == 1"
if: env.RUNTIME_CHECK == 1
id: run_benchmarks
run: .maintain/run_benchmarks.sh
5 changes: 1 addition & 4 deletions .github/workflows/simnode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ jobs:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
run: .maintain/check_runtime.sh

- name: Echo output
run: echo ${{ env.RUNTIME_CHECK }}

- name: Run Simnode
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
if: "$${{ env.RUNTIME_CHECK }} == 1"
if: env.RUNTIME_CHECK == 1
id: run_simnode
run: .maintain/run_simnode.sh
11 changes: 5 additions & 6 deletions .maintain/check_runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ VERSIONS_FILES=(
"runtime/composable/src/lib.rs,composable,composable"
)

echo "latest 10 commits of ${GITHUB_REF_NAME}"
git log --graph --oneline --decorate=short -n 10

echo "make sure the main branch and release tag are available in shallow clones"
git fetch --depth="${GIT_DEPTH:-100}" origin "${BASE_BRANCH}"

simnode_check() {
VERSIONS_FILE="$1"
if has_runtime_changes "${BASE_BRANCH}" "${GITHUB_REF_NAME}" "$3" && check_runtime "$VERSIONS_FILE" "$2"
if has_runtime_changes "${BASE_BRANCH}" "${GITHUB_REF_NAME}" "$2" && check_runtime "$VERSIONS_FILE" "$2"
then
echo "Wasm sources have changed"
echo "RUNTIME_CHECK=1" >> "$GITHUB_ENV"
echo "RUNTIME_CHECK=1" >> $GITHUB_ENV
else
echo "RUNTIME_CHECK=0" >> $GITHUB_ENV
fi
}

for i in "${VERSIONS_FILES[@]}"; do
while IFS=',' read -r output chain folder; do
echo "check if the wasm sources changed for $chain"
boldprint "Check if the wasm sources changed for $chain"
simnode_check $output $folder
done <<< "$i"
done
Expand Down
5 changes: 4 additions & 1 deletion .maintain/client_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
. "$(dirname "${0}")/./common/lib.sh"

RELEASE_VERSION=$(git tag --sort=committerdate | grep -E '^v[0-9]' | tail -1 )
# Because this script runs when a tag has been published, the previous tag is the
# last two tags
PREV_TAG=$(gh release list -L=2 | sed -n '2 p' | awk '{print $(NF-1)}')


if has_client_changes "${BASE_BRANCH}" "${GITHUB_REF_NAME}"
if has_client_changes "${PREV_TAG}" "${GITHUB_REF_NAME}"
then
boldprint "Building new client binaries"
cargo build --release -p composable
Expand Down
130 changes: 13 additions & 117 deletions .maintain/common/lib.sh
Original file line number Diff line number Diff line change
@@ -1,132 +1,35 @@
#!/bin/bash

api_base="https://api.github.com/repos"
GITHUB_REF_NAME=$(git rev-parse HEAD)

# Function to take 2 git tags/commits and get any lines from commit messages
# that contain something that looks like a PR reference: e.g., (#1234)
sanitised_git_logs() {
git --no-pager log --pretty=format:"%s" "$1...$2" |
# Only find messages referencing a PR
grep -E '\(#[0-9]+\)' |
# Strip any asterisks
sed 's/^* //g' |
# And add them all back
sed 's/^/* /g'
}
GITHUB_REF_NAME=$(git rev-parse --abbrev-ref HEAD)

get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
LATEST_TAG_NAME=$(get_latest_release ComposableFi/composable)

if [[ $BASE_BRANCH == "releases" ]] # this is a pr to the releases branch, use the latest tag instead
then
BASE_BRANCH=$LATEST_TAG_NAME
git tag -f "${BASE_BRANCH}" FETCH_HEAD
git log -n1 "${BASE_BRANCH}"
fi

# Returns the last published release on github
# Note: we can't just use /latest because that ignores prereleases
# repo: 'organization/repo'
# Usage: last_github_release "$repo"
last_github_release() {
i=0
# Iterate over releases until we find the last release that's not just a draft
while [ $i -lt 29 ]; do
out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$1/releases" | jq ".[$i]")
echo "$out"
# Ugh when echoing to jq, we need to translate newlines into spaces :/
if [ "$(echo "$out" | tr '\r\n' ' ' | jq '.draft')" = "false" ]; then
echo "$out" | tr '\r\n' ' ' | jq '.tag_name'
return
else
i=$((i + 1))
fi
done
}


# Checks whether a tag on github has been verified
# repo: 'organization/repo'
# tagver: 'v1.2.3'
# Usage: check_tag $repo $tagver
check_tag() {
repo=$1
tagver=$2
tag_out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/git/refs/tags/$tagver")
tag_sha=$(echo "$tag_out" | jq -r .object.sha)
object_url=$(echo "$tag_out" | jq -r .object.url)
if [ "$tag_sha" = "null" ]; then
return 2
fi
verified_str=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$object_url" | jq -r .verification.verified)
if [ "$verified_str" = "true" ]; then
# Verified, everything is good
return 0
else
# Not verified. Bad juju.
return 1
fi
}


# Checks whether a given PR has a given label.
# repo: 'organization/repo'
# pr_id: 12345
# label: B1-silent
# Usage: has_label $repo $pr_id $label
has_label() {
repo="$1"
pr_id="$2"
label="$3"

# These will exist if the function is called in Gitlab.
# If the function's called in Github, we should have GITHUB_ACCESS_TOKEN set
# already.
if [ -n "$GITHUB_RELEASE_TOKEN" ]; then
GITHUB_TOKEN="$GITHUB_RELEASE_TOKEN"
elif [ -n "$GITHUB_PR_TOKEN" ]; then
GITHUB_TOKEN="$GITHUB_PR_TOKEN"
fi

out=$(curl -H "Authorization: token $GITHUB_TOKEN" -s "$api_base/$repo/pulls/$pr_id")
[ -n "$(echo "$out" | tr -d '\r\n' | jq ".labels | .[] | select(.name==\"$label\")")" ]
}

# Formats a message into a JSON string for posting to Matrix
# message: 'any plaintext message'
# formatted_message: '<strong>optional message formatted in <em>html</em></strong>'
# Usage: structure_message $content $formatted_content (optional)
structure_message() {
if [ -z "$2" ]; then
body=$(jq -Rs --arg body "$1" '{"msgtype": "m.text", $body}' </dev/null)
else
body=$(jq -Rs --arg body "$1" --arg formatted_body "$2" '{"msgtype": "m.text", $body, "format": "org.matrix.custom.html", $formatted_body}' </dev/null)
fi
echo "$body"
boldprint() { printf "|\n| \033[1m%s\033[0m\n|\n" "${@}"; }
boldcat() {
printf "|\n"
while read -r l; do printf "| \033[1m%s\033[0m\n" "${l}"; done
printf "|\n"
}

# Post a message to a matrix room
# body: '{body: "JSON string produced by structure_message"}'
# room_id: !fsfSRjgjBWEWffws:matrix.parity.io
# access_token: see https://matrix.org/docs/guides/client-server-api/
# Usage: send_message $body (json formatted) $room_id $access_token
send_message() {
curl -XPOST -d "$1" "https://matrix.parity.io/_matrix/client/r0/rooms/$2/send/m.room.message?access_token=$3"
}
LATEST_TAG_NAME=$(get_latest_release ComposableFi/composable)
#LATEST_TAG_NAME=$(gh release list -L=5 | sed -n '5 p' | awk '{print $(NF-1)}')
boldprint $LATEST_TAG_NAME
git fetch origin tag "${LATEST_TAG_NAME}" --no-tags

# Check for runtime changes between two commits. This is defined as any changes
# to runtime/, frame/
has_runtime_changes() {
from=$1
to=$2
echo "diffing $from & $to"
if git diff --name-only "${from}...${to}" |
grep -q -e '^frame/' -e "^runtime/$3/"; then
return 0

else
return 1
fi
Expand All @@ -145,13 +48,6 @@ has_client_changes() {
fi
}

boldprint() { printf "|\n| \033[1m%s\033[0m\n|\n" "${@}"; }
boldcat() {
printf "|\n"
while read -r l; do printf "| \033[1m%s\033[0m\n" "${l}"; done
printf "|\n"
}

# checks if the spec/impl version has increased
check_runtime() {
VERSIONS_FILE="$1"
Expand Down Expand Up @@ -203,6 +99,6 @@ check_runtime() {
versions file: ${VERSIONS_FILE}
EOT
return 1
exit 1 # Exit because user needs to bump spec version
fi
}
13 changes: 9 additions & 4 deletions .maintain/runtime_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
# shellcheck disable=SC2039
VERSIONS_FILES=(
"runtime/picasso/src/lib.rs,picasso,picasso"
# "runtime/dali/src/lib.rs,dali-chachacha,dali"
# "runtime/composable/src/lib.rs,composable,composable"
"runtime/dali/src/lib.rs,dali-chachacha,dali"
"runtime/composable/src/lib.rs,composable,composable"
)
# Because this script runs when a tag has been published, the previous tag is the
# last two tags
PREV_TAG=$(gh release list -L=2 | sed -n '2 p' | awk '{print $(NF-1)}')
CURRENT_TAG=$(gh release list -L=1 | sed -n '1 p' | awk '{print $(NF-1)}')

# Install the neccessary tools needed for building
cargo install --git https://github.com/chevdor/srtool-cli
Expand All @@ -35,10 +39,11 @@ build_runtime () {
for i in "${VERSIONS_FILES[@]}"; do
while IFS=',' read -r output chain folder; do
echo "check if the wasm sources changed for $chain"
if has_runtime_changes "${BASE_BRANCH}" "${GITHUB_REF_NAME}" "$folder"
if has_runtime_changes "${PREV_TAG}" "${GITHUB_REF_NAME}" "$folder"
then
build_runtime $output $chain $folder

CHANGES=gh view release tag $CURRENT_TAG
echo $CHANGES | sed '1,/--/ d' >> release.md
echo "$chain-wasm=1" >> "$GITHUB_ENV"
fi
done <<< "$i"
Expand Down

0 comments on commit 052ae7f

Please sign in to comment.