Compare commits

...

2 Commits

Author SHA1 Message Date
Lucas Frérot 038424b526
added gitea methods to setup remote repo 2024-12-30 18:17:22 +01:00
Lucas Frérot c229c9dd81
adding data to gitea api call function 2024-12-28 22:55:15 +01:00
1 changed files with 75 additions and 2 deletions

77
rtenets
View File

@ -117,9 +117,17 @@ gitea() {
local method="$1"
local request="$2"
\curl -X "${request}" \
local data=""
if [[ $# > 2 ]]; then
data="$3"
fi
\curl -s -X "${request}" \
-H "Content-Type: application/json" \
-H "Authorization: token ${TOKEN}" "${ENDPOINT}/${method}"
-H "Authorization: token ${TOKEN}" \
-d "${data}" \
"${ENDPOINT}/${method}"
}
# ----------------- Git commands -----------------------
@ -255,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"