-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new util function "is_tmp_valid" to make handling of temp files based on last update reusable * add new segment * add jq requirement to README.md fixes #431
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# shellcheck shell=bash | ||
|
||
# shellcheck source=lib/util.sh | ||
source "${TMUX_POWERLINE_DIR_LIB}/util.sh" | ||
|
||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE:-$(date +%Y-%m-%dT00:00:00Z)}" | ||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE_ENABLE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE_ENABLE:-no}" | ||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_PER_PAGE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_PER_PAGE:-50}" | ||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_UPDATE_INTERVAL="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_UPDATE_INTERVAL:-60}" | ||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE:-no}" | ||
TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS:-yes}" | ||
|
||
generate_segmentrc() { | ||
read -r -d '' rccontents <<EORC | ||
# Github token (https://github.com/settings/tokens) with at least "notifications" scope | ||
export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_TOKEN="" | ||
# Include available notification reasons (https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#about-notification-reasons), | ||
# in the format "REASON:SEPARATOR" | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_REASONS=("approval_requested:areq" "assign:as" "author:au" "comment:co" "ci_activity:ci" "invitation:in" "manual:ma" "mention:me" "review_requested:rreq" "security_alert:sec" "state_change:st" "subscribed:sub" "team_mention:team") | ||
# Summarize all notifications | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE}" | ||
# Hide if no notifications | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS}" | ||
# Only show new notifications since date (default: today) (takes up to UPDATE_INTERVAL time to take effect) | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE="\$(date +%Y-%m-%dT00:00:00Z)" | ||
# Enable show only notifications since date (takes up to UPDATE_INTERVAL time to take effect) | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE_ENABLE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE_ENABLE}" | ||
# Maximum notifications to retreive (upstream github default per_page) | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_PER_PAGE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_PER_PAGE}" | ||
# Update interval to pull latest state from github api | ||
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_UPDATE_INTERVAL="${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_UPDATE_INTERVAL}" | ||
EORC | ||
echo "$rccontents" | ||
} | ||
|
||
run_segment() { | ||
__process_settings | ||
if ! type curl >/dev/null 2>&1; then | ||
return 0 | ||
fi | ||
|
||
if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_TOKEN" ]; then | ||
return 0 | ||
fi | ||
|
||
local tmp_file | ||
local api_query | ||
|
||
tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/github_notify.stat" | ||
|
||
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE_ENABLE"; then | ||
api_query="since=${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SINCE}&" | ||
fi | ||
api_query="${api_query}per_page=${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_PER_PAGE}" | ||
|
||
if ! is_tmp_valid "$tmp_file" "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_UPDATE_INTERVAL"; then | ||
curl --url "https://api.github.com/notifications?${api_query}" --header "Authorization: Bearer $TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_TOKEN" >"$tmp_file" | ||
fi | ||
read -r -d '' notifications <"$tmp_file" | ||
|
||
all_count=0 | ||
result="" | ||
|
||
for reason_entry in "${TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_REASONS[@]}"; do | ||
count=0 | ||
IFS=$':' read -r reason separator <<<"$reason_entry" | ||
count="$(echo "$notifications" | jq -c '.[] | select( .reason == "'"$reason"'" )' | jq -s 'length')" | ||
|
||
if [ "$count" -eq 0 ] && is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS"; then | ||
continue | ||
fi | ||
|
||
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE"; then | ||
all_count="$((all_count + count))" | ||
else | ||
result="$result $separator:$count" | ||
fi | ||
done | ||
|
||
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_SUMMARIZE"; then | ||
if [ "$all_count" -gt 0 ] || ! is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_HIDE_NO_NOTIFICATIONS"; then | ||
echo " $all_count" | ||
fi | ||
else | ||
if [ -n "$result" ]; then | ||
echo " $result" | ||
fi | ||
fi | ||
} | ||
|
||
__process_settings() { | ||
if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_REASONS" ]; then | ||
export TMUX_POWERLINE_SEG_GITHUB_NOTIFICATIONS_REASONS=("approval_requested:areq" "assign:as" "author:au" "comment:co" "ci_activity:ci" "invitation:in" "manual:ma" "mention:me" "review_requested:rreq" "security_alert:sec" "state_change:st" "subscribed:sub" "team_mention:team") | ||
fi | ||
} |