From 00039af463f18f9c990b35047edbf5af02699a9d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 11 Apr 2014 16:45:52 -0500 Subject: [PATCH] Rewrite upload-to-github.sh for current GitHub API Signed-off-by: Johannes Schindelin --- bin/upload-to-github.sh | 192 ++++++++-------------------------------- 1 file changed, 35 insertions(+), 157 deletions(-) diff --git a/bin/upload-to-github.sh b/bin/upload-to-github.sh index a78d48ae..60fc1412 100755 --- a/bin/upload-to-github.sh +++ b/bin/upload-to-github.sh @@ -1,167 +1,45 @@ #!/bin/sh -# Written with a lot of assistance by Scott Chacon +die () { + echo "$*" >&2 + exit 1 +} -USAGE="$0"' ... -Options: - --description (required) - --user (required) - --repository (required) -' +test $# -ge 2 || +die "Usage: $0 ..." -description= -user= -repository= -filepath= -filesize= -basename= -dryrun= -while test $# -gt 0 +tagname="$1" +shift + +url=https://api.github.com/repos/msysgit/msysgit/releases +id="$(curl --netrc -s $url | + grep -B1 "\"tag_name\": \"$tagname\"" | + sed -n 's/.*"id": *\([0-9]*\).*/\1/p')" +test -n "$id" || { + out="$(curl --netrc -s -XPOST -d '{"tag_name":"'"$tagname"'"}' $url)" || + die "Error creating release: $out" + id="$(echo "$out" | + sed -n 's/.*"id": *\([0-9]*\).*/\1/p')" + test -n "$id" || + die "Could not create release for tag $tagname" +} + +url=https://uploads.${url#https://api.} + +for path do - case "$1" in - --description|--user|--repository) - test $# -gt 1 || { - echo "Option '$1' needs a value" >&2 - exit 1 - } - eval ${1#--}="\"$2\"" - shift + case "$path" in + *.exe) + contenttype=application/executable ;; - --description=*|--user=*|--repository=*) - pair="${1#--}" - eval ${pair%%=*}="${pair#*=}" - ;; - --dry-run) - dryrun=YouGotThatRight - ;; - --help) - echo "$USAGE" - exit 1 + *.7z) + contenttype=application/zip ;; *) - break; + die "Unknown file type: $path" + ;; esac - shift + basename="$(basename "$path")" + curl -i --netrc -XPOST -H "Content-Type: $contenttype" \ + --data-binary @"$path" "$url/$id/assets?name=$basename" done - -if test $# -ne 1 || ! test -f "$1" -then - echo "$USAGE" - exit 1 -fi - -filepath="$1" -filesize="$(stat -c %s "$filepath")" -basename="$(basename "$filepath")" - -# extract 'for version ' from basename -for_version=${basename%-preview*} -for_version=${for_version##*Git-} -for_version=${for_version##*install-} -for_version="for official Git for Windows $for_version" - -case "$basename" in -Git-*) - description="${description:-Full installer $for_version}" - repository=${repository:-msysgit/git} - ;; -PortableGit-*) - description="${description:-Portable application $for_version}" - repository=${repository:-msysgit/git} - ;; -msysGit-netinstall-*) - description="${description:-Net installer if you want to hack on Git}" - repository=${repository:-msysgit/git} - ;; -msysGit-fullinstall-*) - description="${description:-Full installer (self-contained) if you want to hack on Git}" - repository=${repository:-msysgit/git} - ;; -esac - -test -z "$user" && -user="$(grep -A2 -i '^machine *api.github.com' < "$HOME/.netrc" 2> /dev/null | - sed -n 's|login *||pi')" - -if test -z "$description" || test -z "$user" || test -z "$repository" -then - echo "$USAGE" - exit 1 -fi - -get_password () { # args: user host - # try $HOME/.netrc; ignore parameter first - password="$(grep -A2 -i "^machine *$2" < $HOME/.netrc 2> /dev/null | - sed -n 's|^password *||p')" - test -z "$password" && - password="$(git gui--askpass "Password for $1@$2")" - echo "$password" -} - -json_get () { # args: key json - echo "$2" | - sed -n -s "s|^ *\"$1\" *: *\"\(.*\)\",\?$|\1|p" -} - -json_wrap () { # args: key1, value1, [key2, value2], ... - printf '{' - sep= - while test $# -gt 1 - do - printf '%s"%s":"%s"' "$sep" "$1" "$2" - sep=, - shift - shift - done - printf '}' -} - -windowsfilepath="$(cd "$(dirname "$filepath")" && pwd -W)\\$basename" - -test -n "$dryrun" && { - cat << EOF -basename: $basename -size: $filesize -description: $description -user: $user -repository: $repository -EOF - exit 0 -} - -password="$(get_password "$user" "api.github.com")" -test -n "$password" || exit - -# get ticket -json="$(curl -XPOST \ - -d "$(json_wrap \ - name "$basename" \ - size "$filesize" \ - description "$description")" \ - -u "$user:$password" \ - https://api.github.com/repos/$repository/downloads)" - -# upload for real, using S3 -result="$(curl \ - -F key="$(json_get path "$json")" \ - -F acl="$(json_get acl "$json")" \ - -F success_action_status=201 \ - -F Filename="$(json_get name "$json")" \ - -F AWSAccessKeyId="$(json_get accesskeyid "$json")" \ - -F Policy="$(json_get policy "$json")" \ - -F Signature="$(json_get signature "$json")" \ - -F Content-Type="$(json_get mime_type "$json")" \ - -F file=@"$windowsfilepath" \ - "$(json_get s3_url "$json")")" - -echo "$result" - -# Verify that the upload was successful -case "$result" in -*""*""*) - echo "Success!" - ;; -*) - exit 1 - ;; -esac