Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / tools / release_tool / git_release.sh
1 #!/bin/bash
2 # This script is to automate the process of monthly release with github API
3
4 # Test if getopt is enhanced version
5 getopt --test > /dev/null
6 if [ $? -ne 4 ]; then
7   echo "[ERROR] Your system doesn't have enhanced getopt"
8   echo 2
9 fi
10
11 function Usage()
12 {
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] ..."
16   echo ""
17   echo "[OPTIONS]"
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"
28   echo ""
29   echo "[EXAMPLE]"
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"
33   echo ""
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"
38   echo ""
39   echo "[REFERENCE]"
40   echo "https://developer.github.com/v3/repos/releases/#create-a-release"
41
42 }
43
44 SHORT_OPTS=h
45 LONG_OPTS="\
46 help,\
47 tag:,\
48 release_name:,\
49 release_note:,\
50 commitish:,\
51 draft,\
52 token:,\
53 host_name:,\
54 repo_owner:,\
55 repo_name:,\
56 asset:"
57
58 OPTS=$(getopt --options "$SHORT_OPTS" --longoptions "$LONG_OPTS" --name "$0" -- "$@")
59
60 if [ $? != 0 ] ; then echo "[ERROR] Failed to parse options" ; exit 2 ; fi
61
62 eval set -- "$OPTS"
63
64 unset TAG_NAME
65 unset RELEASE_NAME
66 unset RELEASE_NOTE
67 unset TARGET_COMMITISH
68 unset USER_TOKEN
69 unset HOST_NAME
70 unset REPO_OWNER
71 unset REPO_NAME
72 IS_DRAFT=false
73 ASSET_PATHS=()
74
75 while true ; do
76   case "$1" in
77     -h|--help )
78       Usage
79       exit 0
80       ;;
81     --tag ) # REQUIRED
82       TAG_NAME="$2"
83       shift 2
84       ;;
85     --release_name )
86       RELEASE_NAME="$2"
87       shift 2
88       ;;
89     --release_note ) # REQUIRED
90       RELEASE_NOTE="$2"
91       shift 2
92       ;;
93     --commitish )
94       TARGET_COMMITISH="$2"
95       shift 2
96       ;;
97     --draft )
98       IS_DRAFT=true
99       shift
100       ;;
101     --token ) # REQUIRED
102       USER_TOKEN="$2"
103       shift 2
104       ;;
105     --host_name )
106       HOST_NAME="$2/api/v3"
107       shift 2
108       ;;
109     --repo_owner )
110       REPO_OWNER="$2"
111       shift 2
112       ;;
113     --repo_name )
114       REPO_NAME="$2"
115       shift 2
116       ;;
117     --asset )
118       ASSET_PATHS+=("$2")
119       shift 2
120       ;;
121     -- )
122       shift
123       break
124       ;;
125     *)
126       echo "[ERROR] getopt internal error"
127       exit 2
128       ;;
129   esac
130 done
131
132 # Check if required options are specified
133 if [ -z ${TAG_NAME} ]; then
134   echo "[ERROR] You must specify '--tag' option"
135   Usage
136   exit 0
137 fi
138 if [ -z ${RELEASE_NOTE} ]; then
139   echo "[ERROR] You must specify '--release_note' option"
140   Usage
141   exit 0
142 fi
143 if [ -z ${USER_TOKEN} ]; then
144   echo "[ERROR] You must specify '--token' option"
145   Usage
146   exit 0
147 fi
148
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 "==========================================================="
166
167 function generate_release_data()
168 {
169   cat <<EOF
170 {
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},
176   "prerelease": false
177 }
178 EOF
179 }
180
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} | \
184 jq -r '.url')
185
186 if [ $RELEASE_URL != null ]; then
187   echo "[ERROR] The tag name you specified already exists."
188   exit 2
189 fi
190
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" | \
196 jq -r '.upload_url')
197
198 UPLOAD_URL=$(echo ${UPLOAD_URL} | cut -d "{" -f 1)?name=
199
200 # Upload the assets
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
206 done