-
Notifications
You must be signed in to change notification settings - Fork 9
/
github_release_api.sh
executable file
·488 lines (440 loc) · 13.8 KB
/
github_release_api.sh
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
## /usr/bin/env bash
#*****************************************************************************************
# Github management script.
#
# Script relying on Github API v3, as described in:
# https://developer.github.com/v3/repos/releases/
#
# This script contains an API and is intended to be used as an include within other
# scripts. Methods of interest are:
# getGithubReleaseId()
# getGithubReleaseDescription()
# uploadAsset()
# deleteAsset()
# listReleaseSummary()
# listAssetSummary()
# deleteRelease()
# For more information, see methods description below, as well as their use within the
# main section of the script.
#
# Author: Patrick Durand, Inria
# Created: December 2015
#*****************************************************************************************
# ========================================================================================
# Section: prepare access to JSON script
s_dir=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )
JSON_SH=$s_dir/json-v2.sh
# ========================================================================================
# Section: variable declarations
#for internal use, to store github api answers
github_answer="_ga_.json"
#some global variables to store credentials and github repos
LOGIN=""
TOKEN=""
OWNER=""
REPOSITORY=""
#command to execute
COMMAND=""
#git tag name
TAG=""
#list of files from command-line ([file ...] part if any)
FILES=""
#default message used to create a new release
CREATE_MESSAGE="new draft release"
#by default this script talk to you
SILENT="off"
# ========================================================================================
# Section: utility function declarations
# --------
# FUNCTION: print out an simple message on stderr (only if SILENT mode is off)
function errorMsg(){
if [ "$SILENT" == "off" ]; then
printf "$* \n" >&2
fi
}
# --------
# FUNCTION: print out an simple message on stdout (only if SILENT mode is off)
function infoMsg(){
if [ "$SILENT" == "off" ]; then
printf "$* \n"
fi
}
# --------
# FUNCTION: print out an error message on stderr and exit from application
function throw () {
errorMsg "$* \n"
exit 77
}
# --------
# FUNCTION: check whether or not github returns an error
# arg1: file containing json answer
# return: error msg to display
function isError(){
local github_error_msg=$(cat "$1" | $JSON_SH -b | grep -F -e "[message]" | cut -s -f 2 | sed -e "s/\"//g" | tr [:upper:] [:lower:])
if [ "$github_error_msg" == "not found" ]; then
echo "true"
else
echo "false"
fi
}
# --------
# FUNCTION: clean any resources created
function clean(){
if [ -e $github_answer ]; then
rm -f $github_answer
fi
}
# --------
# FUNCTION: return a key associated to a particular key from a json file
# arg1: file containing json answer from github
# arg2: key. Can be a simple string or structured keys should have the form A.B.C when
# a json file has a structured contents.
# return: a string containing the value or an empty string if not found
function getDataField(){
echo $(cat "$1" | $JSON_SH -b | grep -F -e "[$2]" | cut -s -f 2 | sed -e "s/\"//g")
}
# ========================================================================================
# Section: github_API_v3 based function declarations
# --------
# FUNCTION: return a github release id given a git release tag
# arg1: git release tag
# return: a string containing release id
# throw: exit application if release does not exist
function getGithubReleaseId(){
# Connect github to check whether or not release already exists
curl --user ${LOGIN}:${TOKEN} \
--request GET \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases/tags/${1} <<END
END
#do we have an error ?
if [ $(isError "$github_answer") == "true" ]; then
throw "/!\ Cannot find release github ID for git tag ${1}"
fi
echo $(getDataField "$github_answer" "id")
}
# --------
# FUNCTION: return a github release description
# arg1: github release ID
# return: a json containing release description
# throw: exit application if release does not exist
function getGithubReleaseDescription(){
# Connect github to check whether or not release already exists
curl --user ${LOGIN}:${TOKEN} \
--request GET \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases/${1} <<END
END
#do we have an error ?
if [ $(isError "$github_answer") == "true" ]; then
throw "/!\ Cannot find github ID ${1}"
fi
echo $(cat "$github_answer")
}
# --------
# FUNCTION: return a list of assets that are contained in a specific release
# arg1: github release id
# return: a json structured contents
function getAssetsDescription(){
curl --user ${LOGIN}:${TOKEN} \
--request GET \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases/${1}/assets <<END
END
echo $(cat "$github_answer")
}
# --------
# FUNCTION: return a list of release that are contained in a specific repository
# args: none
# return: a json structured contents
function getReleasesDescription(){
curl --user ${LOGIN}:${TOKEN} \
--request GET \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases <<END
END
echo $(cat "$github_answer")
}
# --------
# FUNCTION: display the release available on github for a particular repository
# args: none
# return: nothing
function listReleaseSummary(){
local dRelName=""
local dRelDate=""
local dRelId=""
infoMsg "Releases available on github for ${OWNER}/${REPOSITORY}:"
local rels=$($(echo getReleasesDescription $1) | $JSON_SH -b | cut -s -d "." -f 1 | sed -e "s/\[//g" | uniq)
if [ ! -z "$rels" ]; then
for key in $rels;
do
dRelName=$(getDataField "$github_answer" "${key}.tag_name")
dRelId=$(getDataField "$github_answer" "${key}.id")
dRelDate=$(getDataField "$github_answer" "${key}.created_at")
infoMsg " asset $key: $dRelName ($dRelId), $dRelDate";
done
else
infoMsg " none."
fi
}
# --------
# FUNCTION: display the assets (id and name) contained in a release
# arg1: github release id
# return: nothing
function listAssetSummary(){
local dFileName=""
local dFileSize=""
local dFileDate=""
local dFileDownload=""
infoMsg "File(s) for release $1:"
local files=$($(echo getAssetsDescription $1) | $JSON_SH -b | cut -s -d "." -f 1 | sed -e "s/\[//g" | uniq)
if [ ! -z "$files" ]; then
for key in $files;
do
dFileName=$(getDataField "$github_answer" "${key}.name")
dFileSize=$(getDataField "$github_answer" "${key}.size")
dFileDate=$(getDataField "$github_answer" "${key}.created_at")
dFileDownload=$(getDataField "$github_answer" "${key}.download_count")
infoMsg " asset $key: $dFileName ($dFileSize bytes), $dFileDate; $dFileDownload downloads.";
done
else
infoMsg " none."
fi
}
# --------
# FUNCTION: delete an asset by its file name. Utility method, use deleteAsset() instead.
# arg1: github release id
# arg2: file name to delete
# return: 0 if success (file found and deleted), 1 otherwise
function deleteAssetbyName(){
local dFileName=""
local dFileId=""
for key in $($(echo getAssetsDescription $1) | $JSON_SH -b | cut -s -d "." -f 1 | sed -e "s/\[//g" | uniq);
do
dFileName=$(getDataField "$github_answer" "${key}.name")
if [ "$dFileName" == "$2" ]; then
dFileId=$(getDataField "$github_answer" "${key}.id")
curl --user ${LOGIN}:${TOKEN} \
--request DELETE \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases/assets/$dFileId <<END
END
return 0
fi
done
return 1
}
# --------
# FUNCTION: delete a file on remote github repository
# arg1: github release id
# arg2: file name to delete
# return: nothing
function deleteAsset(){
infoMsg "Deleting $2"
if $(deleteAssetbyName "$1" "$2");
then
infoMsg " deleted.";
else
errorMsg " not deleted (maybe not found)";
fi
}
# --------
# FUNCTION: upload a file to github
# arg1: github release id
# arg2: file to upload
# return: nothing
# throw: exit application if file cannot be found of if upload failed
function uploadAsset(){
local release_desc=""
local tmpFile=""
local dField=""
local dField2=""
local upURL=""
local upFile=""
local upBaseName=""
# Connect github to get upload_url from release description
release_desc=$(getGithubReleaseDescription $1)
tmpFile="zmtp.json"
echo $release_desc > $tmpFile
dField=$(getDataField "$tmpFile" "upload_url")
rm -f $tmpFile
# Connect github to upload a file (binary mode only)
upURL=$(echo $dField | cut -d "{" -f 1)
upURL="${upURL}?name="
upFile=$2
infoMsg "Uploading files on github for release $1"
infoMsg "Upload URL: ${upURL}"
infoMsg " uploading file: ${upFile}"
upBaseName=$(basename $upFile)
curl --user ${LOGIN}:${TOKEN} \
--request POST \
--silent \
--output "$github_answer" \
--header "Content-Type: application/octet-stream" \
--data-binary @${upFile} \
${upURL}${upBaseName} <<END
END
dField=$(getDataField "$github_answer" "state")
if [ ! "$dField" == "uploaded" ]; then
dField=$(getDataField "$github_answer" "message")
dField2=$(getDataField "$github_answer" "errors.0.code")
throw "/!\ Cannot upload file. ${dField}. ${dField2}."
fi
infoMsg " uploaded."
}
# --------
# FUNCTION: upload a file to github
# arg1: tag name
# arg2: create draft release? "true" or "false" (optional, defaults is "true")
# arg3: release description (optional)
# return: nothing
# throw: exit if tag already exist as a release on github side, or if release
# creation failed.
function createRelease(){
local release_desc="$CREATE_MESSAGE"
if [ ! -z "$3" ]; then
release_desc="$3"
fi
local draft="true"
if [ ! -z "$2" ]; then
draft="$2"
fi
local dField=""
# Connect github to create release
infoMsg "Connecting github to create release: $1"
if [ $# -eq 2 ]; then
release_desc=$2
fi
cat <<END
{
"tag_name": "$1",
"target_commitish": "master",
"name": "$1",
"body": "$release_desc",
"draft": $draft,
"prerelease": false
}
END
curl --user ${LOGIN}:${TOKEN} \
--request POST \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases <<END
{
"tag_name": "$1",
"target_commitish": "master",
"name": "$1",
"body": "$release_desc",
"draft": $draft,
"prerelease": false
}
END
#do we find that release ?
dField=$(getDataField "$github_answer" "errors.0.code")
if [ "$dField" == "already_exists" ]; then
throw "/!\ Release ${1} already exists, cannot create it again."
fi
#if created, we should have a github ID, show it!
dField=$(getDataField "$github_answer" "id")
if [ ! -z "$dField" ]; then
infoMsg " created with github ID: $dField"
else
dField=$(getDataField "$github_answer" "message")
throw " Failed. $dField"
fi
# show tag, which is needed to access a draft release
tagField=$(getDataField "$github_answer" "html_url")
if [ ! -z "$tagField" ]; then
infoMsg " tag is: $(echo $tagField | sed -e 's@.*/@@')"
else
msgField=$(getDataField "$github_answer" "message")
throw " Failed. $msgField"
fi
}
# --------
# FUNCTION: delete a github release
# !!!! se with caution: there is no undo !!!!
# arg1: github release id
# return: nothing
function deleteRelease(){
infoMsg "Deleting github release: $1"
curl --user ${LOGIN}:${TOKEN} \
--request DELETE \
--output "$github_answer" \
--silent \
--data @- \
https://api.github.com/repos/${OWNER}/${REPOSITORY}/releases/${1} <<END
END
infoMsg " done."
}
# --------
# FUNCTION: check if mandatory arguments are provided
# args: none
# return: nothing
# throw: exit application if value is empty
function checkMandatoryArg(){
local params=( "-l" "-t" "-o" "-r" "-c" )
local vars=( "$LOGIN" "$TOKEN" "$OWNER" "$REPOSITORY" "$COMMAND" )
for ((i=0;i<${#params[@]};++i)); do
#printf " %s %s\n" "${params[i]}" "${vars[i]}"
if [ -z "${vars[i]}" ]; then
throw "/!\ Missing mandatory argument: ${params[i]}"
fi
done
}
# --------
# FUNCTION: check if command is valid
# arg1: command name (a string)
# return: nothing
# throw: exit application if value is unknown
function checkCommand(){
local cmds=( "create" "draft" "flist" "rlist" "upload" "delete" "info" "erase")
if [[ "${cmds[*]}" =~ "$1" ]]; then
return 0
fi
throw "/!\ Unknown command: '$1'. Valid command is one of: ${cmds[*]}"
}
# --------
# FUNCTION: check if we have a git tag for commands that require it
function checkTag(){
if [ -z "$TAG" ]; then
throw "/!\ Missing mandatory argument: -d"
fi
}
# --------
# FUNCTION: check if we have some files for command delete/upload
# arg1: command name (a string)
# arg2: a string containing some file names
# return: nothing
# throw: exit application if we do not provide some files for commands delete/upload
function checkFileList(){
local cmds=( "upload" "delete")
if [[ "${cmds[*]}" =~ "$1" ]]; then
if [ -z "$2" ]; then
throw "/!\ no file(s) provided for command: $1"
fi
for fname in $2
do
if [ ! -f "$fname" ]; then
throw "/!\ not a file: $fname"
fi
done
fi
}
# ========================================================================================
# Section : tips and tricks...
# these two lines from solution 12 on http://unix.stackexchange.com/questions/48533/exit-shell-script-from-a-subshell
# to enable exit to work within the throw() method.
set -E
trap '[ "$?" -ne 77 ] || exit 77' ERR