diff --git a/build_system.d b/build_system.d index bd2e513..9a217eb 100755 --- a/build_system.d +++ b/build_system.d @@ -121,12 +121,10 @@ void createRelease(string ver) { "name": "Rail Signal v" ~ ver, "body": "An automated release." ]; - data.object["prerelease"] = JSONValue(true); + data.object["prerelease"] = JSONValue(false); data.object["generate_release_notes"] = JSONValue(false); - print("Sending release API request:\n%s", data.toPrettyString); auto rq = Request(); - rq.verbosity = 2; auto props = Properties("github_token.properties"); string username = props["username"]; string token = props["token"]; @@ -139,17 +137,15 @@ void createRelease(string ver) { if (response.code == 201) { string responseBody = cast(string) response.responseBody; JSONValue responseData = parseJSON(responseBody); - string assetUrl = responseData["assets_url"].str; - print("Got asset url: %s", assetUrl); - auto f = File("./target/rail-signal-" ~ ver ~ ".jar", "rb"); - auto assetResponse = rq.post( - assetUrl, - f.byChunk(4096), - "application/zip" + print("Created release %s", responseData["url"].str); + string command = format!"./upload-asset.sh github_api_token=%s owner=andrewlalis repo=RailSignalAPI tag=v%s filename=%s"( + token, + ver, + "./target/rail-signal-" ~ ver ~ ".jar" ); - writeln(assetResponse); + runOrQuit(command); } else { - error("An error occurred."); + error("An error occurred while creating the release."); writeln(response.responseBody); } } diff --git a/upload-asset.sh b/upload-asset.sh new file mode 100755 index 0000000..206e3cd --- /dev/null +++ b/upload-asset.sh @@ -0,0 +1,64 @@ +#!/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