feat: Implement repository provider interfaces for GitHub and Forgejo

This commit is contained in:
Matteo Cherubini 2026-05-08 21:09:42 +02:00
parent 8608bb14cc
commit e2c78fec46
2 changed files with 107 additions and 0 deletions

53
providers/forgejo.sh Normal file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env bash
# =============================================================================
# providers/forgejo.sh
# Forgejo REST API provider implementation.
# =============================================================================
provider_name() {
echo "Forgejo (${FORGEJO_URL})"
}
# ---------------------------------------------------------------------------
# provider_create_repo <name> <description> <private: true|false>
# ---------------------------------------------------------------------------
provider_create_repo() {
local name="$1"
local desc="$2"
local private="$3"
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token ${FORGEJO_TOKEN}" \
-H "Content-Type: application/json" \
-X POST "${FORGEJO_URL}/api/v1/user/repos" \
-d "{
\"name\": \"${name}\",
\"description\": \"${desc}\",
\"private\": ${private},
\"auto_init\": false
}")
case "$http_code" in
201) success "Repository '${name}' created successfully." ;;
409) info "Repository '${name}' already exists - skipping." ;;
401) error "Unauthorized. Check your FORGEJO_TOKEN."; exit 1 ;;
*) error "Forgejo API returned HTTP ${http_code}. Check connectivity."; exit 1 ;;
esac
}
provider_clone_url() {
echo "${FORGEJO_URL}/${FORGEJO_USER}/${1}.git"
}
provider_ssh_url() {
local host
# Extract hostname by removing protocol and trailing slashes
host=$(echo "${FORGEJO_URL}" | sed -e 's|^[^/]*//||' -e 's|/*$||')
# Using port 222 as default for many homelab Forgejo/Gitea setups
echo "ssh://git@${host}:222/${FORGEJO_USER}/${1}.git"
}
provider_web_url() {
echo "${FORGEJO_URL}/${FORGEJO_USER}/${1}"
}

54
providers/github.sh Normal file
View file

@ -0,0 +1,54 @@
#!/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."; exit 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}"
}