Skip to content

Instantly share code, notes, and snippets.

@twosdai
Created April 26, 2025 01:47
Show Gist options
  • Select an option

  • Save twosdai/cc017c2aa186a52618d2d4dbcadde363 to your computer and use it in GitHub Desktop.

Select an option

Save twosdai/cc017c2aa186a52618d2d4dbcadde363 to your computer and use it in GitHub Desktop.
gas.sh
function gas() {
local repo_dir branch owner repo api_url gh_status conclusion color
# Ensure GITHUB_TOKEN is set
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "❌ GITHUB_TOKEN is not set. Export it in your shell environment."
return 1
fi
# Find the Git repo root
repo_dir=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -z "$repo_dir" ]]; then
echo "❌ Not inside a Git repository."
return 1
fi
cd "$repo_dir" || return 1
# Get branch name
branch=$(git rev-parse --abbrev-ref HEAD)
# Get remote URL and parse owner/repo
remote_url=$(git config --get remote.origin.url)
# Normalize GitHub remote URLs to https://github.com/owner/repo format
if [[ "$remote_url" =~ ^git@github\.com:(.*)/(.*)(\.git)?$ ]]; then
owner="${match[1]}"
repo="${match[2]%.git}"
elif [[ "$remote_url" =~ ^https://github\.com/(.*)/(.*)(\.git)?$ ]]; then
owner="${match[1]}"
repo="${match[2]%.git}"
else
echo "❌ Could not parse GitHub repository from remote URL: $remote_url"
return 1
fi
echo "remote_url: $remote_url"
# GitHub API URL for workflow runs
api_url="https://api.github.com/repos/$owner/$repo/actions/runs?branch=$branch&per_page=1"
# Call GitHub API
response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" "$api_url")
if [[ -z "$response" ]]; then
echo "❌ Failed to get response from GitHub."
return 1
fi
gh_status=$(echo "$response" | jq -r '.workflow_runs[0].status')
conclusion=$(echo "$response" | jq -r '.workflow_runs[0].conclusion')
if [[ "$gh_status" == "completed" ]]; then
if [[ "$conclusion" == "success" ]]; then
color="%F{green}Success%f"
else
color="%F{red}Failed%f"
fi
else
color="%F{yellow}In progress%f"
fi
print -P "$color"
}
@twosdai
Copy link
Copy Markdown
Author

twosdai commented Apr 26, 2025

This quick script handles getting your current repo's status of its job and printing it out to your CLI. EG:
image

Its purpose is so that when you go to a repo, and you're working you can quickly type gas get the current status of the job without leaving your terminal.

@twosdai
Copy link
Copy Markdown
Author

twosdai commented Apr 26, 2025

Make sure to export a token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment