Skip to content

Commit c0689d6

Browse files
committed
New segment github_notify
* add new util function "is_tmp_valid" to make handling of temp files based on last update reusable * add new segment fixes #431
1 parent 29143d8 commit c0689d6

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

lib/util.sh

+30
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,33 @@ is_flag_enabled() {
1212
;;
1313
esac
1414
}
15+
16+
is_tmp_valid() {
17+
local tmp_file="$1"
18+
local tmp_validity="$2"
19+
20+
if [ -f "$tmp_file" ]; then
21+
if shell_is_osx || shell_is_bsd; then
22+
stat >/dev/null 2>&1 && is_gnu_stat=false || is_gnu_stat=true
23+
if [ "$is_gnu_stat" == "true" ]; then
24+
last_update=$(stat -c "%Y" "${tmp_file}")
25+
else
26+
last_update=$(stat -f "%m" "${tmp_file}")
27+
fi
28+
elif shell_is_linux || [ -z "$is_gnu_stat" ]; then
29+
last_update=$(stat -c "%Y" "${tmp_file}")
30+
fi
31+
32+
time_now=$(date +%s)
33+
valid=$(echo "(${time_now}-${last_update}) < ${tmp_validity}" | bc)
34+
35+
if [ "$valid" -eq 1 ]; then
36+
return 0
37+
else
38+
return 1
39+
fi
40+
else
41+
return 1
42+
fi
43+
44+
}

segments/github_notify.sh

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# shellcheck shell=bash
2+
3+
# shellcheck source=lib/util.sh
4+
source "${TMUX_POWERLINE_DIR_LIB}/util.sh"
5+
6+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE:-$(date +%Y-%m-%dT00:00:00Z)}"
7+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE:-no}"
8+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE:-50}"
9+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL:-60}"
10+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE:-no}"
11+
TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS:-yes}"
12+
13+
generate_segmentrc() {
14+
read -r -d '' rccontents <<EORC
15+
# Github token (https://github.com/settings/tokens) with at least "notifications" scope
16+
export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_TOKEN=""
17+
# Include available notification reasons (https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#about-notification-reasons),
18+
# in the format "REASON:SEPARATOR"
19+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_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")
20+
# Summarize all notifications
21+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE}"
22+
# Hide if no notifications
23+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS}"
24+
# Only show new notifications since date (default: today)
25+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE:-\$(date +%Y-%m-%dT00:00:00Z)}"
26+
# Enable show only notifications since date
27+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE}"
28+
# Maximum notifications to retreive (upstream github default per_page)
29+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE}"
30+
# Update interval to pull latest state from github api
31+
# export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL}"
32+
EORC
33+
echo "$rccontents"
34+
}
35+
36+
run_segment() {
37+
__process_settings
38+
if ! type curl >/dev/null 2>&1; then
39+
return 0
40+
fi
41+
42+
if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_TOKEN" ]; then
43+
return 0
44+
fi
45+
46+
local tmp_file
47+
local api_query
48+
49+
tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/github_notify.stat"
50+
51+
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE"; then
52+
api_query="since=${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE}&"
53+
fi
54+
api_query="${api_query}per_page=${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE}"
55+
56+
if ! is_tmp_valid "$tmp_file" "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL"; then
57+
curl --url "https://api.github.com/notifications?${api_query}" --header "Authorization: Bearer $TMUX_POWERLINE_SEG_GITHUB_NOTIFY_TOKEN" >"$tmp_file"
58+
fi
59+
read -r -d '' notifications <"$tmp_file"
60+
61+
all_count=0
62+
result=""
63+
64+
for reason_entry in "${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_REASONS[@]}"; do
65+
count=0
66+
IFS=$':' read -r reason separator <<<"$reason_entry"
67+
count="$(echo "$notifications" | jq -c '.[] | select( .reason == "'"$reason"'" )' | jq -s 'length')"
68+
69+
if [ "$count" -eq 0 ] && is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS"; then
70+
continue
71+
fi
72+
73+
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE"; then
74+
all_count="$((all_count + count))"
75+
else
76+
result="$result $separator:$count"
77+
fi
78+
done
79+
80+
if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE"; then
81+
if [ "$all_count" -gt 0 ] || ! is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS"; then
82+
echo "$all_count"
83+
fi
84+
else
85+
if [ -n "$result" ]; then
86+
echo "$result"
87+
fi
88+
fi
89+
}
90+
91+
__process_settings() {
92+
if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_REASONS" ]; then
93+
export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_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")
94+
fi
95+
}

0 commit comments

Comments
 (0)