-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-gh-release.sh
executable file
·156 lines (144 loc) · 5.69 KB
/
create-gh-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
#!/usr/bin/env bash
# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
# https://gist.github.com/mcguffin/746afffa0929ca8e2ea2ba8538776742
# https://gist.github.com/mjdietzx/6ec00ebd1223ca1cf87fa0c80e0bf84e
set -e
# Define some useful variables
# wpforms-epfl-payonline
PROJECT_NAME=$(basename ${PWD})
# 0.0.4
VERSION=$(cat wpforms-epfl-payonline.php | grep '* Version:' | awk '{print $3}')
VERSION_NO_DOTS=$(echo $VERSION | sed -e 's/\.//g')
# [email protected]:epfl-si/wpforms-epfl-payonline.git
REPO_REMOTE=$(git config --get remote.origin.url)
# Nicolas Borboën
REPO_OWNER_NAME=$(git config --get user.name)
REPO_OWNER_EMAIL=$(git config --get user.email)
# epfl-si/wpforms-epfl-payonline
REPO_GH_PATH=$(echo $REPO_REMOTE | cut -d':' -f 2 | cut -d'.' -f 1)
# epfl-si
REPO_ORG_OR_USR=$(echo $REPO_GH_PATH | cut -d'/' -f1)
# wpforms-epfl-payonline
REPO_NAME=$(echo $REPO_GH_PATH | cut -d'/' -f2)
GH_URL=https://github.com/
GH_API=https://api.github.com
# https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
GH_ACCESS_TOKEN=$GH_ACCESS_TOKEN
# https://github.com/epfl-si/wpforms-epfl-payonline
REPO_HTTP_URL=$GH_URL$REPO_GH_PATH
# https://github.com/epfl-si/wpforms-epfl-payonline/releases
GH_RELEASE_URL=$GH_URL$REPO_ORG_OR_USR/$REPO_NAME/releases
GH_API_RELEASE_URL=$GH_API/repos/$REPO_ORG_OR_USR/$REPO_NAME/releases
# GET /repos/:owner/:repo/releases/tags/:tag
GH_RELEASE_CHECK_URL=$GH_API/repos/$REPO_ORG_OR_USR/$REPO_NAME/releases/tags/v${VERSION}
AUTH="Authorization: token $GH_ACCESS_TOKEN"
function printInfo() {
echo "--------------------------------------------------------------------------------"
echo "Version : $VERSION"
echo "Version no dots : $VERSION_NO_DOTS"
echo "Token : $GH_ACCESS_TOKEN"
echo "Remote : $REPO_REMOTE"
echo "Org : $REPO_ORG_OR_USR"
echo "Repo name : $REPO_NAME"
echo "Repo URL : $REPO_HTTP_URL"
echo "Project name : $PROJECT_NAME"
echo "Owner name : $REPO_OWNER_NAME"
echo "Owner email : $REPO_OWNER_EMAIL"
echo "--------------------------------------------------------------------------------"
}
# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
function validate_token () {
curl -o /dev/null -sH "$AUTH" $GH_API_RELEASE_URL || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
}
function create_tag () {
echo -e "\nchecking existing release..."
# check if tag already exists
# GET /repos/:owner/:repo/releases/tags/:tag
RELEASE_ID=$(curl -sH "$AUTH" $GH_RELEASE_CHECK_URL | jq -r .id)
if [ -n "$RELEASE_ID" -a "$RELEASE_ID" != 'null' ]
then
echo "... release ID: $RELEASE_ID already exists!"
return
else
echo "... no release found"
echo -e "\ncreating release..."
fi
RELEASE_JSON="{\
\"tag_name\":\"v${VERSION}\",\
\"target_commitish\":\"master\",\
\"name\":\"$(printf %q ${PROJECT_NAME}_v${VERSION})\",\
\"body\":\"[CHANGELOG.md](${REPO_HTTP_URL}/blob/master/CHANGELOG.md#v${VERSION_NO_DOTS})\",\
\"draft\":false,\
\"prerelease\":false\
}"
RELEASE=$(curl --data ${RELEASE_JSON} -sH "$AUTH" ${GH_API_RELEASE_URL})
echo $RELEASE
RELEASE_ID=$( echo $RELEASE | jq -r .id)
echo $RELEASE_ID
RELEASE_ERRORS=$( echo $RELEASE | jq -r .errors)
echo $RELEASE_ERRORS
if [ -n "$RELEASE_ERRORS" -a "$RELEASE_ERRORS" != 'null' ]
then
echo "Failed to create GitHub release. Exiting with errors: $RELEASE_ERRORS"
exit 1
fi
# exit script if GitHub release was not successfully created
if [ -z "$RELEASE_ID" ]
then
echo "Failed to create GitHub release. Exiting with error."
exit 1
else
echo -e " ... release created!\n"
echo -e "Release ID : $RELEASE_ID"
fi
}
function add_release_file () {
echo -e "\nchecking existing asset..."
# check if release already exists
# GET /repos/:owner/:repo/releases/:release_id/assets
ASSET_DATA=$(curl -sH "$AUTH" "https://api.github.com/repos/$REPO_ORG_OR_USR/$REPO_NAME/releases/$RELEASE_ID/assets")
ASSET_ID=$(echo $ASSET_DATA | jq -r '.[0].id')
echo "ASSET ID: $ASSET_ID"
if [ -n "$ASSET_ID" -a "$ASSET_ID" != 'null' ]
then
echo "... asset ID $ASSET_ID: already exists for this release!"
echo "... DELETING asset ID $ASSET_ID!"
ASSET_DELETE=$(curl -sH "$AUTH" -X DELETE "https://api.github.com/repos/$REPO_ORG_OR_USR/$REPO_NAME/releases/assets/$ASSET_ID")
echo $ASSET_DELETE
# return
else
echo "... no asset found"
echo -e "\ncreating asset..."
fi
FILENAME=./builds/${PROJECT_NAME}.zip
GH_ASSET="https://uploads.github.com/repos/$REPO_ORG_OR_USR/$REPO_NAME/releases/$RELEASE_ID/assets?name=$(basename $FILENAME)"
ASSET_INFO=$(curl -sH "$AUTH" --data-binary @"$FILENAME" -H "Content-Type: application/octet-stream" "$GH_ASSET")
ASSET_ERRORS=$( echo $ASSET_INFO | jq -r .errors)
ASSET_DWNLD_URL=$( echo $ASSET_INFO | jq -r .browser_download_url)
ASSET_ID=$( echo $ASSET_INFO | jq -r .id)
ASSET_NAME=$( echo $ASSET_INFO | jq -r .name)
ASSET_SIZE=$( echo $ASSET_INFO | jq -r .size)
if [ -n "$ASSET_ERRORS" -a "$ASSET_ERRORS" != 'null' ]
then
echo "Failed to create GitHub asset. Exiting with errors: $ASSET_ERRORS"
exit 1
fi
if [ -n "$ASSET_ID" -a "$ASSET_ID" != 'null' ]
then
echo -e " ... asset created !\n"
echo "Asset ID : $ASSET_ID"
echo "Asset name : $ASSET_NAME"
echo "Asset size : $ASSET_SIZE"
echo "Asset download : $ASSET_DWNLD_URL"
echo "View releases : $GH_RELEASE_URL"
echo "View release details : $GH_RELEASE_URL/tag/v$VERSION"
else
echo "Failed to create GitHub asset. Exiting with error."
exit 1
fi
}
printInfo
validate_token
create_tag
add_release_file