-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub_notif
executable file
·228 lines (197 loc) · 7.79 KB
/
github_notif
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
#!/bin/bash
readonly DIR_NAME=$(dirname "$BASH_SOURCE")
readonly MAX_NUMBER_OF_MISSED_NOTIFICATIONS_TO_SHOW=2
readonly INSTANT_OF_THE_BIG_BANG=-1
readonly NOTIFY_ABOUT_FAILED_NOTIFICATIONS=1
KEEP_IN_SCREEN_TIME_IN_SECONDS=5
PATH=$PATH:/usr/local/bin
. "$DIR_NAME/lib/config_accessor.sh"
. "$DIR_NAME/lib/filter_manager.sh"
. "$DIR_NAME/lib/logger.sh"
. "$DIR_NAME/lib/url.sh"
. "$DIR_NAME/lib/notifier.sh"
. "$DIR_NAME/lib/remote_gateway.sh"
. "$DIR_NAME/lib/last_shown_info.sh"
function show_notification () {
local access_token=$1
local notifications_json=$2
local notification_index=$3
local config_name=$4;
eval "$(echo "$notifications_json" | jq ".[$notification_index]" |
jq -r '@sh "
latest_comment_url=\(.subject.latest_comment_url)
repo=\(.repository.name)
repo_full_name=\(.repository.full_name)
title=\(.subject.title)
general_url=\(.subject.url)
notification_type=\(.subject.type)
updated_at=\(.updated_at)
"')"
local call_result=$?
if [[ $call_result -ne 0 ]]; then
log_error "Failed to parse notifications list"
return 1
fi
local active_project_filters=($(get_active_project_filters $config_name))
local should_ignore_project=0
if [[ "${#active_project_filters[@]}" -lt "2" ]]; then
: #no project filters nothing to do
elif [[ "${active_project_filters[0]}" == "$INCLUDED" ]]; then
local found=0
for included_project in "${active_project_filters[@]:1}"; do
if [[ "${repo_full_name}" == "${included_project}" ]]; then
found=1;
break;
fi
done
[[ $found == 0 ]] && should_ignore_project=1
elif [[ "${active_project_filters[0]}" == "$EXCLUDED" ]]; then
for excluded_project in "${active_project_filters[@]:1}"; do
if [[ "${repo_full_name}" == "${excluded_project}" ]]; then
should_ignore_project=1;
break;
fi
done
else
log_error "${active_project_filters[@]}"
fi
if [[ $should_ignore_project == 1 ]]; then
#return success to ignore showing notification of the blacklisted project
return 0
fi
notification_type=$(echo "$notification_type" | sed 's|PullRequest|a PR|;s|Issue|an issue|;s|Commit|a commit|')
# sometimes latest_comment_url="null", for example when PR title is set
[[ "$latest_comment_url" == "null" ]] && notification_details_url="$general_url" || notification_details_url="$latest_comment_url"
local notification_details_json
notification_details_json=$(do_github_remote_call "$notification_details_url" "$access_token")
local call_result=$?
if [[ $call_result -ne 0 ]]; then
echo "$notification_details_json"
return 1
fi
eval "$(echo "$notification_details_json" | jq -r '@sh "
details_user=\(.user.login)
details_user_avatar_url=\(.user.avatar_url)
details_body=\(.body)
details_html_url=\(.html_url)
details_updated=\(.updated_at)
details_title=\(.title)
details_created_at=\(.created_at)
details_updated_at=\(.updated_at)
details_closed_at=\(.closed_at)
details_merged_at=\(.merged_at)
details_closed_by=\(.closed_by.login)
details_closed_by_avatar_url=\(.closed_by.avatar_url)
details_merged_by=\(.merged_by.login)
details_merged_by_avatar_url=\(.merged_by.avatar_url)
"')"
local user=$details_user
local avatar_url=$DIR_NAME/logo.png
[[ "$details_user_avatar_url" != "null" ]] && avatar_url=$details_user_avatar_url
echo "$notification_details_url" | grep -q 'comments'
local call_result=$?
if [[ $call_result -eq 0 ]]; then
event_action='commented on'
else
if [[ "$details_closed_by" != "null" && $(( $(date_to_epoch "$updated_at") - $(date_to_epoch "$details_closed_at") )) -lt 5 ]]; then
event_action="closed"
user=$details_closed_by
avatar_url=$details_closed_by_avatar_url
elif [[ "$details_merged_by" != "null" && $(( $(date_to_epoch "$updated_at") - $(date_to_epoch "$details_merged_at") )) -lt 5 ]]; then
event_action="merged"
user=$details_merged_by
avatar_url=$details_merged_by_avatar_url
elif [[ $(( $(date_to_epoch "$updated_at") - $(date_to_epoch "$details_created_at") )) -lt 5 ]]; then
event_action="created"
elif [[ $(( $(date_to_epoch "$updated_at") - $(date_to_epoch "$details_updated_at") )) -lt 5 ]]; then
event_action="updated" #TODO: distuinguish committed /pull/<id>/[commits,comments]
else
event_action='contributed on'
fi
fi
#TODO: may be no description provided instead
[[ $details_body != "" ]] && body="$details_body" || body="$details_title"
show_notification_window "${user//\"} ${event_action} $notification_type in ${repo//\"}" "${title//\"}" "${body//\"}" "${details_html_url//\"}" "${avatar_url}"
}
function date_to_epoch() {
local input_date=$1
if [[ $(echo "$input_date" | egrep '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z') == "" ]]; then
echo "0"
else
echo $(date -u -j -f "%Y-%m-%dT%TZ" +"%s" "$input_date")
fi
}
function show_all_notifications () {
config_url=$1
show_notification_window "More missed notifications on $(get_resource_name "$config_url")" "See all" "" "$config_url/notifications"
}
function show_notification_about_failure () {
config_url=$1
error_message=$2
show_notification_window "Failed to fetch a notification" "$error_message" "" "$config_url/notifications"
}
function show_missed_notifications() {
local config_url=$1
local access_token=$2
local notifications_json=$3
local shown_date=$4
local latest_commit_dates=( $(echo "$notifications_json" | jq '.[0,1,2].updated_at' | \
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z' | xargs -n 1 date -u -j -f "%Y-%m-%dT%TZ" +"%s") )
for ((i = 0; i < $MAX_NUMBER_OF_MISSED_NOTIFICATIONS_TO_SHOW; i++)); do
if (( $shown_date < ${latest_commit_dates[$i]:-INSTANT_OF_THE_BIG_BANG} )); then
error_message=$(show_notification "$access_token" "$notifications_json" $i "$config_url")
local call_result=$?
if [[ $call_result -ne 0 ]]; then
if (( $NOTIFY_ABOUT_FAILED_NOTIFICATIONS == 1 )); then
show_notification_about_failure "$config_url" "$error_message"
fi;
fi
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
else
break;
fi
done
if (( $shown_date < ${latest_commit_dates[2]:-INSTANT_OF_THE_BIG_BANG} )); then
show_all_notifications "$config_url"
fi
(( $shown_date > ${latest_commit_dates[0]:-INSTANT_OF_THE_BIG_BANG} )) && echo "$shown_date" || echo "${latest_commit_dates[0]}"
return $exit_code
}
function main() {
exit_code=0
local active_configs=$(get_active_configs)
if [ -z "$active_configs" ]; then
echo "There is no any active configuration to get notifications"
return 1
fi
for config_name in $active_configs ; do
local last_shown_commit_date=$(get_last_shown_commit_date "$config_name")
local notifications_json
access_token=$(get_token "$config_name")
local notifications_api_url
if [[ "$config_name" == "https://github.com" ]]; then
notifications_api_url="${config_name/https:\/\//https://api.}"/notifications
else
notifications_api_url="${config_name}/api/v3/notifications"
fi
notifications_json=$(do_github_remote_call "$notifications_api_url" "$access_token")
local call_result=$?
if [[ $call_result -ne 0 ]]; then
log_error "$notifications_json"
exit_code=1
continue
fi
local updated_last_shown_commit_date
updated_last_shown_commit_date=$(show_missed_notifications "$config_name" "$access_token" "$notifications_json" "$last_shown_commit_date")
if [[ "$updated_last_shown_commit_date" != "$last_shown_commit_date" ]]; then
save_last_shown_commit_date "$config_name" "$updated_last_shown_commit_date"
local call_result=$?
if [[ $call_result -ne 0 ]]; then
log_error "Cannot save last shown commit date. $updated_last_shown_commit_date"
exit_code=1
fi
fi
done
return $exit_code
}
main