import/include/gitea.sh

85 lines
2.6 KiB
Bash

gitea_endpoint() {
UTYPE="$1"
if [ "${UTYPE}" == "org" ]; then
API_ENDPOINT="orgs"
elif [ "${UTYPE}" == "user" ]; then
API_ENDPOINT="admin/users"
else
echo "unknown type '${UTYPE}'" 1>&2
exit 1
fi
echo -n "${API_ENDPOINT}"
}
gitea_users() {
GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1"
curl --silent \
-H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
"${GITEA_SERVER_API_URL}/$(gitea_endpoint "${UTYPE:-user}")" \
"$@"
}
gitea_repos() {
GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1"
owner="$1"
curl --silent \
-H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
"${GITEA_SERVER_API_URL}/$(gitea_endpoint "${UTYPE:-user}")/${owner}/repos" \
"${@:2}"
}
gitea_create_org() {
name="$1"
id=$(UTYPE=org gitea_users -X POST -d "{ \"username\": \"$name\", \"visibility\": \"limited\"}" | jq -r '.id')
[[ "$id" != "null" ]] && sleep 1 || true
}
gitea_create_user() {
name="$1"
id=$(gitea_users -X POST -d "{ \"username\": \"$name\", \"visibility\": \"limited\"}" | jq -r '.id')
[[ "$id" != "null" ]] && sleep 1 || true
}
gitea_create_repo() {
owner="$1"
repo="$2"
repoid=$(
UTYPE=$3 gitea_repos \
"$owner" \
-X POST \
-d "{\"auto_init\": false, \"private\": true, \"name\": \"$repo\"}" |
jq -r '.id'
)
[[ "$repoid" != "null" ]] && sleep 5 || true
}
gitea_create_repo_user() {
gitea_create_repo "$1" "$2" "user"
}
gitea_create_repo_org() {
gitea_create_repo "$1" "$2" "opg"
}
gitea_add_key_ssh() {
GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1"
owner="$1"
pubkey_val="$(cat "${2}" | cut -d ' ' -f1,2 | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))')"
pubkey_name="$(cat "${2}" | cut -d ' ' -f3)"
curl --silent -X POST "${GITEA_SERVER_API_URL}/admin/users/${owner}/keys" \
-H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" -H "Content-Type: application/json" \
-d "{ \"key\": ${pubkey_val}, \"read_only\": false ,\"title\":\"${pubkey_name}\"}"
}
gitea_add_key_gpg() {
GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1"
gpg_key="$(gpg --export --armor "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))')"
curl --silent -X POST "${GITEA_SERVER_API_URL}/user/gpg_keys" \
-H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" -H "Content-Type: application/json" \
-d "{ \"armored_public_key\": ${gpg_key}"
}