Skip to content

Commit

Permalink
Merge pull request #20 from sargsyan/addresses-perpetually-repeating-…
Browse files Browse the repository at this point in the history
…notifications

Addresses perpetually repeating notifications
  • Loading branch information
sargsyan authored Feb 15, 2020
2 parents c8fa04a + bdc3446 commit 6001610
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
35 changes: 24 additions & 11 deletions github_notif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

Expand Down Expand Up @@ -109,6 +111,12 @@ function show_all_notifications () {
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
Expand All @@ -119,26 +127,30 @@ function show_missed_notifications() {
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]:--1} )); then
if (( $shown_date < ${latest_commit_dates[$i]:-INSTANT_OF_THE_BIG_BANG} )); then
if ((i != 0)); then
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
fi

error_message=$(show_notification "$access_token" "$notifications_json" $i)
local call_result=$?
if [[ $call_result -ne 0 ]]; then
log_error "An error occured while showing notification. $error_message"
exit_code=1
else
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
if (( $NOTIFY_ABOUT_FAILED_NOTIFICATIONS == 1 )); then
show_notification_about_failure "$config_url" "$error_message"
fi;
fi
else
break;
fi
done

if (( $shown_date < ${latest_commit_dates[2]:--1} )); then
if (( $shown_date < ${latest_commit_dates[2]:-INSTANT_OF_THE_BIG_BANG} )); then
show_all_notifications "$config_url"
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
fi

(( $exit_code == 0 && $shown_date > ${latest_commit_dates[0]:--1} )) && echo "$shown_date" || echo "${latest_commit_dates[0]}"
(( $shown_date > ${latest_commit_dates[0]:-INSTANT_OF_THE_BIG_BANG} )) && echo "$shown_date" || echo "${latest_commit_dates[0]}"
return $exit_code
}

Expand Down Expand Up @@ -172,12 +184,13 @@ function main() {

local updated_last_shown_commit_date
updated_last_shown_commit_date=$(show_missed_notifications "$config_name" "$access_token" "$notifications_json" "$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
else
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

Expand Down
8 changes: 8 additions & 0 deletions lib/last_shown_info.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/bin/bash

readonly CURR_DIR_NAME=$(dirname "$BASH_SOURCE")
readonly LATEST_SHOWN_FILE="$HOME/.latest_shown"

. "$CURR_DIR_NAME/logger.sh"

function get_last_shown_commit_date() {
local config_name=$1

Expand All @@ -16,6 +19,11 @@ function save_last_shown_commit_date() {
local config_name=$1
local value=$2
if [ -f "$LATEST_SHOWN_FILE" ]; then
if [ ! -w "$LATEST_SHOWN_FILE" ]; then
# TODO: consider notifying (with exponential backup) if this really happens
log_error "$LATEST_SHOWN_FILE is not writable for the current user"
return 1
fi
local line_number=$(grep -wn "$config_name" "$LATEST_SHOWN_FILE" | cut -d: -f1)
if [[ -n "$line_number" ]]; then
sed -i .bak "${line_number}s|.*|$config_name $value|" "$LATEST_SHOWN_FILE" &&
Expand Down
4 changes: 2 additions & 2 deletions test/github_notif_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ test_show_missed_notifications_on_failing_notification_details_call() {
alias do_github_remote_call=mock_failing_notification_details_call
local shown_date=0
show_missed_notifications https://github.com token "$NOTIFICATIONS_JSON" $shown_date > $SHUNIT_TMPDIR/last_shown_date
assertEquals 1 $?
verify_with_all_args terminal_notifier "$COMMIT2"
assertEquals 0 $?
verify_with_all_args terminal_notifier "Failed to fetch a notification Failed to connect to https://api.github.com/repos/octokit/octokit.rb/issues/comments/123 https://github.com/notifications"
verify_with_all_args terminal_notifier "More missed notifications on github.com" "See all" "" https://github.com/notifications
}

Expand Down

0 comments on commit 6001610

Please sign in to comment.