Skip to content

Commit 9f430bb

Browse files
committed
Merge branch 'feat/aws_s3'
2 parents 03a6f9a + 4044b54 commit 9f430bb

File tree

7 files changed

+568
-226
lines changed

7 files changed

+568
-226
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

.github/scripts/on-pages.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function git_remove_from_pages {
2323
local sha
2424
local message
2525

26-
info=$(curl -s -k -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")
26+
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")
2727
type=$(echo "$info" | jq -r '.type')
2828

2929
if [ ! "$type" == "file" ]; then
@@ -38,7 +38,7 @@ function git_remove_from_pages {
3838
sha=$(echo "$info" | jq -r '.sha')
3939
message="Deleting "$(basename "$path")
4040
local json="{\"branch\":\"gh-pages\",\"message\":\"$message\",\"sha\":\"$sha\"}"
41-
echo "$json" | curl -s -k -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.raw+json" -X DELETE --data @- "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path"
41+
echo "$json" | curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.raw+json" -X DELETE --data @- "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path"
4242
}
4343

4444
function git_upload_to_pages {
@@ -56,7 +56,7 @@ function git_upload_to_pages {
5656
local sha=""
5757
local content=""
5858

59-
info=$(curl -s -k -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")
59+
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")
6060
type=$(echo "$info" | jq -r '.type')
6161
message=$(basename "$path")
6262

@@ -74,7 +74,7 @@ function git_upload_to_pages {
7474
content=$(base64 -i "$src")
7575
data="{\"branch\":\"gh-pages\",\"message\":\"$message\",\"content\":\"$content\"$sha}"
7676

77-
echo "$data" | curl -s -k -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"
77+
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"
7878
}
7979

8080
function git_safe_upload_to_pages {

0 commit comments

Comments
 (0)