diff options
Diffstat (limited to 'tools/github-release.sh')
-rwxr-xr-x | tools/github-release.sh | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tools/github-release.sh b/tools/github-release.sh new file mode 100755 index 0000000..4f760c0 --- /dev/null +++ b/tools/github-release.sh | |||
@@ -0,0 +1,109 @@ | |||
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 accepts the following parameters: | ||
12 | # | ||
13 | # * owner | ||
14 | # * repo | ||
15 | # * tag | ||
16 | # * type (asset or edit) | ||
17 | # * filename | ||
18 | # * github_api_token | ||
19 | # * overwrite (optional, could be ture, false, delete, default to be false) | ||
20 | # | ||
21 | # Script to upload a release asset using the GitHub API v3. | ||
22 | # | ||
23 | # Example: | ||
24 | # | ||
25 | # github-release.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 type=asset filename=./build.zip overwrite=true | ||
26 | # | ||
27 | |||
28 | # Check dependencies. | ||
29 | set -e | ||
30 | |||
31 | # Validate settings. | ||
32 | [ "$TRACE" ] && set -x | ||
33 | |||
34 | CONFIG=$@ | ||
35 | |||
36 | for line in $CONFIG; do | ||
37 | eval "$line" | ||
38 | done | ||
39 | |||
40 | # Define variables. | ||
41 | GH_API="https://api.github.com" | ||
42 | GH_REPO="$GH_API/repos/$owner/$repo" | ||
43 | GH_TAGS="$GH_REPO/releases/tags/$tag" | ||
44 | AUTH="Authorization: token $github_api_token" | ||
45 | |||
46 | if [ "$tag" = 'LATEST' ]; then | ||
47 | GH_TAGS="$GH_REPO/releases/latest" | ||
48 | fi | ||
49 | |||
50 | # Validate token. | ||
51 | curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } | ||
52 | |||
53 | # Read asset tags. | ||
54 | response=$(curl -sH "$AUTH" $GH_TAGS) | ||
55 | |||
56 | # Get ID of the release. | ||
57 | eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/') | ||
58 | [ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } | ||
59 | |||
60 | upload_asset() { | ||
61 | # Get ID of the asset based on given filename. | ||
62 | # If exists, delete it. | ||
63 | eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') | ||
64 | if [ "$asset_id" = "" ]; then | ||
65 | echo "No need to overwrite asset" | ||
66 | else | ||
67 | if [ "$overwrite" = "true" ] || [ "$overwrite" = "delete" ]; then | ||
68 | echo "Deleting asset($asset_id)... " | ||
69 | curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id" | ||
70 | if [ "$overwrite" = "delete" ]; then | ||
71 | exit 0 | ||
72 | fi | ||
73 | else | ||
74 | echo "File already exists on tag $tag" | ||
75 | echo "If you want to overwrite it, set overwrite=true" | ||
76 | exit 1 | ||
77 | fi | ||
78 | fi | ||
79 | |||
80 | # Upload asset | ||
81 | echo "Uploading asset... " | ||
82 | |||
83 | # Construct url | ||
84 | GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)" | ||
85 | |||
86 | curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET | ||
87 | } | ||
88 | |||
89 | edit_release() { | ||
90 | GH_RELEASE="$GH_REPO/releases/$release_id" | ||
91 | body=$(cat <<EOF | ||
92 | { | ||
93 | "tag_name": "$tag", | ||
94 | "target_commitish": "master", | ||
95 | "name": "daily-taiwan-pbf", | ||
96 | "body": "$(cat $filename | sed 's/"/\\"/g; $! s/$/\\n/' | tr -d '\n')", | ||
97 | "draft": false, | ||
98 | "prerelease": false | ||
99 | } | ||
100 | EOF | ||
101 | ) | ||
102 | curl -v -X PATCH -H "$AUTH" -H "Content-Type: application/json" -d "$body" $GH_RELEASE | ||
103 | } | ||
104 | |||
105 | case $type in | ||
106 | asset) upload_asset;; | ||
107 | edit) edit_release;; | ||
108 | *) echo "type should be 'asset' or 'edit'";; | ||
109 | esac | ||