#!/usr/bin/env bash set -euf -o pipefail curp="$(cd "$(dirname "$0")" && pwd)" create_org() { orgid=$(curl --silent -X POST "${GITEA_SERVER_API_URL}/orgs" \ -H "Authorization: token ${GITEA_ADMIN_TOKEN}" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d "{ \"username\": \"$1\", \"visibility\": \"limited\"}" | jq -r '.id') [[ "$orgid" != "null" ]] && sleep 1 || true } create_repo() { repoid=$(curl --silent -X POST "${GITEA_SERVER_API_URL}$2" \ -H "Authorization: token ${GITEA_ADMIN_TOKEN}" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d "{\"auto_init\": false, \"private\": true, \"name\": \"$1\"}" | jq -r '.id') [[ "$repoid" != "null" ]] && sleep 5 || true } ARGUMENT_LIST=( "url:" "token:" "admin::" "password::" "pushurl::" ) # defaults : ${GITEA_ADMIN_USERNAME:=$USER} : ${GITEA_ADMIN_PASSWORD:=} # example ssh://git@gitea.example.com:3022 : ${GITEA_SERVER_PUSH_URL:=} opts=$( getopt \ --longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \ --name "$(basename "$0")" \ --options "" \ -- "$@" ) while [[ $# -gt 0 ]]; do case "$1" in --url) GITEA_SERVER_URL="$2" shift 2 ;; --token) GITEA_ADMIN_TOKEN="$2" shift 2 ;; --admin) GITEA_ADMIN_USERNAME="$2" shift 2 ;; --password) GITEA_ADMIN_PASSWORD="$2" shift 2 ;; --pushurl) GITEA_SERVER_PUSH_URL="$2" shift 2 ;; *) break ;; esac done [ -z "${GITEA_SERVER_PUSH_URL}" ] && GITEA_SERVER_PUSH_URL="$(echo "${GITEA_SERVER_URL}" | perl -pe "s#(https?://)#\${1}${GITEA_ADMIN_USERNAME}:${GITEA_ADMIN_PASSWORD}@#")" GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1" # repo examples # /path/to/repo:orgname ( orgname only ) # /path/to/repo::reponame ( repo name only ) # /path/to/repo:orgname:reponame ( all options passed ) # /path/to/repo ( use name from path, no org ) # init repos for repo in "$@"; do repoPath="$(echo "$repo" | cut -d ':' -f1 | tr -d '[:space:]')" repoOrg="$(echo "$repo" | cut -d ':' -f2 | tr -d '[:space:]')" repoName="$(echo "$repo" | cut -d ':' -f3 | tr -d '[:space:]')" [ -z "$repoName" ] && repoName="$(basename "$repoPath" ".git")" if [ -z "$repoOrg" ]; then repoAPIEP="/user/repos" repoOwner="${GITEA_ADMIN_USERNAME}" else create_org "$repoOrg" repoAPIEP="/org/$repoOrg/repos" repoOwner="$repoOrg" fi create_repo "${repoName}" "${repoAPIEP}" 1>&2 ( cd "$repoPath" git push "${GITEA_SERVER_PUSH_URL}/${repoOwner}/${repoName}.git" master ) done