-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathemail-notification.sh
More file actions
executable file
·67 lines (51 loc) · 2.17 KB
/
email-notification.sh
File metadata and controls
executable file
·67 lines (51 loc) · 2.17 KB
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
#!/bin/bash
API=${API:-http://validator-api/api/v1}
PREFIX="Loop unprocessed testfiles"
if [[ -n $1 ]]; then
WSID=$1
echo "$PREFIX: check whether to send email notification for workspace $WSID"
# get the workspace info
HTTP_STATUS=$(curl -s "$API/iati-testworkspaces/$WSID" -o "./tmp_$WSID" -w "%{http_code}")
if [[ $HTTP_STATUS == 200 ]]; then
WS=`cat ./tmp_$WSID`
RECENT=`date -u --iso-8601=seconds -d "1 hour ago"`
# if there is an email address
# and all datasets have a JSON-updated field
# and last-email-notification does not exist
# or last-email-notification is more than an hour ago
# then send an email and update the last-email-notification timestamp
SEND=`echo $WS | jq '[ (has("email") and (.email!=null)), \
(.datasets | all(has("json-updated"))), \
( (has("last-email-notification") | not) \
or (.["last-email-notification"] < "$RECENT") )] | all'`
if [[ $SEND == true ]]; then
# get email, remove leadig and trailing quote
E1=`echo $WS | jq '.email'`
E2=${E1#\"}
EMAIL=${E2%\"}
cat <<EOF | msmtp $EMAIL
To: $EMAIL
From: iati-validator@data4development.nl
Subject: Your IATI validation results are ready
This is a message from the IATI Validator.
All files uploaded in your workspace have been validated.
Your personal workspace with validation results is here:
https://test-validator.iatistandard.org/validate/$WSID
Your personal workspace (including your email address) will be automatically deleted after 72 hours.
Thank you for your interest in the IATI Standard.
https://www.iatistandard.org
EOF
if [[ $? == 0 ]]; then
NOW=`date -u --iso-8601=seconds`
APIDATA="{\"last-email-notification\": \"$NOW\"}"
echo "$PREFIX: update last-email-notification for iati-testworkspace '$WSID' ($NOW) to indicate processing"
curl -sS -X PATCH --header 'Content-Type: application/json' --header 'Accept: application/json' \
-d "$APIDATA" \
"$API/iati-testworkspaces/$WSID"
echo "$PREFIX: email notification sent for workspace $WSID"
else
echo "$PREFIX: email notification failed for workspace $WSID"
fi
fi
fi
fi