2 # This script is to automate the process of monthly release with github API
4 # Test if getopt is enhanced version
5 getopt --test > /dev/null
7 echo "[ERROR] Your system doesn't have enhanced getopt"
13 echo "Usage: ./$(basename ${BASH_SOURCE[0]}) --tag TAG --release_note RELEASE_NOTE \
14 --token TOKEN [--release_name RELEASE_NAME] [--commitish COMMITISH] [--draft] \
15 [--host_name HOST_NAME] [--repo_owner REPO_OWNER] [--repo_name REPO_NAME] [--asset] ..."
18 echo "--tag The name of the tag"
19 echo "--release_name The name of the release"
20 echo "--release_note Path of text file describing the contents of the release"
21 echo "--commitish The commitish value that determines where the Git tag is created from"
22 echo "--draft Create a draft release"
23 echo "--token User token for authentication"
24 echo "--host_name Host name for endpoint URL [Enterprise-specific endpoint only]"
25 echo "--repo_owner Owner of the repository"
26 echo "--repo_name The name of the repository"
27 echo "--asset Path of release asset"
30 echo "$ ./git_release.sh --tag 1.9.0 --commitish release/1.9.0 --token 0de25f1ca5d1d758fe877b18c06 \\"
31 echo " --repo_owner mhs4670go --repo_name test_repo --release_note local/repo/release_note \\"
32 echo " --asset ONE-compiler.tar.gz --asset ONE-runtime.tar.gz"
34 echo "$ ./git_release.sh --tag v1.1 --commitish c024e85d0ce6cb1ed2fbc66f1a9c1c2814da7575 \\"
35 echo " --token 0de25f1ca5d1d758fe877b18c06 --repo_owner Samsung --repo_name ONE \\"
36 echo " --release_name \"Release Automation\" --release_note /home/mhs4670go/ONE/release_doc \\"
37 echo " --host_name github.sec.company.net --draft"
40 echo "https://developer.github.com/v3/repos/releases/#create-a-release"
58 OPTS=$(getopt --options "$SHORT_OPTS" --longoptions "$LONG_OPTS" --name "$0" -- "$@")
60 if [ $? != 0 ] ; then echo "[ERROR] Failed to parse options" ; exit 2 ; fi
67 unset TARGET_COMMITISH
89 --release_note ) # REQUIRED
106 HOST_NAME="$2/api/v3"
126 echo "[ERROR] getopt internal error"
132 # Check if required options are specified
133 if [ -z ${TAG_NAME} ]; then
134 echo "[ERROR] You must specify '--tag' option"
138 if [ -z ${RELEASE_NOTE} ]; then
139 echo "[ERROR] You must specify '--release_note' option"
143 if [ -z ${USER_TOKEN} ]; then
144 echo "[ERROR] You must specify '--token' option"
149 # Print variables and set default value
150 DEFAULT_RELEASE_NAME="ONE Release ${TAG_NAME}"
151 DEFAULT_HOST_NAME="api.github.com"
152 DEFAULT_REPO_OWNER="Samsung"
153 DEFAULT_REPO_NAME="ONE"
154 echo "======================[RELEASE INFO]======================"
155 echo "TAG_NAME : ${TAG_NAME}"
156 echo "RELEASE_NAME : ${RELEASE_NAME:=${DEFAULT_RELEASE_NAME}}"
157 echo "RELEASE_NOTE : ${RELEASE_NOTE}"
158 echo "TARGET_COMMITISH : ${TARGET_COMMITISH:=${TAG_NAME}}"
159 echo "IS_DRAFT : ${IS_DRAFT}"
160 echo "USER_TOKEN : ${USER_TOKEN}"
161 echo "HOST_NAME : ${HOST_NAME:=${DEFAULT_HOST_NAME}}"
162 echo "REPO_OWNER : ${REPO_OWNER:=${DEFAULT_REPO_OWNER}}"
163 echo "REPO_NAME : ${REPO_NAME:=${DEFAULT_REPO_NAME}}"
164 echo "ASSETS : ${ASSET_PATHS[@]}"
165 echo "==========================================================="
167 function generate_release_data()
171 "tag_name": "${TAG_NAME}",
172 "target_commitish": "${TARGET_COMMITISH}",
173 "name": "${RELEASE_NAME}",
174 "body": "$(cat $1 | sed 's/$/\\n/' | tr -d '\n')",
175 "draft": ${IS_DRAFT},
181 # Check if the release already exists
182 RELEASE_URL=$(curl -s --request GET --header "Authorization: token ${USER_TOKEN}" \
183 https://${HOST_NAME}/repos/${REPO_OWNER}/${REPO_NAME}/releases/tags/${TAG_NAME} | \
186 if [ $RELEASE_URL != null ]; then
187 echo "[ERROR] The tag name you specified already exists."
191 # Create a release (with assinging upload_url using jq)
192 UPLOAD_URL=$(curl -s --request POST --header "Authorization: token ${USER_TOKEN}" \
193 --header "Accept: application/json" \
194 --data "$(eval generate_release_data '${RELEASE_NOTE}')" \
195 "https://${HOST_NAME}/repos/${REPO_OWNER}/${REPO_NAME}/releases" | \
198 UPLOAD_URL=$(echo ${UPLOAD_URL} | cut -d "{" -f 1)?name=
201 for ASSET_PATH in "${ASSET_PATHS[@]}"; do
202 curl -s --request POST --header "Authorization: token ${USER_TOKEN}" \
203 --header "Content-Type: $(file -b --mime-type ${ASSET_PATH})" \
204 --data-binary @${ASSET_PATH} \
205 ${UPLOAD_URL}${ASSET_PATH} > /dev/null