aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/github/github-release.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/github/github-release.sh')
-rwxr-xr-xbin/github/github-release.sh154
1 files changed, 154 insertions, 0 deletions
diff --git a/bin/github/github-release.sh b/bin/github/github-release.sh
new file mode 100755
index 0000000..70ba672
--- /dev/null
+++ b/bin/github/github-release.sh
@@ -0,0 +1,154 @@
1#!/usr/bin/env sh
2#
3# Author: Hsieh Chin Fan (typebrook) <typebrook@gmail.com>
4# License: MIT
5# https://gist.github.com/typebrook/4947769e266173303d8848f496e272c9
6#
7# Originally created by stefanbuck
8# fork from: https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
9#
10# --
11# This script manages a release note or its asset with GitHub API v3.
12# It accepts the following parameters:
13#
14# * repo
15# * tag
16# * type (asset or edit)
17# * filename
18# * github_api_token
19# * action (optional, could be upload, overwrite, rename, delete, default to be upload)
20# * extra (optional, new filename for action 'rename')
21# * create (optional, create a new release if it doesn't exist)
22#
23# Example:
24#
25# # Upload a file as asset. If there is a asset with the same filename, then overwrite it.
26# github-release.sh github_api_token=TOKEN repo=stefanbuck/playground tag=v0.1.0 type=asset filename=./build.zip overwrite=true
27#
28# # Edit a release note with file content
29# github-release.sh github_api_token=TOKEN repo=stefanbuck/playground tag=v0.1.0 type=edit filename=note
30
31# Check dependencies.
32set -e
33
34# Validate settings.
35[ "$TRACE" ] && set -x
36# Keep tty
37exec 3>&1
38
39CONFIG=$@
40
41for line in $CONFIG; do
42 eval "$line"
43done
44
45# Define variables.
46GH_API="https://api.github.com"
47GH_REPO="$GH_API/repos/$repo"
48GH_TAGS="$GH_REPO/releases/tags/$tag"
49AUTH="Authorization: token $github_api_token"
50
51if [ "$tag" = 'LATEST' ]; then
52 GH_TAGS="$GH_REPO/releases/latest"
53fi
54if [ "$type" = '' ]; then
55 sed -E -n -e ' /^$/ q; /# --/,$ s/^# *//p' "$0"
56 exit 0
57fi
58
59# Validate token.
60curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
61
62# Read asset tags.
63response=$(curl -sH "$AUTH" $GH_TAGS)
64
65# Get ID of the release.
66release_id=$(echo "$response" | grep -m1 -w \"id\": | sed -E -e 's/[^0-9]//g')
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
87
88post_asset() {
89 # Upload asset
90 echo "Uploading asset... " >&3
91 # Construct url
92 GH_ASSET="https://uploads.github.com/repos/$repo/releases/$release_id/assets?name=$1"
93
94 curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET
95}
96
97rename_asset() {
98 echo "Renaming asset($1) from $2 to $3" >&3
99 curl -X PATCH -H "$AUTH" -H "Content-Type: application/json" \
100 --data "{\"name\":\"$3\", \"label\":\"$3\"}" "$GH_REPO/releases/assets/$1"
101}
102
103delete_asset() {
104 echo "Deleting asset($1)... " >&3
105 curl -X "DELETE" -H "$AUTH" "$GH_REPO/releases/assets/$1"
106}
107
108upload_asset() {
109 # Get ID of the asset based on given filename.
110 # If exists, delete it.
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/')
112 if [ "$asset_id" = "" ]; then
113 echo "No need to overwrite asset"
114 post_asset $(basename $filename)
115 else
116 if [ "$action" = "overwrite" ]; then
117 new_asset_id=$(post_asset "bak_$(basename $filename)" | sed -E 's/^\{[^{]+"id":([0-9]+).+$/\1/')
118 [ "$new_asset_id" = "" ] && exit 1 || delete_asset "$asset_id"
119
120 rename_asset "$new_asset_id" "bak_$(basename $filename)" "$(basename $filename)"
121 elif [ "$action" = "rename" ]; then
122 rename_asset "$asset_id" "$filename" "$extra"
123 elif [ "$action" = "delete" ]; then
124 delete_asset "$asset_id"
125 exit 0
126 else
127 echo "File already exists on tag $tag"
128 echo "If you want to overwrite it, set action=overwrite"
129 exit 1
130 fi
131 fi
132}
133
134edit_release() {
135 GH_RELEASE="$GH_REPO/releases/$release_id"
136 body=$(cat <<EOF
137{
138 "tag_name": "$tag",
139 "target_commitish": "master",
140 "name": "$tag",
141 "body": "$(cat $filename | sed 's/"/\\"/g; $! s/$/\\n/' | tr -d '\n')",
142 "draft": false,
143 "prerelease": false
144}
145EOF
146 )
147 curl -X PATCH -H "$AUTH" -H "Content-Type: application/json" -d "$body" $GH_RELEASE
148}
149
150case $type in
151 asset) upload_asset;;
152 edit) edit_release;;
153 *) echo "type should be 'asset' or 'edit'";;
154esac