Skip to content

Commit

Permalink
feat(run): notify desktop when completed (experimental)
Browse files Browse the repository at this point in the history
Add a util subcommand for watching a GitHub Action run and displaying a
desktop notification on completion. Usage:

    $ gh fzf util notify-run-completed <run-id>

The supported notification programs (attempted in order) are:

- dunstify
- notify-send
- osascript (untested so far)

The util doesn't have a builtin keybinding for now, but can be enabled
in a gh config alias:

    # ~/.config/gh/config.yml
    aliases:
        runs: |
            !FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS
                --bind='alt-n:execute-silent(
                    gh fzf util notify-run-completed {-1} &
                )'
            " gh fzf run $*

An icon can be displayed next to the notification if you are using
dunstify or notify-send. Set the `GH_FZF_NOTIFY_ICON` environment
variable to specify the icon path:

    export GH_FZF_NOTIFY_ICON="$XDG_DATA_HOME/icons/Gruvbox/apps/scalable/github.svg"

WARNING: Subcommands of `gh fzf util` are considered "internal", meaning
their issues may be closed with: Whatever, I do what I want ¯\_(ツ)_/¯
  • Loading branch information
benelan committed May 30, 2024
1 parent 23d7ef9 commit 009598c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions gh-fzf
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,48 @@ copy_url() {
gh "$1" view "$2" --json 'url' -q '.url' $repo_flag | $GH_FZF_COPY_CMD
}

# arg1: title
# arg2: message
# arg3: urgency (low, normal, critical)
# arg4: icon path
notify() {
if has dunstify; then
dunstify "$1" "$2" ${3:+-u $3} ${4:+-i $4} -a "gh-fzf"
elif has notify-send; then
notify-send "$1" "$2" ${3:+-u $3} ${4:+-i $4} -a "gh-fzf"
elif has osascript; then
osascript -e "display notification '$2' with title '$1'"
else
error "no notification support found." \
"Supported: osascript, dunstify, notify-send"
fi

}

# arg1: run id
notify_run_completed() {
[ -z "$1" ] && error "missing argument." \
"Usage: gh fzf util notify-run-completed <run-id>"

branch="$(gh run view ${1} --json headBranch --jq .headBranch)"
workflow="$(gh run view ${1} --json workflowName --jq .workflowName)"

if gh run watch --exit-status ${1}; then
notify "$workflow" "Passed${branch:+ on branch: $branch}" \
"normal" "$GH_FZF_NOTIFY_ICON"
else
notify "$workflow" "Failed${branch:+ on branch: $branch}" \
"critical" "$GH_FZF_NOTIFY_ICON"
fi
}

util_cmd() {
util="$1"
[ -n "$util" ] && shift || error "missing util name"

case $util in
copy-url) copy_url "$@" ;;
notify-run-completed) notify_run_completed "$@" ;;
branch-prompt) branch_prompt "$@" ;;
develop-issue) develop_issue "$@" ;;
*) error "invalid util: \"$command\"" ;;
Expand Down

0 comments on commit 009598c

Please sign in to comment.