-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
create-github-release.sh
155 lines (144 loc) · 5.6 KB
/
create-github-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
#!/usr/bin/env bash
# Creates the release with the latest tag if any release does not already exists
# Example usage:
# bash "create-github-release.sh" \
# --repository "undergroundwires/privacy.sexy" \
# --type "draft" \
# --token "YOUR_SECRET_PAT"
# Prerequisites:
# - Ensure your current folder is the repository root & git is installed
# - Ensure all tags are fetched
# Dependencies:
# - External: git, curl, jq
# - Local: ./shared/log-commits.sh, ./shared/utilities.sh
# Globals
readonly SCRIPTS_DIRECTORY=$(dirname "$0")
readonly LOG_COMMITS_SCRIPT_PATH="$SCRIPTS_DIRECTORY/shared/log-commits.sh"
# Import dependencies
# shellcheck source=shared/utilities.sh
source "$SCRIPTS_DIRECTORY/shared/utilities.sh"
release_exists() {
local -r version="$1" repository="$2" access_token="$3"
local -r api_url="https://api.github.com/repos/$repository/releases/tags/$version"
local -i status_code;
if ! status_code=$(curl \
--write-out '%{http_code}' \
--silent \
--output /dev/null \
--header "Authorization: token $access_token" \
"$api_url"); then
echo "Request to check if release exists has failed"
exit 1;
fi
echo "$status_code from $api_url"
if [[ "$status_code" -eq 200 ]] ; then
echo "Skipping creating a release as a release already exists for \"$version\""
return 0
elif [[ "$status_code" -eq 401 ]] ; then
echo "401 Unauthorized: Is release access token valid?"
return 0
elif [[ "$status_code" -eq 404 ]] ; then
echo "404 Not Found: \"$version\" is not yet released"
return 1
else
echo "Unexpected status code: $status_code"
exit 1;
fi
}
print_release_notes() {
local -r version="$1" repository="$2"
if utilities::has_single_version; then
echo "Initial release"
return 0
fi
local version_before
if ! version_before=$(utilities::print_previous_version); then
echo "Could not get the previous version. $version_before"
exit 1
fi
local changes
if ! changes=$(bash "$LOG_COMMITS_SCRIPT_PATH" \
--repository "$repository" \
--current "$version" \
--previous "$version_before"); then
echo "$LOG_COMMITS_SCRIPT_PATH has failed"
exit 1;
fi
if ! utilities::is_empty_or_null "$changes"; then
printf "%s\n\n" "$changes"
fi
printf "[compare](https://github.com/%s/compare/%s...%s)" \
"$repository" "$version_before" "$version"
}
create_release() {
local -r version="$1" repository="$2" access_token="$3" release_type="$4"
echo "Creating a new release for $version"
local changelog
if ! changelog=$(print_release_notes "$version" "$repository"); then
echo "print_release_notes has failed"
exit 1;
fi
local json_payload # https://developer.github.com/v3/repos/releases/#create-a-release
if ! json_payload=$(jq -n \
--arg version "$version" \
--arg body "$changelog" \
'{ tag_name: $version, name: $version, body: $body }'); then
echo "jq has failed"
exit 1;
fi
if is_release_type_draft "$release_type"; then
json_payload=$(echo "$json_payload" | jq --argjson value true '. + {draft: $value}')
fi
if is_release_type_prerelease "$release_type"; then
json_payload=$(echo "$json_payload" | jq --argjson value true '. + {prerelease: $value}')
fi
curl --header "Authorization: token $access_token" \
"https://api.github.com/repos/$repository/releases" \
--data "$json_payload" \
--silent \
--fail
}
is_release_type_draft() { utilities::equals_case_insensitive "$1" "draft"; }
is_release_type_none() { utilities::equals_case_insensitive "$1" "none"; }
is_release_type_prerelease() { utilities::equals_case_insensitive "$1" "prerelease"; }
is_release_type_release() { utilities::equals_case_insensitive "$1" "release"; }
validate_parameters() {
local repository="$1" access_token="$2" release_type="$3"
if utilities::is_empty_or_null "$repository"; then echo "Repository name is not set."; exit 1; fi;
if utilities::is_empty_or_null "$release_type"; then echo "Release type is not set."; exit 1; fi;
if ! (is_release_type_draft "$release_type" \
|| is_release_type_none "$release_type" \
|| is_release_type_prerelease "$release_type" \
|| is_release_type_release "$release_type"); then
echo "Unkown release type: \"$release_type\".";
exit 1;
fi;
if (! is_release_type_none "$release_type") \
&& utilities::is_empty_or_null "$access_token"; then
echo "Access token is not set.";
exit 1;
fi;
}
main() {
local -r repository="$1" access_token="$2" release_type="$3"
validate_parameters "$repository" "$access_token" "$release_type"
if is_release_type_none "$release_type"; then
echo "Skipping release as release type is set to \"$release_type\""
exit 0;
fi;
local latest_version
if ! latest_version=$(utilities::print_latest_version); then
echo "Could not get the latest version. $latest_version"
exit 1;
fi
if ! release_exists "$latest_version" "$repository" "$access_token"; then
create_release "$latest_version" "$repository" "$access_token" "$release_type"
fi
}
while [[ "$#" -gt 0 ]]; do case $1 in
--repository) REPOSITORY="$2"; shift;;
--token) ACCESS_TOKEN="$2"; shift;;
--type) RELEASE_TYPE="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
main "$REPOSITORY" "$ACCESS_TOKEN" "$RELEASE_TYPE"