Skip to content

Instantly share code, notes, and snippets.

@camalot
Created January 8, 2018 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camalot/0ac707f6b6576eabb4feb4501e87ca6b to your computer and use it in GitHub Desktop.
Save camalot/0ac707f6b6576eabb4feb4501e87ca6b to your computer and use it in GitHub Desktop.
github release : list, create, and upload
#!/usr/bin/env bash
set -e;
[ "$TRACE" ] && set -x;
GH_API="https://api.github.com";
GH_UPLOADS="https://uploads.github.com";
__print_help_arg() {
SHORT="$1";
LONG="$2";
DESC="${@:3}";
(>&2 echo -e "-$SHORT|--$LONG\t$DESC");
}
__help() {
(>&2 echo -e "$@");
}
__print_help() {
l="$0 list --token=GITHUB_TOKEN --owner=REPO_OWNER --repo=REPO_NAME";
c="$0 create --token=GITHUB_TOKEN --owner=REPO_OWNER --repo=REPO_NAME --tag=TAG_NAME [--draft] [--pre]";
u="$0 upload --token=GITHUB_TOKEN --owner=REPO_OWNER --repo=REPO_NAME --id=RELEASE_ID --filename=/path/to/file";
case $1 in
list)
__help "$l";
__help "";
__print_help_arg "a" "token" "The github auth token.";
__print_help_arg "o" "owner" "The owner or org that owns the repository.";
__print_help_arg "r" "repo" "The name of the repository.";
;;
create)
__help "$c";
__help "";
__print_help_arg "a" "token" "The github auth token.";
__print_help_arg "o" "owner" "The owner or org that owns the repository.";
__print_help_arg "r" "repo" "The name of the repository.";
__print_help_arg "t" "tag" "The commit tag name. Will be used to associate to, and the name of the release.";
__print_help_arg "d" "draft" "Flag to indicate if the release is a draft.";
__print_help_arg "p" "pre" "Flag to indicate if the release is a pre-release.";
;;
upload)
__help "$u";
__help "";
__print_help_arg "a" "token" "The github auth token.";
__print_help_arg "o" "owner" "The owner or org that owns the repository.";
__print_help_arg "r" "repo" "The name of the repository.";
__print_help_arg "i" "id\t" "The id of the release to upload the file to.";
__print_help_arg "f" "filename" "The full path of a file to upload to the release";
;;
*)
__help "Usage: $0 <command> <arguments>";
__help "";
__help "list";
__help "\t$l";
__help "\tFor full help: $0 list --help";
__help "";
__help "create";
__help "\t$c";
__help "\tFor full help: $0 create --help";
__help "";
__help "upload";
__help "\t$u";
__help "\tFor full help: $0 upload --help";
;;
esac
exit 1;
}
__error() {
RED='\033[0;31m';
NC='\033[0m';
dt=$(date "+%F %T");
caller=$(basename $0);
__help "${RED}[${dt}]\tERROR\t${caller}\t${1:-"Unknown Error"}${NC}";
exit 1;
}
__warning() {
YELLOW='\033[0;33m';
NC='\033[0m';
dt=$(date "+%F %T");
caller=$(basename $0);
__help "${RED}[${dt}]\tERROR\t${caller}\t${1:-"Unknown Error"}${NC}";
exit 1;
}
__command() {
case $1 in
list)
__list "${@:2}";
;;
create)
__create "${@:2}";
;;
upload)
__upload "${@:2}";
;;
*)
__print_help;
;;
esac
}
__list() {
for i in "$@"; do
case $i in
-a=*|--token=*)
opt_token="${i#*=}";
shift; # past argument=value
;;
-o=*|--owner=*)
opt_owner="${i#*=}";
shift; # past argument=value
;;
-r=*|--repo=*)
opt_repo="${i#*=}";
shift; # past argument=value
;;
*)
__print_help "list";
;;
esac
done
[[ -z "${opt_token// }" ]] && __error "--token parameter is required, but was not defined with a value.";
[[ -z "${opt_owner// }" ]] && __error "--owner parameter is required, but was not defined with a value.";
[[ -z "${opt_repo// }" ]] && __error "--repo parameter is required, but was not defined with a value.";
GH_REPO="$GH_API/repos/$opt_owner/$opt_repo";
AUTH="Authorization: token $opt_token";
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { __error "Error: Invalid repo, token or network issue!"; }
data=$(curl -sH "$AUTH" "${GH_REPO}/releases" | jq -r '.[] | "\(.id)\t\t\(.name)\t\t\(.tag_name)\t\t\(.target_commitish)" ');
__help "ID\t\tNAME\t\tTAG\t\tTARGET";
printf '%s' "$data";
}
__create() {
for i in "$@"; do
case $i in
-a=*|--token=*)
opt_token="${i#*=}";
shift; # past argument=value
;;
-o=*|--owner=*)
opt_owner="${i#*=}";
shift; # past argument=value
;;
-r=*|--repo=*)
opt_repo="${i#*=}";
shift; # past argument=value
;;
-t=*|--tag=*)
opt_tag="${i#*=}";
shift; # past argument with no value
;;
# flag as draft
-d|--draft)
opt_draft=1;
shift;
;;
-p|--pre)
opt_prerelease=1;
shift;
;;
*)
__print_help "create";
;;
esac
done
[[ -z "${opt_token// }" ]] && __error "--token parameter is required, but was not defined with a value.";
[[ -z "${opt_owner// }" ]] && __error "--owner parameter is required, but was not defined with a value.";
[[ -z "${opt_repo// }" ]] && __error "--repo parameter is required, but was not defined with a value.";
[[ -z "${opt_tag// }" ]] && __error "--tag parameter is required, but was not defined with a value.";
# Define variables.
GH_API="https://api.github.com";
GH_REPO="$GH_API/repos/$opt_owner/$opt_repo";
GH_TAGS="$GH_REPO/releases/tags/$opt_tag";
GH_CREATE="$GH_REPO/releases";
AUTH="Authorization: token $opt_token";
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { __error "Error: Invalid repo, token or network issue!"; }
is_draft="false";
[[ $opt_draft -eq 1 ]] && is_draft="true";
is_prerelease="false";
# create release
id=$(curl -X POST -sH "$AUTH" -H "Content-Type: application/json" -d \
"{ \"tag_name\": \"$opt_tag\", \"name\": \"$opt_tag\", \"draft\": $is_draft, \"prerelease\": false }" \
$GH_CREATE | jq -r ' .id ');
[[ -z "${id}" ]] && __error "$id";
echo "${id}";
}
__upload() {
for i in "$@"; do
case $i in
-a=*|--token=*)
opt_token="${i#*=}";
shift; # past argument=value
;;
-o=*|--owner=*)
opt_owner="${i#*=}";
shift; # past argument=value
;;
-r=*|--repo=*)
opt_repo="${i#*=}";
shift; # past argument=value
;;
-i=*|--id=*)
opt_id="${i#*=}";
shift;
;;
-f=*|--filename=*)
opt_filename="${i#*=}";
shift;
;;
*)
__print_help "upload";
esac
done
__help "opt_filename: $opt_filename";
__help "opt_owner: $opt_owner";
[[ -z "${opt_token// }" ]] && __error "--token parameter is required, but was not defined with a value.";
[[ -z "${opt_owner// }" ]] && __error "--owner parameter is required, but was not defined with a value.";
[[ -z "${opt_repo// }" ]] && __error "--repo parameter is required, but was not defined with a value.";
[[ -z "${opt_id// }" ]] && __error "--id parameter is required, but was not defined with a value.";
[[ -z "${opt_filename// }" ]] && __error "--filename parameter is required, but was not defined with a value.";
[[ ! -f "${opt_filename}" ]] && __error "'${opt_filename}' does not exist.";
__help "Uploading asset... $opt_filename";
AUTH="Authorization: token $opt_token";
GH_ASSET="$GH_UPLOADS/repos/$opt_owner/$opt_repo/releases/$opt_id/assets?name=$(basename $opt_filename)";
upload="$(curl --data-binary @"$opt_filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET)";
echo $upload | jq -r ' .browser_download_url ';
}
__command "$@";
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment