From af0754a921255ea9103df39c158a4966040d9c3e Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 9 Mar 2020 15:25:55 +0800 Subject: update --- alias | 4 +- tools/github-release.sh | 109 +++++++++++++++++++++++++++++++++++ tools/upload-github-release-asset.sh | 109 ----------------------------------- 3 files changed, 111 insertions(+), 111 deletions(-) create mode 100755 tools/github-release.sh delete mode 100755 tools/upload-github-release-asset.sh diff --git a/alias b/alias index 0bf6dab..d5da487 100644 --- a/alias +++ b/alias @@ -96,7 +96,7 @@ gcg() { git clone git@github.com:$1/$2.git && cd $(basename $2) } github_release_asset() { - command="upload-github-release-asset.sh\n github_api_token=$GITHUB_API_TOKEN\n owner=typebrook\n repo=tig\n tag=LATEST\n type=asset\n filename=$(which tig)\n overwrite=false" + command="github-release.sh\n github_api_token=$GITHUB_API_TOKEN\n owner=typebrook\n repo=tig\n tag=LATEST\n type=asset\n filename=$(which tig)\n overwrite=false" prompt "$command" } @@ -107,7 +107,7 @@ alias ts='tig status' alias ta='tig --all' alias get-tig='curl -LO https://github.com/typebrook/tig/releases/download/tig-2.5.0/tig' upload_tig() { - upload-github-release-asset.sh \ + github-release.sh \ github_api_token=$GITHUB_API_TOKEN \ owner=typebrook \ repo=tig \ diff --git a/tools/github-release.sh b/tools/github-release.sh new file mode 100755 index 0000000..4f760c0 --- /dev/null +++ b/tools/github-release.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env sh +# +# Author: Hsieh Chin Fan (typebrook) +# License: MIT +# https://gist.github.com/typebrook/4947769e266173303d8848f496e272c9 +# +# Originally created by stefanbuck +# fork from: https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 +# +# +# This script accepts the following parameters: +# +# * owner +# * repo +# * tag +# * type (asset or edit) +# * filename +# * github_api_token +# * overwrite (optional, could be ture, false, delete, default to be false) +# +# Script to upload a release asset using the GitHub API v3. +# +# Example: +# +# github-release.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 type=asset filename=./build.zip overwrite=true +# + +# Check dependencies. +set -e + +# Validate settings. +[ "$TRACE" ] && set -x + +CONFIG=$@ + +for line in $CONFIG; do + eval "$line" +done + +# Define variables. +GH_API="https://api.github.com" +GH_REPO="$GH_API/repos/$owner/$repo" +GH_TAGS="$GH_REPO/releases/tags/$tag" +AUTH="Authorization: token $github_api_token" + +if [ "$tag" = 'LATEST' ]; then + GH_TAGS="$GH_REPO/releases/latest" +fi + +# Validate token. +curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } + +# Read asset tags. +response=$(curl -sH "$AUTH" $GH_TAGS) + +# Get ID of the release. +eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/') +[ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } + +upload_asset() { + # Get ID of the asset based on given filename. + # If exists, delete it. + eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') + if [ "$asset_id" = "" ]; then + echo "No need to overwrite asset" + else + if [ "$overwrite" = "true" ] || [ "$overwrite" = "delete" ]; then + echo "Deleting asset($asset_id)... " + curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id" + if [ "$overwrite" = "delete" ]; then + exit 0 + fi + else + echo "File already exists on tag $tag" + echo "If you want to overwrite it, set overwrite=true" + exit 1 + fi + fi + + # Upload asset + echo "Uploading asset... " + + # Construct url + GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)" + + curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET +} + +edit_release() { + GH_RELEASE="$GH_REPO/releases/$release_id" + body=$(cat < -# License: MIT -# https://gist.github.com/typebrook/4947769e266173303d8848f496e272c9 -# -# Originally created by stefanbuck -# fork from: https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 -# -# -# This script accepts the following parameters: -# -# * owner -# * repo -# * tag -# * type (asset or edit) -# * filename -# * github_api_token -# * overwrite (optional, could be ture, false, delete, default to be false) -# -# Script to upload a release asset using the GitHub API v3. -# -# Example: -# -# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip overwrite=true -# - -# Check dependencies. -set -e - -# Validate settings. -[ "$TRACE" ] && set -x - -CONFIG=$@ - -for line in $CONFIG; do - eval "$line" -done - -# Define variables. -GH_API="https://api.github.com" -GH_REPO="$GH_API/repos/$owner/$repo" -GH_TAGS="$GH_REPO/releases/tags/$tag" -AUTH="Authorization: token $github_api_token" - -if [ "$tag" = 'LATEST' ]; then - GH_TAGS="$GH_REPO/releases/latest" -fi - -# Validate token. -curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } - -# Read asset tags. -response=$(curl -sH "$AUTH" $GH_TAGS) - -# Get ID of the release. -eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/') -[ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } - -upload_asset() { - # Get ID of the asset based on given filename. - # If exists, delete it. - eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') - if [ "$asset_id" = "" ]; then - echo "No need to overwrite asset" - else - if [ "$overwrite" = "true" ] || [ "$overwrite" = "delete" ]; then - echo "Deleting asset($asset_id)... " - curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id" - if [ "$overwrite" = "delete" ]; then - exit 0 - fi - else - echo "File already exists on tag $tag" - echo "If you want to overwrite it, set overwrite=true" - exit 1 - fi - fi - - # Upload asset - echo "Uploading asset... " - - # Construct url - GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)" - - curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET -} - -edit_release() { - GH_RELEASE="$GH_REPO/releases/$release_id" - body=$(cat <