scripts/gh-env.sh
scripts/gh-env.shBrowse 17 files
782 tokens
2,556 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env bash2# GitHub environment detection helper for Hermes Agent skills.3#4# Usage (via terminal tool):5# source skills/github/github-auth/scripts/gh-env.sh6#7# After sourcing, these variables are set:8# GH_AUTH_METHOD - "gh", "curl", or "none"9# GITHUB_TOKEN - personal access token (set if method is "curl")10# GH_USER - GitHub username11# GH_OWNER - repo owner (only if inside a git repo with a github remote)12# GH_REPO - repo name (only if inside a git repo with a github remote)13# GH_OWNER_REPO - owner/repo (only if inside a git repo with a github remote)14 15# --- Auth detection ---16 17GH_AUTH_METHOD="none"18GITHUB_TOKEN="${GITHUB_TOKEN:-}"19GH_USER=""20 21if command -v gh &>/dev/null && gh auth status &>/dev/null 2>&1; then22 GH_AUTH_METHOD="gh"23 GH_USER=$(gh api user --jq '.login' 2>/dev/null)24elif [ -n "$GITHUB_TOKEN" ]; then25 GH_AUTH_METHOD="curl"26elif _hermes_env="${HERMES_HOME:-$HOME/.hermes}/.env"; [ -f "$_hermes_env" ] && grep -q "^GITHUB_TOKEN=" "$_hermes_env" 2>/dev/null; then27 GITHUB_TOKEN=$(grep "^GITHUB_TOKEN=" "$_hermes_env" | head -1 | cut -d= -f2 | tr -d '\n\r')28 if [ -n "$GITHUB_TOKEN" ]; then29 GH_AUTH_METHOD="curl"30 fi31elif [ -f "$HOME/.git-credentials" ]; then32 GITHUB_TOKEN=$(uv run python3 "${HERMES_HOME:-$HOME/.hermes}/skills/github/github-auth/scripts/git-credential-token.py")33 if [ -n "$GITHUB_TOKEN" ]; then34 GH_AUTH_METHOD="curl"35 fi36fi37 38# Resolve username for curl method39if [ "$GH_AUTH_METHOD" = "curl" ] && [ -z "$GH_USER" ]; then40 GH_USER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \41 https://api.github.com/user 2>/dev/null \42 | python3 -c "import sys,json; print(json.load(sys.stdin).get('login',''))" 2>/dev/null)43fi44 45# --- Repo detection (if inside a git repo with a GitHub remote) ---46 47GH_OWNER=""48GH_REPO=""49GH_OWNER_REPO=""50 51_remote_url=$(git remote get-url origin 2>/dev/null)52if [ -n "$_remote_url" ] && echo "$_remote_url" | grep -q "github.com"; then53 GH_OWNER_REPO=$(echo "$_remote_url" | sed -E 's|.*github\.com[:/]||; s|\.git$||')54 GH_OWNER=$(echo "$GH_OWNER_REPO" | cut -d/ -f1)55 GH_REPO=$(echo "$GH_OWNER_REPO" | cut -d/ -f2)56fi57unset _remote_url58 59# --- Summary ---60 61echo "GitHub Auth: $GH_AUTH_METHOD"62[ -n "$GH_USER" ] && echo "User: $GH_USER"63[ -n "$GH_OWNER_REPO" ] && echo "Repo: $GH_OWNER_REPO"64[ "$GH_AUTH_METHOD" = "none" ] && echo "⚠ Not authenticated — see github-auth skill"65 66export GH_AUTH_METHOD GITHUB_TOKEN GH_USER GH_OWNER GH_REPO GH_OWNER_REPO67