forked from disorderedmaterials/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-release
executable file
·297 lines (261 loc) · 8.17 KB
/
update-release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash
# Update / create GitHub release with assets
REPO=""
RELEASE_TAG=""
RELEASE_NAME="UnnamedRelease"
RELEASE_BODY=""
RELEASE_BODY_FILE=""
RELEASE_BODY_FILE_WARN="false"
RELEASE_PRE=false
DELETE_EXISTING="false"
DELETE_EXISTING_ALWAYS="false"
DELETE_ALL_ASSETS="false"
UPDATE_TAG_SHA="false"
ARTIFACTS=""
NARTIFACTS=0
CURL_OPTS="-H 'Accept:application/vnd.github.v3+json' --silent --show-error"
usage()
{
echo "Usage: $0 [-r owner/repo] [-t release tag] [-n release_name] [-b release_body] [-f release_body_file] [-a] [-d] [-e] [-w] [-p] [-u] artifacts ..."
exit 1
}
# Parse options
while getopts ":r:t:n:b:f:pwdaeu" opt
do
case "${opt}" in
r)
REPO=${OPTARG}
echo "Repo set to '${REPO}'"
;;
t)
RELEASE_TAG=${OPTARG}
echo "Release tag is/will be '${RELEASE_TAG}'"
;;
b)
RELEASE_BODY=${OPTARG}
echo "Release body set to '${RELEASE_BODY}'"
;;
f)
RELEASE_BODY_FILE=${OPTARG}
echo "Release body will be set from markdown file '${RELEASE_BODY_FILE}'"
;;
w)
RELEASE_BODY_FILE_WARN="true"
echo "A non-existent release body file will not cause an error."
;;
n)
RELEASE_NAME=${OPTARG}
echo "Release name set to '${RELEASE_NAME}'"
;;
p)
RELEASE_PRE=true
echo "Release will be marked as a pre-release."
;;
a)
DELETE_EXISTING_ALWAYS="true"
echo "Existing release with the same tag will always be deleted if it exists."
;;
d)
DELETE_EXISTING="true"
echo "Existing release with the same tag will be deleted if it exists (unless HEAD/tag hashes match)"
;;
e)
DELETE_ALL_ASSETS="true"
echo "All existing release assets will be deleted"
;;
u)
UPDATE_TAG_SHA="true"
echo "SHA for specified tag will updated to reflect current HEAD"
;;
\?)
usage
;;
*)
usage
;;
esac
done
if [ "x$REPO" = "x" ]
then
echo "Error: The owner and repo must be supplied."
exit 1
fi
# Set up vars
echo "Repository is $REPO"
REPO_URL="https://api.github.com/repos/${REPO}"
echo "API URL is $REPO_URL"
# Enable erroring
set -e
# Disable command expansion so we don't leak secrets
set +x
# Assemble array of artifacts to upload to release
shift $(expr $OPTIND - 1)
while test $# -gt 0
do
if [ ! -f $1 ]
then
echo "Error: No such artifact '$1' exists."
exit 1
fi
ARTIFACTS[NARTIFACTS]=$1
let NARTIFACTS=NARTIFACTS+1
shift
done
# Load and process release body file if required
if [ "x$RELEASE_BODY_FILE" != "x" ]
then
if [ -e $RELEASE_BODY_FILE ]
then
# Read in the file, preserving line-feeds
RELEASE_BODY=$(<$RELEASE_BODY_FILE)
elif [ "$RELEASE_BODY_FILE_WARN" = "true" ]
then
echo "Warning: Release body file $RELEASE_BODY_FILE specified, but it does not exist."
else
echo "Error: Release body file $RELEASE_BODY_FILE does not exist."
exit 1
fi
fi
# Check tag
if [ "x$RELEASE_TAG" = "x" ]
then
echo "Error: No tag for current release - check your setup (did you push a release without --tags?)."
exit 1
fi
echo "Tag for release is: $RELEASE_TAG"
# Get available releases information
RELEASES=$(curl ${CURL_OPTS} -XGET --header "Authorization: token ${GITHUB_TOKEN}" ${REPO_URL}/releases)
# Check for error or no data
if [[ $RELEASES =~ "documentation_url" ]]
then
MESSAGE=$(echo $RELEASES | jq ". | .message")
echo "Error returned: "$MESSAGE
exit 1
fi
# Does our tag already exist (i.e. the release already exists)?
echo "Checking for presence of current release..."
FILTER=".[] | select(.tag_name | match(\"$RELEASE_TAG\"))"
CURRENT_RELEASEINFO=$(echo $RELEASES | jq "$FILTER")
CURRENT_URL=$(echo $CURRENT_RELEASEINFO | jq -r '. | .url')
if [ "x$CURRENT_URL" != "x" ]
then
echo " -- Found existing release with tag $RELEASE_TAG"
echo " url: $CURRENT_URL"
# Get short hash for existing tag
TAG_HASH=$(echo $CURRENT_RELEASEINFO | jq -r '. | .committish')
echo " hash: $TAG_HASH"
# Delete existing release?
if [ "$DELETE_EXISTING" = "true" ]
then
# Check hash against HEAD
HEAD_HASH=`git rev-parse --short HEAD`
echo " head: $HEAD_HASH"
if [ "$TAG_HASH" != "$HEAD_HASH" ] || [ "$DELETE_EXISTING_ALWAYS" = "true" ]
then
echo " -- Removing existing release..."
curl ${CURL_OPTS} -XDELETE --header "Authorization: token ${GITHUB_TOKEN}" $CURRENT_URL
# Reset url so it gets recreated
CURRENT_URL="x"
fi
else
# Patch the current release
# Set GitHub username and email
git config --local user.name "trisyoungs"
git config --local user.email "[email protected]"
# Update tag SHA?
if [ "$UPDATE_TAG_SHA" = "true" ]
then
TAG_HASH=$(git rev-parse HEAD)
echo " -- Updating tag hash to that of the current HEAD (${TAG_HASH})"
RESULT=$(curl ${CURL_OPTS} -XPATCH --header "Authorization: token ${GITHUB_TOKEN}" --data '{ "sha": "'${TAG_HASH}'" }' ${REPO_URL}/git/refs/tags/${RELEASE_TAG})
fi
# Construct release information
RELEASE_DATA=$(jq --null-input --arg tag "$RELEASE_TAG" --arg commitish "$TAG_HASH" --arg name "$RELEASE_NAME" --arg body "$RELEASE_BODY" --argjson pre $RELEASE_PRE '{"tag_name": $tag, "target_commitish": $commitish, "name": $name, "body": $body, "draft": false, "prerelease": $pre}')
echo "Release data is:"
echo $RELEASE_DATA
# Patch release
echo " -- Patching existing release info..."
RESULT=$(curl ${CURL_OPTS} -XPATCH --header "Authorization: token ${GITHUB_TOKEN}" --data "$RELEASE_DATA" ${CURRENT_URL})
# Delete existing assets?
if [ "$DELETE_ALL_ASSETS" = "true" ]
then
ASSETS=$(curl ${CURL_OPTS} -XGET --header "Authorization: token ${GITHUB_TOKEN}" ${CURRENT_URL}/assets)
ASSET_IDS=$(echo $ASSETS | jq ".[] | .id")
for id in $ASSET_IDS
do
echo " -- Removing release asset id ${id}..."
RESULT=$(curl ${CURL_OPTS} -XDELETE --header "Authorization: token ${GITHUB_TOKEN}" ${REPO_URL}/releases/assets/${id})
done
fi
fi
fi
# Create new release?
if [ "x$CURRENT_URL" = "x" ]
then
echo " -- No release with tag $RELEASE_TAG currently exists."
echo " A new one will be created."
# Get the hash of the current revision
TAG_HASH=$(git rev-parse HEAD)
# Construct release information
RELEASE_DATA=$(jq --null-input --arg tag "$RELEASE_TAG" --arg commitish "$TAG_HASH" --arg name "$RELEASE_NAME" --arg body "$RELEASE_BODY" --argjson pre $RELEASE_PRE '{"tag_name": $tag, "target_commitish": $commitish, "name": $name, "body": $body, "draft": false, "prerelease": $pre}')
echo "Release data is:"
echo $RELEASE_DATA
# Create release
CURRENT_RELEASEINFO=$(curl ${CURL_OPTS} --header "Authorization: token ${GITHUB_TOKEN}" --data "$RELEASE_DATA" ${REPO_URL}/releases)
echo "Release creation returned:"
echo $CURRENT_RELEASEINFO
# Check for messages
MSG=$(echo $CURRENT_RELEASEINFO | jq -r '. | .message')
if [ "x$MSG" = "x" ]
then
echo "Received message:"
echo $MSG
fi
# Check errors
ERRORS=$(echo $CURRENT_RELEASEINFO | jq -r '. | .errors')
if [ "x$ERRORS" = "x" ]
then
echo "Error: Failed to create release."
echo "Reported error(s) are:"
echo $ERRORS
exit 1
fi
# Extract url
CURRENT_URL=$(echo $CURRENT_RELEASEINFO | jq -r '. | .url')
fi
# Upload assets if provided
if (( $NARTIFACTS > 0 ))
then
# Upload specified assets
echo "Uploading assets to release..."
# Extract upload path for assets
# -- Need to strip trailing {?name,label} part from the returned url
UPLOAD_URL=$(echo $CURRENT_RELEASEINFO | jq -r '. | .upload_url' | cut -d '{' -f 1)
if [ "x$UPLOAD_URL" = "x" ]
then
echo "Error: Failed to determine upload url for release."
exit 1
fi
echo " -- Upload url is: $UPLOAD_URL"
for file in ${ARTIFACTS[@]}
do
# Get the basename of the file so we call it the correct name
BASENAME=$(basename "${file}")
echo "... $file (basename=$BASENAME)"
ARTIFACT_DATA=$(curl ${CURL_OPTS} --header "Authorization: token ${GITHUB_TOKEN}" \
--header "Accept: application/vnd.github.manifold-preview" \
--header "Content-Type: application/octet-stream" \
-T $file \
"$UPLOAD_URL?name=$BASENAME")
ARTIFACT_ID=$(echo $ARTIFACT_DATA | jq -r '. | .id')
ARTIFACT_URL=$(echo $ARTIFACT_DATA | jq -r '. | .browser_download_url')
if [ "x$ARTIFACT_ID" = "x" ] || [ "$ARTIFACT_ID" = "null" ]
then
echo "Error: Failed to upload artifact."
echo "Returned data was: $ARTIFACT_DATA"
exit 1
fi
echo " -- Success (id: $ARTIFACT_ID, url: $ARTIFACT_URL)"
done
fi
# All done.