Skip to content

Instantly share code, notes, and snippets.

@Rory-Z
Forked from stefanbuck/upload-github-release-asset.sh
Last active August 26, 2019 01:47
Show Gist options
  • Save Rory-Z/beb69097fc8aaa70c6f10524d10237f3 to your computer and use it in GitHub Desktop.
Save Rory-Z/beb69097fc8aaa70c6f10524d10237f3 to your computer and use it in GitHub Desktop.
Script to GitHub API v3.
#!/usr/bin/env bash
#
#
# This script accepts the following parameters:
#
# * owner
# * repo
# * version
# * github_api_token
#
# Script to create pull requests using the GitHub API v3.
# https://developer.github.com/v3/pulls/#create-a-pull-request
#
# Example:
#
# ./create-github-milestone.sh github_api_token=TOKEN owner=owner repo=repo version=v4.0
#
set -e
xargs=$(which gxargs || which xargs)
# 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_MILESTONES="$GH_API/repos/$owner/$repo/milestones"
AUTH="Authorization: token $github_api_token"
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
# Create a pull request.
echo $(curl -sH "$AUTH" -X POST -d "{\"title\":\"${version#v}\",\"state\":\"open\"}" $GH_MILESTONES)
#!/usr/bin/env bash
#
#
# This script accepts the following parameters:
#
# * owner
# * repo
# * title
# * head
# * base
# * github_api_token
#
# Script to create pull requests using the GitHub API v3.
# https://developer.github.com/v3/pulls/#create-a-pull-request
#
# Example:
#
# ./create-github-pull-request.sh github_api_token=TOKEN owner=owner repo=repo title="auto PR" head="develop" base="master"
#
set -e
xargs=$(which gxargs || which xargs)
# 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_PR="$GH_API/repos/$owner/$repo/pulls"
AUTH="Authorization: token $github_api_token"
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
# Create a pull request.
echo $(curl -sH "$AUTH" -X POST -d "{\"title\":\"$title\",\"head\":\"$head\",\"base\":\"$base\"}" $GH_PR)
#!/usr/bin/env bash
#
# Author: Stefan Buck
# License: MIT
# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
#
#
# This script accepts the following parameters:
#
# * owner
# * repo
# * tag
# * filename
# * github_api_token
#
# 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
#
# Check dependencies.
set -e
xargs=$(which gxargs || which xargs)
# 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"
WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
CURL_ARGS="-LJO#"
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 asset based on given filename.
eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }
# Upload asset
echo "Uploading asset... "
# Construct url
GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment