Skip to content

Commit

Permalink
eliminate globbing and splitting from github_notif
Browse files Browse the repository at this point in the history
  • Loading branch information
sargsyan committed Sep 22, 2018
1 parent c1769e2 commit 80483df
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions github_notif
@@ -1,16 +1,16 @@
#!/bin/bash

readonly DIR_NAME=$(dirname $BASH_SOURCE)
readonly DIR_NAME=$(dirname "$BASH_SOURCE")
readonly MAX_NUMBER_OF_MISSED_NOTIFICATIONS_TO_SHOW=2
KEEP_IN_SCREEN_TIME_IN_SECONDS=5
PATH=$PATH:/usr/local/bin

. $DIR_NAME/lib/config_accessor.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
. "$DIR_NAME/lib/config_accessor.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
Expand All @@ -33,16 +33,16 @@ function show_notification () {
return 1
fi

notification_type=$(echo $notification_type | sed 's|PullRequest|a PR|;s|Issue|an issue|;s|Commit|a commit|')
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"
[[ "$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)
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
echo "$notification_details_json"
return 1
fi

Expand All @@ -65,24 +65,24 @@ function show_notification () {

local user=$details_user
local avatar_url=$DIR_NAME/logo.png
[[ $details_user_avatar_url != "null" ]] && avatar_url=$details_user_avatar_url
[[ "$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
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
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
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
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'
Expand All @@ -92,21 +92,21 @@ function show_notification () {
#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}"
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
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)
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
show_notification_window "More missed notifications on $(get_resource_name "$config_url")" "See all" "" "$config_url/notifications"
}

function show_missed_notifications() {
Expand All @@ -120,7 +120,7 @@ function show_missed_notifications() {

for ((i = 0; i < $MAX_NUMBER_OF_MISSED_NOTIFICATIONS_TO_SHOW; i++)); do
if (( $shown_date < ${latest_commit_dates[$i]:--1} )); then
error_message=$(show_notification $access_token "$notifications_json" $i)
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"
Expand All @@ -134,11 +134,11 @@ function show_missed_notifications() {
done

if (( $shown_date < ${latest_commit_dates[2]:--1} )); then
show_all_notifications $config_url
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]}
(( $exit_code == 0 && $shown_date > ${latest_commit_dates[0]:--1} )) && echo "$shown_date" || echo "${latest_commit_dates[0]}"
return $exit_code
}

Expand All @@ -151,27 +151,27 @@ function main() {
fi

for config_name in $active_configs ; do
local last_shown_commit_date=$(get_last_shown_commit_date $config_name)
local last_shown_commit_date=$(get_last_shown_commit_date "$config_name")
local notifications_json
access_token=$(get_token $config_name)
access_token=$(get_token "$config_name")

local notifications_api_url="${config_name/https:\/\//https://api.}"/notifications
notifications_json=$(do_github_remote_call $notifications_api_url $access_token)
notifications_json=$(do_github_remote_call "$notifications_api_url" "$access_token")
local call_result=$?
if [[ $call_result -ne 0 ]]; then
log_error $notifications_json
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)
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
save_last_shown_commit_date $config_name $updated_last_shown_commit_date
save_last_shown_commit_date "$config_name" "$updated_last_shown_commit_date"
fi
done

Expand Down

0 comments on commit 80483df

Please sign in to comment.