forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-do-notify
executable file
·254 lines (193 loc) · 9.68 KB
/
adpn-do-notify
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
#
# adpn-do-notify: As part of the ingest process, accept some pipelined JSON data
# from stdin, and then drop it into an Issue on the Gitlab repository.
#
# Usage: <INPUT> | adpn-do-notify --subject="<SUBJECT_LINE>"
#
# @version 2021.0707
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$(readlink -f "${SCRIPTPATH}")"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPTNAME="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
##########################################################################################
### COMMAND LINE: loop through switches and sort them to subsidiary scripts ##############
##########################################################################################
declare -a _CMDLINE ; _CMDLINE=("$0")
shopt -s lastpipe
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### PARAMETERS: GET PARAMETER VALUES FROM COMMAND LINE AND JSON INPUT ####################
##########################################################################################
EXITCODE=0
CMD=""
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
declare -a CONTEXT_SW=( "--context=${SCRIPT_CMD_NAME}" )
[[ -n "${DDBG}" ]] && CONTEXT_SW+=( "${DDBG}" )
PIPED_TEXT="$( cat "${_ARGV[@]:1}" )"
JSON="$( printf "%s" "${PIPED_TEXT}" | adpn-json.py --output=application/json --quiet )" || JSON="{}"
PRETTY_JSON="$( printf "%s" "${JSON}" | adpn-json.py --output=application/json --indent=2 )"
INGEST_STEP="$( adpn_parameter_from "--step" ".Ingest Step<${JSON}" "output" )"
STAGED_TO=$( adpn_parameter_from "--from" ".Staged To<${JSON}" "\\${USER_FULLMAIL}")
USER_EMAIL=$( adpn_parameter_from ".user/email" )
USER_REALNAME=$( adpn_parameter_from ".user/realname" )
USER_FULLMAIL=$( printf "%s <%s>" "${USER_REALNAME}" "${USER_EMAIL}" )
USER_NAME=$( adpn_parameter_from ".Staged By<${JSON}" "\\${USER_FULLMAIL}" )
STAGED_BY=$( adpn_parameter_from "--by" ".Staged By<${JSON}" "\\${USER_FULLMAIL}")
ACCEPTED_BY=$( adpn_parameter_from "--by" ".Accepted By<${JSON}" ".Staged By<${JSON}" "\\${USER_FULLMAIL}" )
PROMOTED_BY=$( adpn_parameter_from "--by" ".Promoted By<${JSON}" ".Accepted By<${JSON}" ".Staged By<${JSON}" "\\${USER_FULLMAIL}")
ADMIN_EMAIL=$( adpn_parameter_from "--admin-email" ".preserve/admin-email" )
GITLAB_ISSUE=$( adpn_parameter_from "--gitlab" ".Gitlab Issue<${JSON}" )
GITLAB_RESOURCE=$( adpn_parameter_from "--gitlab-api" ".Gitlab Resource<${JSON}" )
DEFAULT_REPOSITORY=$( adpn_parameter_from ".gitlab/repository" )
EXTENT=$( adpn_parameter_from ".File Size<${JSON}" )
LOCATION=$( adpn_parameter_from ".Staged To<${JSON}" )
SUBDIRECTORY=$( adpn_parameter_from "--subdirectory" "--directory" )
AU_START_URL="$( adpn_parameter_from ".Start URL<${JSON}" )"
AU_TITLE="$( adpn_parameter_from ".Ingest Title<${JSON}" )"
LABELS="$( adpn_parameter_from "--labels" "Content Ingest" )"
declare -A STEP_TEMPLATE=(
[staged]=notify
[published]=announce
[unstaged]=unstage
[output]=output
[default]=notify
)
declare -A TEMPLATE_PARAMS=(
[cmd]="${SCRIPT_CMD_NAME}"
[post_cmd]="${SCRIPT_CMD_NAME}"
[staged_to]="${STAGED_TO}"
[staged_by]="${STAGED_BY}"
[accepted_by]="${ACCEPTED_BY}"
[promoted_by]="${PROMOTED_BY}"
[admin_email]="${ADMIN_EMAIL}"
[gitlab_issue]="${GITLAB_ISSUE}"
[timestamp]="$( date )"
[user_name]="${USER_NAME}"
[extent]="${EXTENT}"
[location]="${LOCATION}"
[subdirectory]="${SUBDIRECTORY}"
[au_start_url]="${AU_START_URL}"
[au_title]="${AU_TITLE}"
[piped_text]="${PIPED_TEXT}"
[json]="${JSON}"
[pretty_json]="${PRETTY_JSON}"
)
for SW in "${!_PARAM[@]}" ; do
if [[ "${SW}" =~ ^template:(.*)$ ]] ; then
TEMPLATE_PARAMS["${BASH_REMATCH[1]}"]="${_PARAM[${SW}]}"
fi
done
###############################################################################
## FORMAT TEMPLATE TEXT #######################################################
###############################################################################
declare -a TEMPLATE_SOURCES=( "--template" )
[[ -n "${STEP_TEMPLATE[$INGEST_STEP]}" ]] && TEMPLATE_SOURCES+=( "\\${STEP_TEMPLATE[$INGEST_STEP]}" )
[[ -n "${STEP_TEMPLATE[default]}" ]] && TEMPLATE_SOURCES+=( "\\${STEP_TEMPLATE[default]}" )
TEMPLATE_SLUG="$( adpn_parameter_from "${TEMPLATE_SOURCES[@]}" )"
TEMPLATE_FILE="${SCRIPTDIR}/templates/adpn.${TEMPLATE_SLUG}.md.sprintf"
if [[ -r "${TEMPLATE_FILE}" ]] ; then
TEMPLATE_TEXT="$( cat "${TEMPLATE_FILE}" )"
else
IFS='' read -r -d '' TEMPLATE_TEXT <<EOText
Add AU from %(staged_to)s: %(subdirectory)s - %(au_title)s
==========================================================
%(user_name)s used the \`adpn\` CLI tool to upload files for an ADPNet ingest to the `%(location)s` staging area, directory: \`%(subdirectory)s\`.
AU meta-data output by \`%(cmd)s\` follows:
~~~
${PIPED_TEXT}
~~~
EOText
fi
for KEY in "${!TEMPLATE_PARAMS[@]}" ; do
SEARCH="%(${KEY})s"
REPLACE="${TEMPLATE_PARAMS[$KEY]}"
TEMPLATE_TEXT="${TEMPLATE_TEXT//${SEARCH}/${REPLACE}}"
done
declare -a BODY_TEXT_SECTIONS=( )
SECT="$( printf "%s" "${TEMPLATE_TEXT}" | sed -E -n '0,/^\s*=+\s*$/p' )"
if [[ -n "${SECT}" ]] ; then
BODY_TEXT_SECTIONS+=( "${SECT}" )
fi
SECT="$( printf "%s" "${TEMPLATE_TEXT}" | sed -E -n '/^\s*=+\s*$/,$p' )"
if [[ -n "${SECT}" ]] ; then
BODY_TEXT_SECTIONS+=( "${SECT}" )
fi
if [[ "${#BODY_TEXT_SECTIONS[@]}" -gt 1 ]] ; then
SUBJECT_TEXT="$( printf "%s" "${BODY_TEXT_SECTIONS[0]}" | head -n 1 )"
BODY_TEXT="$( printf "%s" "${BODY_TEXT_SECTIONS[1]}" | tail -n +2 )"
else
SUBJECT_TEXT="$( adpn_parameter_from "--subject" "ADPNet Ingest" )"
BODY_TEXT="${BODY_TEXT_SECTIONS[0]}"
fi
###############################################################################
## EXECUTE ADPN-GITLAB-DO SCRIPT ##############################################
###############################################################################
ADPN_GITLAB_POST_JSON=$(mktemp)
adpn_debug 50,adpn,notify,gitlab,output,"${INGEST_STEP}" "${LINENO}" "Step: '%s'; Resource: '%s'" "${INGEST_STEP}" "${GITLAB_RESOURCE}"
if [[ "staged" == "${INGEST_STEP}" ]] ; then
printf "%s" "${BODY_TEXT}" | adpn-gitlab-do issue --post --title="${SUBJECT_TEXT}" --labels="${LABELS}" --pipe=description --project="${DEFAULT_REPOSITORY}" --output="application/json" ${V} ${DDBG} > "${ADPN_GITLAB_POST_JSON}" ; ADGIP_EXITCODE="$?"
if [[ "${ADGIP_EXITCODE}" -eq 0 ]] ; then
adpn_debug 10,adpn,notify,gitlab "${LINENO}" "adpn-gitlab-do result: %s" "$( cat "${ADPN_GITLAB_POST_JSON}" )"
ISSUE_ID="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key="iid" )"
ISSUE_URL="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key="web_url" )"
adpn_notice "${LINENO}" "OK: Issue # %d, URL: <%s>" "${ISSUE_ID}" "${ISSUE_URL}"
else
adpn_notice "${LINENO}" "ERROR: Failed to post new issue to Gitlab [%s]" "${DEFAULT_REPOSITORY}"
adpn_notice "${LINENO}" "JSON: <<<EOF\n%s\nEOF;" "$( cat "${ADPN_GITLAB_POST_JSON}" )"
fi
EXITCODE="${ADGIP_EXITCODE}"
elif [[ "output" == "${INGEST_STEP}" && -n "${GITLAB_RESOURCE}" ]] ; then
declare -a _LABEL_SW=()
if [[ -n "${_PARAM[remove_labels]}" ]] ; then
_LABEL_SW+=( "$( printf -- '--remove_labels=%s' "${_PARAM[remove_labels]}" )" )
fi
if [[ -n "${_PARAM[add_labels]}" ]] ; then
_LABEL_SW+=( "$( printf -- '--add_labels=%s' "${_PARAM[add_labels]}" )" )
fi
declare -a ADG_POST_NOTE=( "adpn-gitlab-do" "post" "note" "${CONTEXT_SW[@]}" "${GITLAB_RESOURCE}" "${_LABEL_SW[@]}" )
DBG_BODY="$( printf "%s" "${BODY_TEXT}" | head -c 10 )"
[[ "${#BODY_TEXT}" -gt "${#DBG_BODY}" ]] && DBG_BODY+="..."
adpn_debug 10,adpn,notify,gitlab,output "${LINENO}" "printf '%%s' '%s' | %s" "${DBG_BODY}" "${ADG_POST_NOTE[*]}"
printf "%s" "${BODY_TEXT}" | "${ADG_POST_NOTE[@]}" > "${ADPN_GITLAB_POST_JSON}"
EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
adpn_debug 50,adpn,notify,gitlab,output "${LINENO}" "Gitlab POST, JSON Response: %s" "$( cat "${ADPN_GITLAB_POST_JSON}" )"
GL_ISSUE_ID="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=noteable_iid )" ; EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
GL_NOTE_ID="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=id )"
GL_CREATED_AT="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=created_at )"
GL_AUTHOR_USERNAME="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=author | grep -E '^username\s' | cut -s --fields=2- )"
GL_AUTHOR_REALNAME="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=author | grep -E '^name\s' | cut -s --fields=2- )"
[[ -z "${GL_AUTHOR_REALNAME}" ]] && GL_AUTHOR_REALNAME="${GL_AUTHOR_USERNAME}"
[[ -n "${GL_AUTHOR_USERNAME}" ]] && [[ "${GL_AUTHOR_REALNAME}" != "${GL_AUTHOR_USERNAME}" ]] && GL_AUTHOR_REALNAME="${GL_AUTHOR_REALNAME} (@${GL_AUTHOR_USERNAME})"
GL_NOTE_URL="${GITLAB_ISSUE}"
[[ -n "${GL_NOTE_ID}" ]] && GL_NOTE_URL="$( printf "%s#note_%s" "${GL_NOTE_URL}" "${GL_NOTE_ID}" )"
adpn_notice "${LINENO}" "OK: Issue # %d, Note By %s at %s, URL: <%s>" "${GL_ISSUE_ID}" "${GL_AUTHOR_REALNAME}" "${GL_CREATED_AT}" "${GL_NOTE_URL}"
else
GL_ERROR="$( cat "${ADPN_GITLAB_POST_JSON}" | adpn-json.py --key=error_description )"
adpn_notice "${LINENO}" "ERROR: Failed to post new note to Gitlab Issue <%s>. API responded: '%s'" "${GITLAB_ISSUE}" "${GL_ERROR}"
fi
else
adpn_notice "${LINENO}" "ERROR: Failed to post new note to Gitlab Issue <%s>" "${GITLAB_ISSUE}"
adpn_notice "${LINENO}" "JSON: <<<EOF\n%s\nEOF;" "$( cat "${ADPN_GITLAB_POST_JSON}" )"
fi
else
printf "%s\n" "${BODY_TEXT}"
EXITCODE=0
fi
rm "${ADPN_GITLAB_POST_JSON}"
exit "${EXITCODE}"