aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/github-release.sh
diff options
context:
space:
mode:
Diffstat (limited to 'tools/github-release.sh')
-rwxr-xr-xtools/github-release.sh44
1 files changed, 33 insertions, 11 deletions
diff --git a/tools/github-release.sh b/tools/github-release.sh
index b8f6dba..70ba672 100755
--- a/tools/github-release.sh
+++ b/tools/github-release.sh
@@ -18,6 +18,7 @@
18# * github_api_token 18# * github_api_token
19# * action (optional, could be upload, overwrite, rename, delete, default to be upload) 19# * action (optional, could be upload, overwrite, rename, delete, default to be upload)
20# * extra (optional, new filename for action 'rename') 20# * extra (optional, new filename for action 'rename')
21# * create (optional, create a new release if it doesn't exist)
21# 22#
22# Example: 23# Example:
23# 24#
@@ -32,6 +33,8 @@ set -e
32 33
33# Validate settings. 34# Validate settings.
34[ "$TRACE" ] && set -x 35[ "$TRACE" ] && set -x
36# Keep tty
37exec 3>&1
35 38
36CONFIG=$@ 39CONFIG=$@
37 40
@@ -60,26 +63,45 @@ curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or
60response=$(curl -sH "$AUTH" $GH_TAGS) 63response=$(curl -sH "$AUTH" $GH_TAGS)
61 64
62# Get ID of the release. 65# Get ID of the release.
63eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/') 66release_id=$(echo "$response" | grep -m1 -w \"id\": | sed -E -e 's/[^0-9]//g')
64[ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } 67if [ -z "$release_id" ]; then
68 if [ "$create" = 'true' ]; then
69 body=$(cat <<EOF
70{
71 "tag_name": "$tag",
72 "target_commitish": "master",
73 "name": "$tag",
74 "body": "",
75 "draft": false,
76 "prerelease": false
77}
78EOF
79 )
80 response=$(curl -H "$AUTH" -H "Content-Type: application/json" -d "$body" "$GH_REPO/releases")
81 release_id=$(echo "$response" | grep -m1 -w \"id\": | sed -E -e 's/[^0-9]//g')
82 else
83 echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2
84 exit 1
85 fi
86fi
65 87
66post_asset() { 88post_asset() {
67 # Upload asset 89 # Upload asset
68 echo "Uploading asset... " 90 echo "Uploading asset... " >&3
69 # Construct url 91 # Construct url
70 GH_ASSET="https://uploads.github.com/repos/$repo/releases/$release_id/assets?name=$(basename $1)" 92 GH_ASSET="https://uploads.github.com/repos/$repo/releases/$release_id/assets?name=$1"
71 93
72 curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET 94 curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET
73} 95}
74 96
75rename_asset() { 97rename_asset() {
76 echo "Renaming asset($1) from $2 to $3" 98 echo "Renaming asset($1) from $2 to $3" >&3
77 curl -X PATCH -H "$AUTH" -H "Content-Type: application/json" \ 99 curl -X PATCH -H "$AUTH" -H "Content-Type: application/json" \
78 --data "{\"name\":\"$3\"}" "$GH_REPO/releases/assets/$1" 100 --data "{\"name\":\"$3\", \"label\":\"$3\"}" "$GH_REPO/releases/assets/$1"
79} 101}
80 102
81delete_asset() { 103delete_asset() {
82 echo "Deleting asset($1)... " 104 echo "Deleting asset($1)... " >&3
83 curl -X "DELETE" -H "$AUTH" "$GH_REPO/releases/assets/$1" 105 curl -X "DELETE" -H "$AUTH" "$GH_REPO/releases/assets/$1"
84} 106}
85 107
@@ -89,13 +111,13 @@ upload_asset() {
89 eval $(echo "$response" | grep -C2 "\"name\": \"$(basename $filename)\"" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') 111 eval $(echo "$response" | grep -C2 "\"name\": \"$(basename $filename)\"" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/')
90 if [ "$asset_id" = "" ]; then 112 if [ "$asset_id" = "" ]; then
91 echo "No need to overwrite asset" 113 echo "No need to overwrite asset"
92 post_asset $filename 114 post_asset $(basename $filename)
93 else 115 else
94 if [ "$action" = "overwrite" ]; then 116 if [ "$action" = "overwrite" ]; then
95 new_asset_id=$(post_asset "bak_$filename" | sed -E 's/^\{[^{]+"id":([0-9]+).+$/\1/') 117 new_asset_id=$(post_asset "bak_$(basename $filename)" | sed -E 's/^\{[^{]+"id":([0-9]+).+$/\1/')
96 [ "$new_asset_id" = "" ] && exit 1 || delete_asset "$asset_id" 118 [ "$new_asset_id" = "" ] && exit 1 || delete_asset "$asset_id"
97 119
98 rename_asset "$new_asset_id" "bak_$filename" "$filename" 120 rename_asset "$new_asset_id" "bak_$(basename $filename)" "$(basename $filename)"
99 elif [ "$action" = "rename" ]; then 121 elif [ "$action" = "rename" ]; then
100 rename_asset "$asset_id" "$filename" "$extra" 122 rename_asset "$asset_id" "$filename" "$extra"
101 elif [ "$action" = "delete" ]; then 123 elif [ "$action" = "delete" ]; then
@@ -115,7 +137,7 @@ edit_release() {
115{ 137{
116 "tag_name": "$tag", 138 "tag_name": "$tag",
117 "target_commitish": "master", 139 "target_commitish": "master",
118 "name": "daily-taiwan-pbf", 140 "name": "$tag",
119 "body": "$(cat $filename | sed 's/"/\\"/g; $! s/$/\\n/' | tr -d '\n')", 141 "body": "$(cat $filename | sed 's/"/\\"/g; $! s/$/\\n/' | tr -d '\n')",
120 "draft": false, 142 "draft": false,
121 "prerelease": false 143 "prerelease": false