|
| 1 | +#!/bin/bash |
| 2 | +# Common functions for GitHub Release operations |
| 3 | +# This library is sourced by release scripts |
| 4 | +# Disable shellcheck warning about $? uses. |
| 5 | +# shellcheck disable=SC2181 |
| 6 | + |
| 7 | +function get_file_size { |
| 8 | + local file="$1" |
| 9 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 10 | + eval "$(stat -s "$file")" |
| 11 | + local res="$?" |
| 12 | + echo "${st_size:?}" |
| 13 | + return $res |
| 14 | + else |
| 15 | + stat --printf="%s" "$file" |
| 16 | + return $? |
| 17 | + fi |
| 18 | +} |
| 19 | + |
| 20 | +function git_upload_asset { |
| 21 | + local file="$1" |
| 22 | + local release_id="$2" |
| 23 | + local name |
| 24 | + name=$(basename "$file") |
| 25 | + # local mime=$(file -b --mime-type "$file") |
| 26 | + curl -X POST -sH "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/octet-stream" --data-binary @"$file" "https://uploads.github.com/repos/$GITHUB_REPOSITORY/releases/$release_id/assets?name=$name" |
| 27 | +} |
| 28 | + |
| 29 | +function git_safe_upload_asset { |
| 30 | + local file="$1" |
| 31 | + local release_id="$2" |
| 32 | + local name |
| 33 | + local size |
| 34 | + local upload_res |
| 35 | + |
| 36 | + name=$(basename "$file") |
| 37 | + size=$(get_file_size "$file") |
| 38 | + |
| 39 | + if ! upload_res=$(git_upload_asset "$file" "$release_id"); then |
| 40 | + >&2 echo "ERROR: Failed to upload '$name' ($?)" |
| 41 | + return 1 |
| 42 | + fi |
| 43 | + |
| 44 | + up_size=$(echo "$upload_res" | jq -r '.size') |
| 45 | + if [ "$up_size" -ne "$size" ]; then |
| 46 | + >&2 echo "ERROR: Uploaded size does not match! $up_size != $size" |
| 47 | + #git_delete_asset |
| 48 | + return 1 |
| 49 | + fi |
| 50 | + echo "$upload_res" | jq -r '.browser_download_url' |
| 51 | + return $? |
| 52 | +} |
| 53 | + |
| 54 | +function git_upload_to_pages { |
| 55 | + local path=$1 |
| 56 | + local src=$2 |
| 57 | + |
| 58 | + if [ ! -f "$src" ]; then |
| 59 | + >&2 echo "Input is not a file! Aborting..." |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + |
| 63 | + local info |
| 64 | + local type |
| 65 | + local message |
| 66 | + local sha="" |
| 67 | + local content="" |
| 68 | + |
| 69 | + info=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.object+json" -X GET "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path?ref=gh-pages") |
| 70 | + type=$(echo "$info" | jq -r '.type') |
| 71 | + message=$(basename "$path") |
| 72 | + |
| 73 | + if [ "$type" == "file" ]; then |
| 74 | + sha=$(echo "$info" | jq -r '.sha') |
| 75 | + sha=",\"sha\":\"$sha\"" |
| 76 | + message="Updating $message" |
| 77 | + elif [ ! "$type" == "null" ]; then |
| 78 | + >&2 echo "Wrong type '$type'" |
| 79 | + return 1 |
| 80 | + else |
| 81 | + message="Creating $message" |
| 82 | + fi |
| 83 | + |
| 84 | + content=$(base64 -i "$src") |
| 85 | + data="{\"branch\":\"gh-pages\",\"message\":\"$message\",\"content\":\"$content\"$sha}" |
| 86 | + |
| 87 | + echo "$data" | curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.raw+json" -X PUT --data @- "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path" |
| 88 | +} |
| 89 | + |
| 90 | +function git_safe_upload_to_pages { |
| 91 | + local path=$1 |
| 92 | + local file="$2" |
| 93 | + local name |
| 94 | + local size |
| 95 | + local upload_res |
| 96 | + |
| 97 | + name=$(basename "$file") |
| 98 | + size=$(get_file_size "$file") |
| 99 | + |
| 100 | + if ! upload_res=$(git_upload_to_pages "$path" "$file"); then |
| 101 | + >&2 echo "ERROR: Failed to upload '$name' ($?)" |
| 102 | + return 1 |
| 103 | + fi |
| 104 | + |
| 105 | + up_size=$(echo "$upload_res" | jq -r '.content.size') |
| 106 | + if [ "$up_size" -ne "$size" ]; then |
| 107 | + >&2 echo "ERROR: Uploaded size does not match! $up_size != $size" |
| 108 | + #git_delete_asset |
| 109 | + return 1 |
| 110 | + fi |
| 111 | + echo "$upload_res" | jq -r '.content.download_url' |
| 112 | + return $? |
| 113 | +} |
0 commit comments