-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-commit-status
executable file
·65 lines (51 loc) · 1.54 KB
/
watch-commit-status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
set -euo pipefail
_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
# Cache head commit to avoid breaking when the user makes a new commit on the branch locally
head="$(git rev-parse HEAD)"
api_url="/repos/{owner}/{repo}/commits/${head}/status"
get_state() {
gh api --cache="5s" --jq='.state' "$api_url"
}
print_status() {
"$_dir"/print-commit-status "$head"
}
state="$(get_state)"
print_status
while [ "$state" = 'pending' ]; do
sleep 5
# Cache response so that we can immediately make the same request again with different output formatting
if ! state="$(get_state)"; then
echo 'error: failed to get commit state'
exit 1
fi
if ! [ "$state" = 'pending' ]; then
# Account for gaps in status check execution by checking again
sleep 5
state="$(get_state)"
if ! [ "$state" = 'pending' ]; then
break
fi
fi
tput clear
print_status
done
tput clear
print_status
project="$(basename "$(pwd)")"
if [ "$state" = 'success' ]; then
message="CI for $project succeeded."
elif [ "$state" = 'failure' ]; then
message="CI for $project failed."
else
message="Something unexpected happened in CI for $project."
fi
if [[ "$OSTYPE" == darwin* ]]; then
osascript -e "display notification \"$message\" with title \"gh-watch\""
elif [[ $OSTYPE == *linux* ]]; then
if command -v notify-send >/dev/null; then
notify-send 'gh-watch' "$message"
else
echo -e 'warning: failed to create an OS-level notification with "notify-send", you may need to install \e]8;;https://gitlab.gnome.org/GNOME/libnotify\e\\libnotify-bin\e]8;;\e\\ '
fi
fi