Forked from twosdai/gist:cc017c2aa186a52618d2d4dbcadde363
Created
April 30, 2025 08:58
-
-
Save zaherg/7c0306e4a8055314c4e0d3e0eb4882ff to your computer and use it in GitHub Desktop.
gas.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment