From 1b7cc391d6b4270367d9d2a466018dc5c8d26385 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 12 Aug 2023 11:07:39 -0400 Subject: [PATCH 1/4] add scripts for releases to github --- scripts/release/add_notes_changelog.sh | 106 ++++++++++++++++ scripts/release/github_release.sh | 105 +++++++++++++++ scripts/release/github_utils.sh | 169 +++++++++++++++++++++++++ scripts/release/release.sh | 122 ++++++++++++++++++ scripts/release/utils.sh | 26 ++++ scripts/release/versioning.sh | 95 ++++++++++++++ 6 files changed, 623 insertions(+) create mode 100755 scripts/release/add_notes_changelog.sh create mode 100755 scripts/release/github_release.sh create mode 100644 scripts/release/github_utils.sh create mode 100755 scripts/release/release.sh create mode 100755 scripts/release/utils.sh create mode 100755 scripts/release/versioning.sh diff --git a/scripts/release/add_notes_changelog.sh b/scripts/release/add_notes_changelog.sh new file mode 100755 index 0000000..d0f7594 --- /dev/null +++ b/scripts/release/add_notes_changelog.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +#### +# Utils +#### +source ${BASH_SOURCE%/*}/utils.sh +source ${BASH_SOURCE%/*}/github_utils.sh +### + +# 1. Get options + +## Defaults +APPLY="false" + +while [[ $# -gt 0 ]]; do + case $1 in + -A|--apply) + APPLY="true" + shift # past argument + ;; + -P|--previous-version-tag) + PREV_TAG_VERSION="$2" + shift # past argument + shift # past value + ;; + -V|--version) + VERSION="$2" + shift # past argument + shift # past value + ;; + -T|--github-token) + GITHUB_TOKEN="$2" + shift # past argument + shift # past value + ;; + -B|--release-branch) + RELEASE_BRANCH="$2" + shift # past argument + shift # past value + ;; + -*|--*) + echo "Unknown option $1" + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; + esac +done + +if [[ -z $GITHUB_TOKEN && $APPLY == "true" ]]; then + echo_error "Github token required (-T, --github-token)" + exit 1 +fi + +if [[ -z $PREV_TAG_VERSION ]]; then + echo_error "Previous version tag required (-P, --previous-version-tag)" + exit 1 +fi + +if [[ -z $VERSION ]]; then + echo_error "Version to release required (-V, --version)" + exit 1 +fi + +if [[ -z $RELEASE_BRANCH ]]; then + echo_warning "Release branch not specified with (-B, --release-branch) assuming: release/$VERSION" + RELEASE_BRANCH=release/$VERSION +fi + +DATE=$(date +"%Y-%m-%d") +RELEASE_NAME="$VERSION / $DATE" +TAG_NAME=v$VERSION +PREV_TAG_NAME=v$PREV_TAG_VERSION + +# 2.2. Generate release notes +if [[ $APPLY == "true" ]]; then + echo_info "Generating Github release notes" + RESPONSE=$(generate_github_release_notes_for_changelog $GITHUB_TOKEN) + DESCRIPTION=$(echo $RESPONSE | jq '.body' | tail -1 | sed "s/\"//g") + + if [ $(echo $RESPONSE | jq '.body' | wc -l) -eq 1 ]; then + if [ $(echo $RESPONSE | jq '.' | grep 'documentation_url' | wc -l) -gt 0 ]; then + echo_error "Something went wrong generating Github release notes" + echo $RESPONSE | jq --slurp '.[0]' + exit 1 + fi + + if [ $(echo $RESPONSE | jq '.type' | grep 'error' | wc -l) -gt 0 ]; then + echo_error "Something went wrong generating Github release notes" + echo $RESPONSE | jq --slurp '.[1]' + exit 1 + fi + fi +else + echo_warning "Dry run execution. Not generating Github release notes" +fi + +if [[ $APPLY == "true" ]]; then + echo_info "Adding release notes to CHANGELOG.md" + sed -i "2 i\\\n## $RELEASE_NAME" CHANGELOG.md + sed -i "4 i\\\n$DESCRIPTION\n" CHANGELOG.md +else + echo_warning "Dry run execution. Not adding release notes to CHANGELOG.md" +fi \ No newline at end of file diff --git a/scripts/release/github_release.sh b/scripts/release/github_release.sh new file mode 100755 index 0000000..08a551e --- /dev/null +++ b/scripts/release/github_release.sh @@ -0,0 +1,105 @@ +#!/bin/bash + +#### +# Utils +#### +source ${BASH_SOURCE%/*}/utils.sh +source ${BASH_SOURCE%/*}/github_utils.sh +### + +# 1. Get options + +## Defaults +APPLY="false" + +while [[ $# -gt 0 ]]; do + case $1 in + -A|--apply) + APPLY="true" + shift # past argument + ;; + -P|--previous-version-tag) + PREV_TAG_VERSION="$2" + shift # past argument + shift # past value + ;; + -V|--version) + VERSION="$2" + shift # past argument + shift # past value + ;; + -T|--github-token) + GITHUB_TOKEN="$2" + shift # past argument + shift # past value + ;; + -*|--*) + echo "Unknown option $1" + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; + esac +done + +if [[ -z $GITHUB_TOKEN && apply == "true" ]]; then + echo_error "Github token required (-T, --github-token)" + exit 1 +fi + +if [[ -z $PREV_TAG_VERSION ]]; then + echo_error "Previous version tag required (-P, --previous-version-tag)" + exit 1 +fi + +if [[ -z $VERSION ]]; then + echo_error "Version to release required (-V, --version)" + exit 1 +fi + +# 2. Github +DATE=$(date +"%Y-%m-%d") +RELEASE_NAME="$VERSION / $DATE" +PREV_TAG_NAME=$PREV_TAG_VERSION +TAG_NAME=v$VERSION + +# 2.1 Create Git tag for the repository +if [[ $APPLY == "true" ]]; then + echo_info "Tagging repository" + tag_repository $TAG_NAME +else + echo_warning "Dry run execution. Not tagging Github repo" +fi + +# 2.2. Generate release notes +if [[ $APPLY == "true" ]]; then + echo_info "Generating Github release notes" + RESPONSE=$(generate_github_release_notes $GITHUB_TOKEN) + DESCRIPTION=$(echo $RESPONSE | jq '.body' | tail -1 | sed "s/\"//g") + + if [ $(echo $RESPONSE | jq '.body' | wc -l) -eq 1 ]; then + if [ $(echo $RESPONSE | jq '.' | grep 'documentation_url' | wc -l) -gt 0 ]; then + echo_error "Something went wrong generating Github release notes" + echo $RESPONSE | jq --slurp '.[0]' + exit 1 + fi + + if [ $(echo $RESPONSE | jq '.type' | grep 'error' | wc -l) -gt 0 ]; then + echo_error "Something went wrong generating Github release notes" + echo $RESPONSE | jq --slurp '.[1]' + exit 1 + fi + fi +else + echo_warning "Dry run execution. Not generating Github release notes" +fi + +# 2.3 Create Github release +if [[ $APPLY == "true" ]]; then + echo_info "Generating Github release" + create_github_release $GITHUB_TOKEN +else + echo_warning "Dry run execution. Not creating Github release" +fi \ No newline at end of file diff --git a/scripts/release/github_utils.sh b/scripts/release/github_utils.sh new file mode 100644 index 0000000..243449d --- /dev/null +++ b/scripts/release/github_utils.sh @@ -0,0 +1,169 @@ +#!/bin/bash + +#### +# Utils +#### +source ${BASH_SOURCE%/*}/utils.sh + +# +# Params: +# - First positional argument: version of the tag +# +function tag_repository() +{ + VERSION=$1 + + if [[ -z $VERSION ]]; then + echo_error "tag_repository needs VERSION" + exit 1 + fi + + git tag -a $VERSION -m "Release $VERSION" + git push origin --tags +} + +# +# Params: +# - First positional argument: version of the tag +# +function remove_tag() +{ + VERSION=$1 + + if [[ -z $VERSION ]]; then + echo_error "remove_tag needs VERSION" + exit 1 + fi + + git tag -d $VERSION + git push --delete origin $VERSION +} + +# +# Needs: +# - TAG_NAME +# - PREV_TAG_NAME +# - RELEASE_NAME +# +function generate_github_release_notes_post_data() +{ + cat < /dev/null +} \ No newline at end of file diff --git a/scripts/release/release.sh b/scripts/release/release.sh new file mode 100755 index 0000000..1140be1 --- /dev/null +++ b/scripts/release/release.sh @@ -0,0 +1,122 @@ +#!/bin/bash + +# +# In this script you are going to find the process of releasing bittensor. +# +# This script needs: +# - An existing __version__ var in the __init__.py file +# - Version in __version__ var is not a git tag already +# +# This process will generate: +# - Tag in Github repo: https://github.com/opentensor/validators/tags +# - Release in Github: https://github.com/opentensor/validators/releases +# - New entry in CHANGELOG.md file +# + +### +# Utils +### + +source ${BASH_SOURCE%/*}/utils.sh + +function help(){ + echo Usage: + echo \ \ $0 + echo + echo This script release a openvalidators version. + echo + echo This script needs: + echo \ \ - An existing __version__ var in the __init__.py file + echo \ \ - Version in __version__ var is not a git tag already + echo +} +### + +### +# Start of release process +### + +# 0. Check requirements +# Expected state for the execution environment +# - __version__ exists inside file 'openvalidators/__init__.py' +# - Version has the expected format + +CODE_WITH_VERSION='openvalidators/__init__.py' + +CODE_VERSION=`grep '__version__\ \=\ ' $CODE_WITH_VERSION | awk '{print $3}' | sed 's/"//g'` +VERSION=$CODE_VERSION + +if ! [[ "$CODE_VERSION" =~ ^[0-9]+.[0-9]+.[0-9]+$ ]];then + echo_error "Requirement failure: Version in code '$CODE_VERSION' with wrong format" + exit 1 +fi + +# 1. Get options + +## Defaults +APPLY="false" +APPLY_ACTION="" + +while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + help + exit 0 + ;; + -A|--apply) + APPLY="true" + APPLY_ACTION="--apply" + shift # past argument + ;; + -T|--github-token) + GITHUB_TOKEN="$2" + shift # past argument + shift # past value + ;; + -*|--*) + echo "Unknown option $1" + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; + esac +done + +if [[ $APPLY == "true" ]]; then + echo_warning "Not a Dry run exection" +else + echo_warning "Dry run execution" +fi + +if [[ -z $GITHUB_TOKEN && $APPLY == "true" ]]; then + echo_error "Github token required (-T, --github-token)" + exit 1 +fi + +# 2. Checking version + +CURRENT_VERSION_EXISTS=$(git tag | grep $VERSION) +if [[ ! -z $CURRENT_VERSION_EXISTS ]]; then + echo_error "Current version '$VERSION' already exists" + help + exit 1 +fi + +PREV_VERSION_TAG=`get_git_tag_higher_version` + +TAG_NAME=v$VERSION + +## 2.1. Current VERSION is not already a tag + +echo_info "Detected new version tag: $VERSION" +echo_info "Previous version tag: $PREV_VERSION_TAG" +echo_info "Tag generated: $TAG_NAME" + +# 3. Create Github resources +if [[ $APPLY == "true" ]]; then + ${BASH_SOURCE%/*}/github_release.sh $APPLY_ACTION --github-token $GITHUB_TOKEN -P $PREV_VERSION_TAG -V $VERSION +else + ${BASH_SOURCE%/*}/github_release.sh $APPLY_ACTION $GITHUB_TOKEN -P $PREV_VERSION_TAG -V $VERSION +fi diff --git a/scripts/release/utils.sh b/scripts/release/utils.sh new file mode 100755 index 0000000..91ba2f4 --- /dev/null +++ b/scripts/release/utils.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NC='\033[0m' # No Color + +function echo_error { + echo -e "${RED}[ERROR]${NC} $1" +} + +function echo_warning { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +function echo_info { + echo -e "${GREEN}[INFO]${NC} $1" +} + +function echo_json { + echo "{\"type\":\"$1\",\"message\":\"$2\"}" +} + +function get_git_tag_higher_version { + echo `git tag -l --sort -version:refname | head -n 1` +} \ No newline at end of file diff --git a/scripts/release/versioning.sh b/scripts/release/versioning.sh new file mode 100755 index 0000000..0c3ed9a --- /dev/null +++ b/scripts/release/versioning.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +#### +# Utils +#### +source ${BASH_SOURCE%/*}/utils.sh +### + +# 1. Get options + +## Defaults +APPLY="false" + +while [[ $# -gt 0 ]]; do + case $1 in + -A|--apply) + APPLY="true" + shift # past argument + ;; + -U|--update) + VERSION_TYPE="$2" + shift # past argument + shift # past value + ;; + -*|--*) + echo "Unknown option $1" + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; + esac +done + +if [[ $VERSION_TYPE != "major" && $VERSION_TYPE != "minor" && $VERSION_TYPE != "patch" && $VERSION_TYPE != "rc" ]]; then + echo_error "Incorrect version type (-U|--update). Version types accepted: {major, minor, patch}" + exit 1 +fi + +VERSION=$(cat VERSION) +CODE_WITH_VERSION='bittensor/__init__.py' + +MAJOR=$(awk -F. '{print $1}' <<< $VERSION) +MINOR=$(awk -F. '{print $2}' <<< $VERSION) +PATCH=$(awk -F. '{print $3}' <<< $VERSION) + +# RC version +RC=$(awk -F- '{print $NF}' <<< $version) +if [ -z $RC ]; then + CURRENT_VERSION="$MAJOR.$MINOR.$PATCH" +else + CURRENT_VERSION="$MAJOR.$MINOR.$PATCH-$RC" +fi + +case $VERSION_TYPE in + "major") + echo_info "Applying a $VERSION_TYPE update" + NEW_VERSION="$((MAJOR + 1)).0.0" + ;; + "minor") + echo_info "Applying a $VERSION_TYPE update" + NEW_VERSION="$MAJOR.$((MINOR + 1)).0" + ;; + "patch") + echo_info "Applying a $VERSION_TYPE update" + NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" + ;; + "rc") + SUFFIX=$2 + if [ -z $SUFFIX ]; then + echo_error "Suffix is needed when updating version to a RC" + exit 1 + fi + NEW_VERSION="$MAJOR.$MINOR.$PATCH-$SUFFIX" + ;; + *) + echo_error "This operation is not allowed. Try one of the following: {major, minor, patch, rc}" + exit 1 + ;; +esac + + +echo_info "Current version: $CURRENT_VERSION" +echo_info "New version: $NEW_VERSION" + +if [[ $APPLY == "true" ]]; then + echo_info "Updating version in code: sed -i "18,30s/$VERSION/$NEW_VERSION/g" $CODE_WITH_VERSION" + sed -i "18,30s/$VERSION/$NEW_VERSION/g" $CODE_WITH_VERSION + echo_info "Updating version in file: echo -n $NEW_VERSION > VERSION" + echo -n $NEW_VERSION > VERSION +else + echo_warning "Dry run execution. Version update not applied" + echo_info "Use -A or --apply to apply changes" +fi \ No newline at end of file From a537a4d4e9194201163104445cd919b2a025e894 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 12 Aug 2023 11:15:01 -0400 Subject: [PATCH 2/4] typo --- scripts/release/release.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/release/release.sh b/scripts/release/release.sh index 1140be1..f631fd6 100755 --- a/scripts/release/release.sh +++ b/scripts/release/release.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# In this script you are going to find the process of releasing bittensor. +# In this script you are going to find the process of releasing Openvalidators. # # This script needs: # - An existing __version__ var in the __init__.py file From 4a10eb82ff4cbeef2ee430ebd97d457bc10b7d15 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 12 Aug 2023 11:26:52 -0400 Subject: [PATCH 3/4] fix versioning script --- scripts/release/versioning.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/release/versioning.sh b/scripts/release/versioning.sh index 0c3ed9a..e9affb4 100755 --- a/scripts/release/versioning.sh +++ b/scripts/release/versioning.sh @@ -39,7 +39,7 @@ if [[ $VERSION_TYPE != "major" && $VERSION_TYPE != "minor" && $VERSION_TYPE != " fi VERSION=$(cat VERSION) -CODE_WITH_VERSION='bittensor/__init__.py' +CODE_WITH_VERSION='openvalidators/__init__.py' MAJOR=$(awk -F. '{print $1}' <<< $VERSION) MINOR=$(awk -F. '{print $2}' <<< $VERSION) @@ -87,8 +87,6 @@ echo_info "New version: $NEW_VERSION" if [[ $APPLY == "true" ]]; then echo_info "Updating version in code: sed -i "18,30s/$VERSION/$NEW_VERSION/g" $CODE_WITH_VERSION" sed -i "18,30s/$VERSION/$NEW_VERSION/g" $CODE_WITH_VERSION - echo_info "Updating version in file: echo -n $NEW_VERSION > VERSION" - echo -n $NEW_VERSION > VERSION else echo_warning "Dry run execution. Version update not applied" echo_info "Use -A or --apply to apply changes" From a870e7ab09636bbff6190fb2a36c57fc97956f26 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 12 Aug 2023 11:27:53 -0400 Subject: [PATCH 4/4] add release scripts readme --- scripts/release/README.md | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 scripts/release/README.md diff --git a/scripts/release/README.md b/scripts/release/README.md new file mode 100644 index 0000000..162e22c --- /dev/null +++ b/scripts/release/README.md @@ -0,0 +1,59 @@ +# Release Script(s) Usage + +## Versioning + +This script needs: +- An existing `openvalidators/__init__.py` file +- An existing `__version__` variable in that file +- An existing version for that variable + +This process will generate: +- A modified version in `__version__` for the update type specified + +### Example Usage +`./scripts/release/versioning.sh -U patch -A` + +Where: +* `-U` (major|minor|patch) the type of update +* `-A` is to apply the script changes + + +## Add Notes Changelog + +This script needs: +- An existing `CHANGELOG.md` file with at least three lines +- An existing git tag for the previous version + +This process will generate: +- A new entry in `CHANGELOG.md` + +##### *Note: This will only list merge commits into the release branch since the last tag* + +### Example Usage +`./scripts/release/add_notes_changelog.sh -P 1.1.7 -V 1.1.8 -B hotfix/serve-val-axon -T $GIT -A` + +Where: +* `-P` is the old version +* `-V` is the new version +* `-B` is the release branch name (default: `release/vX.X.X`) +* `-T` is the GIT API token +* `-A` is to apply the script changes + +## Release + +This script needs: +- An existing `__version__` variable in the `openvalidators/__init__.py` file +- Version in the `__version__` variable is not a git tag already + +This process will generate: +- Tag in Github repo: https://github.com/opentensor/validators/tags +- Release in Github: https://github.com/opentensor/validators/releases + + +### Example Usage +`./scripts/release/release.sh -T $GIT -A` + +Where: +* `-T` is the GIT API token +* `-A` is to apply the script changes +