diff options
| author | typebrook <typebrook@gmail.com> | 2020-03-09 15:07:02 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-03-09 15:07:02 +0800 |
| commit | b7d4b0f0f27558bf4d82585e7692a704ac35466b (patch) | |
| tree | 0d6d75759bb16f81919c5ffab9e93d5fe28a5743 | |
| parent | 57bc75def35432cad7df5b7eaa47740ca7247e75 (diff) | |
update
| -rw-r--r-- | alias | 2 | ||||
| -rwxr-xr-x | tools/upload-github-release-asset.sh | 69 |
2 files changed, 46 insertions, 25 deletions
| @@ -96,7 +96,7 @@ gcg() { | |||
| 96 | git clone git@github.com:$1/$2.git && cd $(basename $2) | 96 | git clone git@github.com:$1/$2.git && cd $(basename $2) |
| 97 | } | 97 | } |
| 98 | github_release_asset() { | 98 | github_release_asset() { |
| 99 | command="upload-github-release-asset.sh\n github_api_token=$GITHUB_API_TOKEN\n owner=typebrook\n repo=tig\n tag=LATEST\n filename=$(which tig)\n overwrite=false" | 99 | command="upload-github-release-asset.sh\n github_api_token=$GITHUB_API_TOKEN\n owner=typebrook\n repo=tig\n tag=LATEST\n type=asset\n filename=$(which tig)\n overwrite=false" |
| 100 | prompt "$command" | 100 | prompt "$command" |
| 101 | } | 101 | } |
| 102 | 102 | ||
diff --git a/tools/upload-github-release-asset.sh b/tools/upload-github-release-asset.sh index af78fee..acff25e 100755 --- a/tools/upload-github-release-asset.sh +++ b/tools/upload-github-release-asset.sh | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | # * owner | 13 | # * owner |
| 14 | # * repo | 14 | # * repo |
| 15 | # * tag | 15 | # * tag |
| 16 | # * type (asset or edit) | ||
| 16 | # * filename | 17 | # * filename |
| 17 | # * github_api_token | 18 | # * github_api_token |
| 18 | # * overwrite (optional, could be ture, false, delete, default to be false) | 19 | # * overwrite (optional, could be ture, false, delete, default to be false) |
| @@ -26,7 +27,6 @@ | |||
| 26 | 27 | ||
| 27 | # Check dependencies. | 28 | # Check dependencies. |
| 28 | set -e | 29 | set -e |
| 29 | xargs=$(which gxargs || which xargs) | ||
| 30 | 30 | ||
| 31 | # Validate settings. | 31 | # Validate settings. |
| 32 | [ "$TRACE" ] && set -x | 32 | [ "$TRACE" ] && set -x |
| @@ -57,29 +57,50 @@ response=$(curl -sH "$AUTH" $GH_TAGS) | |||
| 57 | eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/') | 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; } | 58 | [ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } |
| 59 | 59 | ||
| 60 | # Get ID of the asset based on given filename. | 60 | upload_asset() { |
| 61 | # If exists, delete it. | 61 | # Get ID of the asset based on given filename. |
| 62 | eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') | 62 | # If exists, delete it. |
| 63 | if [ "$asset_id" = "" ]; then | 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 | echo "No need to overwrite asset" | 64 | if [ "$asset_id" = "" ]; then |
| 65 | else | 65 | echo "No need to overwrite asset" |
| 66 | if [ "$overwrite" = "true" ] || [ "$overwrite" = "delete" ]; then | 66 | else |
| 67 | echo "Deleting asset($asset_id)... " | 67 | if [ "$overwrite" = "true" ] || [ "$overwrite" = "delete" ]; then |
| 68 | curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id" | 68 | echo "Deleting asset($asset_id)... " |
| 69 | if [ "$overwrite" = "delete" ]; then | 69 | curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id" |
| 70 | exit 0 | 70 | if [ "$overwrite" = "delete" ]; then |
| 71 | fi | 71 | exit 0 |
| 72 | else | 72 | fi |
| 73 | echo "File already exists on tag $tag" | 73 | else |
| 74 | echo "If you want to overwrite it, set overwrite=true" | 74 | echo "File already exists on tag $tag" |
| 75 | exit 1 | 75 | echo "If you want to overwrite it, set overwrite=true" |
| 76 | fi | 76 | exit 1 |
| 77 | fi | 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)" | ||
| 78 | 85 | ||
| 79 | # Upload asset | 86 | curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET |
| 80 | echo "Uploading asset... " | 87 | } |
| 81 | 88 | ||
| 82 | # Construct url | 89 | edit_release() { |
| 83 | GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)" | 90 | GH_RELEASE="$GH_REPO/releases/$release_id" |
| 91 | curl -v -X PATCH -H "$AUTH" -H "Content-Type: application/json" $GH_RELEASE \ | ||
| 92 | -d "{ \ | ||
| 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 | } | ||
| 84 | 101 | ||
| 85 | curl --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET | 102 | case $type in |
| 103 | asset) upload_asset;; | ||
| 104 | edit) edit_release;; | ||
| 105 | *) echo "type should be 'asset' or 'edit'";; | ||
| 106 | esac | ||