From 038424b52630be62f749e1cfaa4d7741d0cb7c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Fr=C3=A9rot?= Date: Mon, 30 Dec 2024 18:17:22 +0100 Subject: [PATCH] added gitea methods to setup remote repo --- rtenets | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/rtenets b/rtenets index 45b096b..4a2eaf9 100755 --- a/rtenets +++ b/rtenets @@ -119,7 +119,7 @@ gitea() { local request="$2" local data="" - if [[ $# > 3 ]]; then + if [[ $# > 2 ]]; then data="$3" fi @@ -263,6 +263,71 @@ create_tenet_file_tree() { script_stub make_all_figures "Generating figures..." } +get_gitea_owner() { + gitea /user GET | jq -r '.login' +} + +# Check if repository exists +get_gitea_repo() { + local owner="$1" + local repo_name="$2" + gitea "/repos/${owner}/${repo_name}" GET +} + +# Create a repository on gitea +create_gitea_repo() { + local owner="$1" + local repo_name="$2" + local get_response="$(get_gitea_repo "${owner}" "${repo_name}")" + + # Check if repo already exits + if [[ "$(jq '.id' <<< "${get_response}")" == "null" ]]; then + info "creating repository '${repo_name}' on gitea" + gitea /user/repos POST "{ \"name\": \"${repo_name}\", \"private\": true }" + else + info "found repository '${repo_name}' on gitea" + printf "%s" "${get_response}" + fi +} + +# API call to setup the software heritage webhook +setup_software_heritage_hook() { + local owner="$1" + local repo_name="$2" + local remote_url="$3" + #gitea "/repos/${owner}/${repo_name}/hooks" GET + gitea "/repos/${owner}/${repo_name}/hooks" POST \ + "{ \"type\": \"gitea\", + \"config\": { + \"content_type\": \"json\", + \"url\": \"https://archive.softwareheritage.org/api/1/origin/save/git/url/${remote_url}\" + }, + \"events\": [\"release\"], + \"active\": true + }" + info "created Software Hertiage webhook for releases in gitea" +} + +# Setup remote +setup_dalembert_gitea() { + local repo_name="$(basename <<< "${PWD}")" + local owner="$(get_gitea_owner)" + local repo_json="$(create_gitea_repo "${repo_name}")" + local remote_ssh="$(jq '.ssh_url' <<< "${repo_json}")" + local remote_http="$(jq '.clone_url' <<< "${repo_json}")" + + if [[ "$(\git remote | \grep -c '^dalembert$')" == "0" ]]; then + \git remote add dalembert "${remote_ssh}" + info "created remote dalembert with '${remote_ssh}" + else + \git remote set-url dalembert "${remote_ssh}" + info "set remote dalembert with '${remote_ssh}" + fi + + setup_software_heritage_hook "${owner}" "${repo_name}" "${remote_http}" +} + + # Create git repository create_repo() { local repo_name="$1"