|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit |
| 4 | +set -o nounset |
| 5 | + |
| 6 | +declare -r debug='false' |
| 7 | +declare -r tmpfile_file="/tmp/publish.$$.tmpfiles" |
| 8 | + |
| 9 | +function make_temp_file |
| 10 | +{ |
| 11 | + local template="${1:-publish.$$.XXXXXX}" |
| 12 | + if [[ $template != *XXXXXX ]] |
| 13 | + then |
| 14 | + template="$template.XXXXXX" |
| 15 | + fi |
| 16 | + local tmp=$(mktemp -t "$template") |
| 17 | + echo "$tmp" >> "$tmpfile_file" |
| 18 | + echo "$tmp" |
| 19 | +} |
| 20 | + |
| 21 | +function now |
| 22 | +{ |
| 23 | + date '+%Y-%m-%d %H:%M:%S' |
| 24 | +} |
| 25 | + |
| 26 | +function pwarn |
| 27 | +{ |
| 28 | + echo "$(now) [warning]: $@" 1>&2 |
| 29 | +} |
| 30 | + |
| 31 | +function perr |
| 32 | +{ |
| 33 | + echo "$(now) [error]: $@" 1>&2 |
| 34 | +} |
| 35 | + |
| 36 | +function pinfo |
| 37 | +{ |
| 38 | + echo "$(now) [info]: $@" |
| 39 | +} |
| 40 | + |
| 41 | +function pdebug |
| 42 | +{ |
| 43 | + if [[ $debug == 'true' ]] |
| 44 | + then |
| 45 | + echo "$(now) [debug]: $@" |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +function errexit |
| 50 | +{ |
| 51 | + perr "$@" |
| 52 | + exit 1 |
| 53 | +} |
| 54 | + |
| 55 | +function onexit |
| 56 | +{ |
| 57 | + if [[ -f $tmpfile_file ]] |
| 58 | + then |
| 59 | + for tmpfile in $(< $tmpfile_file) |
| 60 | + do |
| 61 | + pdebug "removing temp file $tmpfile" |
| 62 | + rm -f $tmpfile |
| 63 | + done |
| 64 | + rm -f $tmpfile_file |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +function gh_publish { |
| 69 | + if [[ -z $version_string ]] |
| 70 | + then |
| 71 | + errexit 'gh_publish: version_string required' |
| 72 | + fi |
| 73 | + |
| 74 | + # NB: no 'v' here at start of version_string |
| 75 | + local -r package_name="riak-$version_string.tar.gz" |
| 76 | + local -r package="./dist/riak-$version_string.tar.gz" |
| 77 | + if [[ ! -s $package ]] |
| 78 | + then |
| 79 | + errexit "gh_publish: expected to find $package in dist/" |
| 80 | + fi |
| 81 | + |
| 82 | + # NB: we use a X.Y.Z tag |
| 83 | + local -r release_json="{ |
| 84 | + \"tag_name\" : \"$version_string\", |
| 85 | + \"name\" : \"Riak Python Client $version_string\", |
| 86 | + \"body\" : \"riak-python-client $version_string\nhttps://github.com/basho/riak-python-client/blob/master/RELNOTES.md\", |
| 87 | + \"draft\" : false, |
| 88 | + \"prerelease\" : $is_prerelease |
| 89 | + }" |
| 90 | + |
| 91 | + pdebug "Release JSON: $release_json" |
| 92 | + |
| 93 | + local curl_content_file="$(make_temp_file)" |
| 94 | + local curl_stdout_file="$(make_temp_file)" |
| 95 | + local curl_stderr_file="$(make_temp_file)" |
| 96 | + |
| 97 | + curl -4so $curl_content_file -w '%{http_code}' -XPOST \ |
| 98 | + -H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \ |
| 99 | + 'https://api.github.com/repos/basho/riak-python-client/releases' -d "$release_json" 1> "$curl_stdout_file" 2> "$curl_stderr_file" |
| 100 | + if [[ $? != 0 ]] |
| 101 | + then |
| 102 | + errexit "curl error exited with code: '$?' see '$curl_stderr_file'" |
| 103 | + fi |
| 104 | + |
| 105 | + local -i curl_rslt="$(< $curl_stdout_file)" |
| 106 | + if (( curl_rslt == 422 )) |
| 107 | + then |
| 108 | + pwarn "Release in GitHub already exists! (http code: '$curl_rslt')" |
| 109 | + curl -4so $curl_content_file -w '%{http_code}' -XGET \ |
| 110 | + -H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \ |
| 111 | + "https://api.github.com/repos/basho/riak-python-client/releases/tags/$version_string" 1> "$curl_stdout_file" 2> "$curl_stderr_file" |
| 112 | + if [[ $? != 0 ]] |
| 113 | + then |
| 114 | + errexit "curl error exited with code: '$?' see '$curl_stderr_file'" |
| 115 | + fi |
| 116 | + elif (( curl_rslt != 201 )) |
| 117 | + then |
| 118 | + errexit "Creating release in GitHub failed with http code '$curl_rslt'" |
| 119 | + fi |
| 120 | + |
| 121 | + if [[ ! -s $curl_content_file ]] |
| 122 | + then |
| 123 | + errexit 'no release info to parse for asset uploads' |
| 124 | + fi |
| 125 | + |
| 126 | + # "upload_url": "https://uploads.github.com/repos/basho/riak-python-client/releases/1115734/assets{?name,label}" |
| 127 | + # https://uploads.github.com/repos/basho/riak-python-client/releases/1115734/assets{?name,label} |
| 128 | + local -r upload_url_with_name=$(perl -ne 'print qq($1\n) and exit if /"upload_url"[ :]+"(https:\/\/[^"]+)"/' "$curl_content_file") |
| 129 | + local -r upload_url="${upload_url_with_name/\{?name,label\}/?name=$package_name}" |
| 130 | + |
| 131 | + local curl_content_file="$(make_temp_file)" |
| 132 | + local curl_stdout_file="$(make_temp_file)" |
| 133 | + local curl_stderr_file="$(make_temp_file)" |
| 134 | + |
| 135 | + curl -4so $curl_content_file -w '%{http_code}' -XPOST \ |
| 136 | + -H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/x-compressed, application/x-tar' \ |
| 137 | + "$upload_url" --data-binary "@$package" 1> "$curl_stdout_file" 2> "$curl_stderr_file" |
| 138 | + if [[ $? != 0 ]] |
| 139 | + then |
| 140 | + errexit "curl error exited with code: '$?' see '$curl_stderr_file'" |
| 141 | + fi |
| 142 | + |
| 143 | + curl_rslt="$(< $curl_stdout_file)" |
| 144 | + if (( curl_rslt != 201 )) |
| 145 | + then |
| 146 | + errexit "Uploading release assets to GitHub failed with http code '$curl_rslt'" |
| 147 | + fi |
| 148 | +} |
| 149 | + |
| 150 | +trap onexit EXIT |
| 151 | + |
| 152 | +declare -r version_string="${1:-unknown}" |
| 153 | + |
| 154 | +if [[ ! $version_string =~ ^[0-9].[0-9].[0-9](-[a-z]+[0-9]+)?$ ]] |
| 155 | +then |
| 156 | + errexit 'first argument must be valid version string in X.Y.Z format' |
| 157 | +fi |
| 158 | + |
| 159 | +is_prerelease='false' |
| 160 | +if [[ $version_string =~ ^[0-9].[0-9].[0-9]-[a-z]+[0-9]+$ ]] |
| 161 | +then |
| 162 | + pinfo "publishing pre-release version: $version_string" |
| 163 | + is_prerelease='true' |
| 164 | +else |
| 165 | + pinfo "publishing version $version_string" |
| 166 | +fi |
| 167 | + |
| 168 | +declare -r current_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 169 | + |
| 170 | +if [[ $debug == 'false' && $is_prerelease == 'false' && $current_branch != 'master' ]] |
| 171 | +then |
| 172 | + errexit 'publish must be run on master branch' |
| 173 | +fi |
| 174 | + |
| 175 | +declare -r github_api_key_file="$HOME/.ghapi" |
| 176 | +if [[ ! -s $github_api_key_file ]] |
| 177 | +then |
| 178 | + errexit "please save your GitHub API token in $github_api_key_file" |
| 179 | +fi |
| 180 | + |
| 181 | +# Validate commands |
| 182 | +if ! hash curl 2>/dev/null |
| 183 | +then |
| 184 | + errexit "'curl' must be in your PATH" |
| 185 | +fi |
| 186 | + |
| 187 | +validate=${2:-''} |
| 188 | +if [[ $validate == 'validate' ]] |
| 189 | +then |
| 190 | + exit 0 |
| 191 | +fi |
| 192 | + |
| 193 | +gh_publish |
0 commit comments