Skip to content

Commit

Permalink
Merge pull request #26 from sargsyan/add-project-filter-support
Browse files Browse the repository at this point in the history
implement project filters
  • Loading branch information
sargsyan authored Jul 24, 2020
2 parents ca58400 + 1f673fa commit 439c808
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 41 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ test:
./test/url_test.sh
./test/mock_test.sh
./test/notifier_test.sh
./test/filters_test.sh
78 changes: 76 additions & 2 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ readonly APP_NAME=$(basename $BASH_SOURCE)
readonly DIR_NAME=$(dirname $BASH_SOURCE)

. $DIR_NAME/lib/config_accessor.sh
. $DIR_NAME/lib/filter_manager.sh

function usage() {
cat <<- EOF
Expand All @@ -15,6 +16,7 @@ Usage:
$APP_NAME activate <github instance url>
$APP_NAME deactivate <github instance url>
$APP_NAME token update <github instance url> [<new token>]
$APP_NAME filters
Normally, the most popular github instance is https://github.com
The other instances are github enterprise instances.
Expand All @@ -25,7 +27,7 @@ Examples:
Add a new configuration:
$APP_NAME add https://github.mycompany.com
Deactivate a configuration:
$APP_NAME deactivate https://github.mycompany.com
Expand Down Expand Up @@ -66,6 +68,9 @@ function route_command() {
local access_token=$4
route_token_command $token_cmd $config_name $access_token
;;
"filters")
route_filters_command $2 $3 $4 $5
;;
*)
if [[ $cmd ]]; then
echo "unknown command $cmd"
Expand All @@ -75,6 +80,75 @@ function route_command() {
esac
}

function filters_usage() {
cat <<- EOF
Usage:
$APP_NAME filters list
$APP_NAME filters add <github instance url> <filter name>
$APP_NAME filters rm <filter name>
$APP_NAME filters activate <filter name>
$APP_NAME filters deactivate <filter name>
$APP_NAME filters add-project <filter name> <github project name> <include|exclude>
$APP_NAME filters list-projects <filter name>
Examples:
Add filter groups:
$APP_NAME filters add https://github.com work_projects
Add a few projects to the new filter group:
$APP_NAME filters add-project work_projects redis/redis include
$APP_NAME filters add-project work_projects redis/redis-rb include
EOF
}

function route_filters_command() {
local cmd=$1
case $cmd in
"list")
list_filter_configs
;;
"add")
local config_name=$2
local filter_name=$3
add_filter_config $config_name $filter_name
;;
"rm")
local filter_name=$2
remove_filter_config $filter_name
;;
"activate")
local filter_name=$2
activate_filter_config $filter_name
;;
"deactivate")
local filter_name=$2
deactivate_filter_config $filter_name
;;
"add-project")
local filter_name=$2
local project_name=$3
local filter_type=$4
add_project_filter $filter_name $project_name $filter_type
;;
"rm-project")
local filter_name=$2
local project_name=$3
remove_project_filter $filter_name $project_name
;;
"list-projects")
local filter_name=$2
list_project_filters $filter_name
;;
*)
if [[ $cmd ]]; then
echo "unknown command for filters $cmd"
fi
filters_usage
;;
esac
}

function route_token_command() {
local cmd=$1
local config_name=$2
Expand All @@ -89,4 +163,4 @@ function route_token_command() {
esac
}

route_command $1 $2 $3 $4
route_command $1 $2 $3 $4 $5
2 changes: 2 additions & 0 deletions constants.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LAUNCH_AGENTS_DIR=$HOME/Library/LaunchAgents/
SERVICE_NAME='github_notif'
LOGFILE_PATH=$HOME/Library/Logs/$SERVICE_NAME/service.log
CONFIG_FILE_NAME=.${SERVICE_NAME}_conf
PROJECT_FILTER='project-filter-'
41 changes: 35 additions & 6 deletions github_notif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand All @@ -18,11 +19,13 @@ 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)
Expand All @@ -35,6 +38,35 @@ function show_notification () {
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
Expand Down Expand Up @@ -128,24 +160,21 @@ function show_missed_notifications() {

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
if ((i != 0)); then
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
fi

error_message=$(show_notification "$access_token" "$notifications_json" $i)
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 (( i!=0 && $shown_date < ${latest_commit_dates[2]:-INSTANT_OF_THE_BIG_BANG} )); then
sleep $KEEP_IN_SCREEN_TIME_IN_SECONDS
if (( $shown_date < ${latest_commit_dates[2]:-INSTANT_OF_THE_BIG_BANG} )); then
show_all_notifications "$config_url"
fi

Expand Down
43 changes: 14 additions & 29 deletions lib/config_accessor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ LIB_DIR_NAME=$(dirname $BASH_SOURCE)
. $LIB_DIR_NAME/macos/keychain_accessor.sh
. $LIB_DIR_NAME/url.sh
. $LIB_DIR_NAME/../constants.sh
. $LIB_DIR_NAME/utils.sh

[ -z $CONFIG_FILE_DIR ] && CONFIG_FILE_DIR=$HOME
readonly CONFIG_FILE=$CONFIG_FILE_DIR/.${SERVICE_NAME}_conf
readonly CONFIG_FILE=$CONFIG_FILE_DIR/$CONFIG_FILE_NAME
readonly STATUS_ACTIVE='active'
readonly STATUS_INACTIVE='inactive'

Expand Down Expand Up @@ -41,9 +42,14 @@ function remove_config() {
ensure_config_file_is_created
assert_is_set "Configuration name" $1
local config_name=$1
local line_number=$(grep -n "$config_name " $CONFIG_FILE| cut -f1 -d:)
if [ -n "$line_number" ]; then
sed -i .bak "${line_number}d" $CONFIG_FILE &&
local line_numbers=($(grep -n "$config_name " $CONFIG_FILE| cut -f1 -d:)) #find configuration and all its filters
local sed_removal_expression;
for line_number in ${line_numbers[@]}; do
sed_removal_expression+="${line_number}d;";
done

if [ ${#line_numbers[@]} -gt 0 ]; then
sed -i .bak "$sed_removal_expression" $CONFIG_FILE &&
rm $CONFIG_FILE.bak
remove_macos_config $config_name $SERVICE_NAME ${USER}
assert_successful $? "Failed to remove $config_name"
Expand Down Expand Up @@ -101,46 +107,25 @@ function get_token() {

function list_configs() {
ensure_config_file_is_created
cat $CONFIG_FILE
cat $CONFIG_FILE | grep -v $PROJECT_FILTER
}

function clear_all_configs() {
ensure_config_file_is_created
local configs=$(cat $CONFIG_FILE | cut -d' ' -f1)
# To remove secrets tokens as well
local configs=$(list_configs | cut -d' ' -f1)
for config in $configs; do
remove_config $config
done
}

function get_active_configs() {
ensure_config_file_is_created
cat $CONFIG_FILE | grep ' active' | cut -d' ' -f1
list_configs | grep ' active' | cut -d' ' -f1
}

function ensure_config_file_is_created() {
if [ ! -f $CONFIG_FILE ]; then
touch $CONFIG_FILE
fi
}

function assert_is_set() {
local arg_description=$1
local arg=$2
if [[ ! $arg ]]; then
show_error "$arg_description is not provided" && exit
fi
}

function assert_successful() {
local command_return_value=$1
local error_message=$2

if [ $command_return_value -ne 0 ]; then
echo $error_message;
fi
}

function show_error() {
local message=$1
[[ $message ]] && echo $message
}
Loading

0 comments on commit 439c808

Please sign in to comment.