-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
217 lines (184 loc) · 5.64 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
#################################################################################
# Slack Task notifier
# $ bash ./slack-new-task.sh TASK [EXTRA_MESSAGE]
#
# And please follow this format in case you don't have JIRA integration
# $ bash ./slack-new-task.sh TYPE TASK TASK_SUMMARY [EXTRA_MESSAGE]
#################################################################################
# Please edit params in files
# - params-base.sh
# - params-base-{JIRA_KEY}.sh
#################################################################################
set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
readonly __dir __file
cd ${__dir}
#################################################################################
# Code
#################################################################################
check_error () {
local status=${1}
shift
if [ '0' != "${status}" ]; then
echo -e "error: $@" > /dev/stderr
exit ${status}
fi
}
check_php() {
local bin php_zip_url
bin=${1}
if test ! -f "${1}" && ! type "${1}" 2>&1 > /dev/null; then
echo 'Downloading PHP binaries...'
php_zip_url='http://windows.php.net/downloads/'$(curl -s http://windows.php.net/download/ | grep -o 'releases/php-.*-x86.zip' | head -1 2>&1)
mkdir $(dirname ${bin}) -p
curl -s ${php_zip_url} --output $(dirname ${bin})/php_downloaded.zip
unzip $(dirname ${bin})/php_downloaded.zip -d $(dirname ${bin}) > /dev/null
echo 'Good. PHP binaries have been downloaded.'
# Set up php.ini file
rm -rf $(dirname ${bin})/php.ini
cp $(dirname ${bin})/php.ini-production $(dirname ${bin})/php.ini
cat ${__dir}/php-default.ini >> $(dirname ${bin})/php.ini
fi
if ! ${bin} -i | grep curl > /dev/null; then
check_error 2 'PHP does not have enabled cURL module.'
fi
}
# task_field FIELD_KEY
task_field() {
${php_bin} -f ./read-task.php "${JIRA_USERNAME}:${JIRA_PASSWORD}" "${JIRA_URL}" "${task_key}" ${1}
}
escape_quotes_for_post () {
echo $@ | sed -r 's|"|\\\x22|g'
}
# Validate parameters
if test -z "${1:-}" || [[ ! "${1}" =~ [A-Z][A-Z0-9]*-[0-9]+ ]]; then
check_error 3 'Set JIRA key as first argument.'
fi
task_no=${1}; shift
if test $(echo "${task_no}" | grep '-'); then
jira_project=$(echo ${task_no} | tr '-' ' ' | awk '{print $1}')
task_no=$(echo ${task_no} | tr '-' ' ' | awk '{print $2}')
else
check_error 3 'Set full JIRA key.'
fi
if [ -z "${php_bin:-}" ]; then
php_bin=${__dir}'/php/php.exe'
if type php 2>&1 > /dev/null; then
# define global PHP
php_bin='php'
fi
fi
check_php ${php_bin}
. ./params.sh
task_sprint=''
task_assignee=''
task_reporter=${APP_MESSAGE_USER}
task_key="${APP_JIRA_PROJECT}-${task_no}"
slack_footer_icon=${APP_SLACK_FOOTER_ICON_URL}
if [ -n "${JIRA_USERNAME}" ] && [ -n "${JIRA_PASSWORD}" ]; then
printf 'Requesting JIRA issue...'
##
# Debug JIRA issue schema
# ${php_bin} -f ./read-task.php "${JIRA_USERNAME}:${JIRA_PASSWORD}" "${JIRA_URL}" "${task_key}"; exit 55
task_type=$(task_field 'issuetype')
if [ -z "${task_type}" ]; then
echo 'FAILED'
check_error 4 'Cannot load issue.'
fi
task_sprint=$(task_field 'sprint')
task_assignee=$(task_field 'assignee')
task_reporter=$(task_field 'reporter')
if [ -z "${task_sprint}" ]; then
task_sprint='Backlog'
fi
task_summary=$(task_field 'summary')
echo 'OK'
else
echo 'notice: JIRA issue cannot be requested.'
task_type=${1}; shift
task_summary=${1}; shift
fi
slack_text=${1:-}
if [ -n "${APP_MESSAGE_CC:-}" ]; then
slack_text="\n"${APP_MESSAGE_CC}
fi
task_url=${JIRA_URL}'/browse/'${APP_JIRA_PROJECT}'-'${task_no}
declare -A issue_color=(
['Bug']='#E11'
['Story']='#EE1'
['Task']='#11E'
['Question']='#69F5FF'
['Issue']='#911'
['Change Request']='#40ff00'
['SubTask']='#CCC'
['Sub-Task Task']='#CCC'
)
if [ -n "${issue_color[${task_type}]:-}" ]; then
attach_color=${issue_color[${task_type}]}
else
echo 'error: Unknown issue type: '${task_type}
echo 'note: Please use one of following: Bug, Story, Task, SubTask, Issue, Question'
exit 3
fi
slack_footer_text=$(
escape_quotes_for_post \
"$(printf "${APP_FOOTER_TEXT:-'JIRA %s. Posted by %s'}" \
"${task_type}" \
"${APP_MESSAGE_USER}"
)"
)
# disable parsing asterisks, or set -f
set -o noglob
slack_format='payload={
"username": "JIRA",
"channel": "'${APP_SLACK_CHANNEL}'",
"text": "'$(escape_quotes_for_post "${slack_text}")'",
"as_user": "true",
"link_names": "true",
"icon_emoji": "'${APP_SLACK_ICON_EMOJI}'",
"attachments": [
{
"fallback": "",
"color": "'${attach_color}'",
"pretext": "",
"author_name": "Reporter: '${task_reporter}' Assignee: '${task_assignee}'",
"title": "'${task_key}'",
"title_link": "'${task_url}'",
"text": "'$(escape_quotes_for_post "${task_summary}")'",
"fields": [
{
"title": "Type",
"value": "'$(escape_quotes_for_post "${task_type}")'",
"short": true
},
{
"title": "Sprint",
"value": "'$(escape_quotes_for_post "${task_sprint}")'",
"short": true
}
],
"image_url": "",
"thumb_url": "",
"footer": "'${slack_footer_text}'",
"footer_icon": "'${slack_footer_icon}'",
"ts": '$(date +%s)'
}
]
}'
set +o noglob
printf 'Posting the message to slack...'
result=$(curl -s --data-urlencode \
"${slack_format}" \
${APP_SLACK_WEBHOOK})
if [ 'ok' != "${result}" ]; then
echo 'FAILED'
check_error 2 'error: Cannot post message.'"\n"'Response: '
else
echo 'OK'
fi