knowledge-genome-orchestrator/providers/github.sh

54 lines
1.4 KiB
Bash

#!/usr/bin/env bash
# =============================================================================
# providers/github.sh
# GitHub REST API provider implementation.
# =============================================================================
provider_name() {
echo "GitHub (https://github.com)"
}
_github_namespace() {
echo "${GITHUB_ORG:-$GITHUB_USER}"
}
provider_create_repo() {
local name="$1"
local desc="$2"
local private="$3"
local namespace
namespace=$(_github_namespace)
local endpoint="https://api.github.com/user/repos"
[[ -n "${GITHUB_ORG:-}" ]] && endpoint="https://api.github.com/orgs/${namespace}/repos"
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST "$endpoint" \
-d "{
\"name\": \"${name}\",
\"description\": \"${desc}\",
\"private\": ${private},
\"auto_init\": false
}")
case "$http_code" in
201) success "Repository '${name}' created on GitHub." ;;
422) info "Repository '${name}' already exists - skipping." ;;
*) error "GitHub API returned HTTP ${http_code}. Check token/permissions."; return 1 ;;
esac
}
provider_ssh_url() {
echo "git@github.com:$(_github_namespace)/${1}.git"
}
provider_clone_url() {
echo "https://github.com/$(_github_namespace)/${1}.git"
}
provider_web_url() {
echo "https://github.com/$(_github_namespace)/${1}"
}