-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-release.sh
executable file
·166 lines (138 loc) · 4.32 KB
/
upload-release.sh
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
#!/bin/bash -e
# Upload a TeXShop release (release notes, binary, sources) to GitHub
######################################################################
#
# Various little helper functions
# print notices in green
notice() {
printf "\033[32m%s\033[0m\n" "$*"
}
# print notices in yellow
warning() {
printf "\033[33mWARNING: %s\033[0m\n" "$*"
}
# print error in red and exit
error() {
printf "\033[31mERROR: %s\033[0m\n" "$*"
exit 1
}
# helper function for parsing GitHub's JSON output. Right now,
# we only extra the value of a single key at a time. This means
# we may end up parsing the same JSON data two times, but that
# doesn't really matter as it is tiny.
json_get_key() {
echo "$response" | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get("'$1'","")'
}
######################################################################
#
# Command line processing
#
FORCE=no
while true; do
case "$1" in
#-r | --relnotes ) RELNOTES="$2"; shift 2 ;;
--token ) TOKEN="$2"; shift 2 ;;
-f | --force ) FORCE=yes; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ $# -lt 1 ]; then
error "Missing argument, must specify version"
fi
VERSION=$1
if [ x"$VERSION" = "x" ] ; then
error "No version specified"
fi
TAG=v$VERSION
#
RELNOTES=$VERSION/relnotes-$VERSION.txt
if [ -f $RELNOTES ] ; then
RELNOTES_BODY="$(python -c 'import json,sys; print json.dumps(sys.stdin.read())' <$RELNOTES)"
else
#error "Could not access release notes at $RELNOTES"
RELNOTES_BODY='""'
fi
API_URL=https://api.github.com/repos/TeXShop/TeXShop/releases
UPLOAD_URL=https://uploads.github.com/repos/TeXShop/TeXShop/releases
######################################################################
#
# Fetch GitHub oauth token, used to authenticate the following commands.
# See https://help.github.com/articles/git-automation-with-oauth-tokens/
#
if [ x$TOKEN = x ] ; then
TOKEN=`git config --get github.token || echo`
fi
if [ x$TOKEN = x -a -r ~/.github_shell_token ] ; then
TOKEN=`cat ~/.github_shell_token`
fi
if [ x$TOKEN = x ] ; then
error "could not determine GitHub access token"
fi
echo ""
######################################################################
#
# Create the GitHub release
#
# check if release already exists
response=`curl -s -S -X GET "$API_URL/tags/$TAG?access_token=$TOKEN"`
MESSAGE=`json_get_key message`
RELEASE_ID=`json_get_key id`
if [ "$MESSAGE" = "Not Found" ] ; then
MESSAGE= # release does not yet exist -> that's how we like it
elif [ x"$RELEASE_ID" != x ] ; then
# release already exists -> error out or delete it
if [ x$FORCE = xyes ] ; then
notice "Deleting existing release $TAG from GitHub"
response=`curl --fail -s -S -X DELETE "$API_URL/$RELEASE_ID?access_token=$TOKEN"`
MESSAGE=
else
error "release $TAG already exists on GitHub, aborting (use --force to override this)"
fi
fi
if [ x"$MESSAGE" != x ] ; then
error "accessing GitHub failed: $MESSAGE"
fi
# Create the release by sending suitable JSON
DATA=`cat <<EOF
{
"tag_name": "$TAG",
"name": "$VERSION",
"body": $RELNOTES_BODY,
"draft": false,
"prerelease": false
}
EOF
`
notice "Creating new release $TAG on GitHub"
response=`curl -s -S -H "Content-Type: application/json" \
-X POST --data "$DATA" "$API_URL?access_token=$TOKEN"`
MESSAGE=`json_get_key message`
if [ x"$MESSAGE" != x ] ; then
error "creating release on GitHub failed: $MESSAGE"
fi
RELEASE_ID=`json_get_key id`
if [ x"$RELEASE_ID" = x ] ; then
error "creating release on GitHub failed: no release id"
fi
######################################################################
#
# Create and upload all requested archive files (as per ARCHIVE_FORMATS)
#
cd "$TMP_DIR"
echo ""
for ARCHIVENAME in texshop-$VERSION.zip texshopsource-$VERSION.zip; do
MIMETYPE="application/zip"
FULLNAME="$VERSION/$ARCHIVENAME"
if [ ! -f $FULLNAME ] ; then
continue
fi
notice "Uploading $ARCHIVENAME with mime type $MIMETYPE"
curl --fail --progress-bar -o "response.log" \
-X POST "$UPLOAD_URL/$RELEASE_ID/assets?name=$ARCHIVENAME" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $TOKEN" \
-H "Content-Type: $MIMETYPE" \
--data-binary @"$FULLNAME" || error "An error occurred, please consult response.log"
done
exit 0