Skip to content

Instantly share code, notes, and snippets.

@alexrashed
Last active March 14, 2024 11:06
Show Gist options
  • Save alexrashed/59963d77b25855f5157cdf57ba10dbe6 to your computer and use it in GitHub Desktop.
Save alexrashed/59963d77b25855f5157cdf57ba10dbe6 to your computer and use it in GitHub Desktop.
Follow CircleCI and GitHub workflows by URL
#!/bin/bash
set -euo pipefail
# Prerequisites:
# - await.sh: https://gist.github.com/alexrashed/9dc04e78cff737b7cbb42861b6555221#file-await-sh
# - ntfy: https://github.com/binwiederhier/ntfy
# - Github CLI: https://cli.github.com/
if [[ $# -eq 0 ]]; then
echo "No URL given, trying to get URL for PR in current branch..."
url=`gh pr view --json url --jq .url`
else
url=$1
fi
circle_pattern="^https://app.circleci.com/pipelines/github/(([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+))/[0-9]+/workflows/(.*)$"
github_run_pattern="^https://github.com/(([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+))/actions/runs/([0-9]+).*$"
github_pr_pattern="^https://github.com/(([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+))/pull/([0-9]+)$"
if [[ $url =~ $circle_pattern ]]; then
echo "Detected CircleCI URL..."
workflow_id=${BASH_REMATCH[4]}
command="await.sh $workflow_id"
# TODO ask for the title for now, maybe auto-determine?
read -r -p "Title? " title; echo "";
else
if [[ $url =~ $github_run_pattern ]]; then
echo "Detected GitHub Run URL..."
repo=${BASH_REMATCH[1]}
run_id=${BASH_REMATCH[4]}
command="gh run watch $run_id --repo $repo"
url="https://github.com/$repo/actions/runs/$run_id"
# TODO ask for the title for now, maybe auto-determine?
read -r -p "Title? " title; echo "";
else
if [[ $url =~ $github_pr_pattern ]]; then
echo "Detected GitHub PR URL..."
repo=${BASH_REMATCH[1]}
pr_id=${BASH_REMATCH[4]}
command="gh pr checks $pr_id --repo $repo --watch"
url="https://github.com/$repo/pull/$pr_id"
title=`gh pr view $pr_id --repo $repo --json title --jq .title`
else
echo "Could not detect what to follow based on URL."
fi
fi
fi
echo "Starting to follow workflow \"$title\": $url"
bash -c "ntfy -b linux -t \"$title\" done $command"
echo "Workflow seems to be done, opening browser window..."
xdg-open $url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment