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