pihanepi/orchestrating/cronjobs/forgejo-clone/forgejo-clone.sh
2026-06-04 14:43:28 +02:00

72 lines
2.1 KiB
Bash
Executable file

#!/usr/bin/env sh
. secrets
#TODO check secrets file exists
#Retrieve token user
user_json=$(curl -s -X 'GET' \
'https://$GIT_DOMAIN/api/v1/user' \
-H 'accept: application/json' \
-H "Authorization: token ${AUTH_TOKEN}")
user_id=$(jq ".id" <(echo "$user_json"))
user_username=$(jq ".username" <(echo "$user_json"))
echo $user_username
#Retrieve repos from user, including private repos
repo_json=$(curl -s -X 'GET' \
"https://$GIT_DOMAIN/api/v1/repos/search?uid=${user_id}?private=true" \
-H 'accept: application/json' \
-H "Authorization: token ${AUTH_TOKEN}")
#Listing only the HTTPS repo clone URL
repo_urls=$(jq ".data" <(echo "$repo_json") | jq ".[] | .html_url" | grep Hane)
echo $repo_urls
echo "----\n"
#Cloning each repo to destdir
repo_idx=0;
repo_finished=false;
while ! $repo_finished
do
repo_idx=$(($repo_idx+1));
repo=$(awk "{print \$$repo_idx}" <(echo $repo_urls))
if [ "$repo" = "" ]
then
repo_finished=true
else
#Retrieve repo name for folder
repo_name="$(awk -F/ '{print $5}' <(echo $repo))"
repo_name="${repo_name%\"}"
#echo $repo_name
#Remove quotes from user_username
username=${user_username%\"}
username=${username#\"}
#Clone or rebase repo
if [ -e "$REPO_DIR$repo_name" ]
then
echo "REPO: $repo_name"
git -C "$REPO_DIR$repo_name" fetch -t --all
branch="$(git -C "$REPO_DIR$repo_name" branch | awk '{print $2}')"
git -C "$REPO_DIR$repo_name" rebase $branch --onto origin/$branch
else
git clone https://$user_username:$AUTH_TOKEN@$GIT_DOMAIN/$username/$repo_name /home/$USER/pihanepi/datamount/gitbackup/$repo_name --recurse-submodules
fi
fi
done
#NOTE: Cool git formatting and same loop to operate on all remote branches
#branches="$(git -C $REPO_DIR$repo_name branch -r --format='%(refname:short)' | awk -F/ '{if ($2) print $2;}')"
#branch_idx=0;
#branch_finished=false;
#while ! $branch_finished
#do
# branch_idx=$(($branch_idx+1));
# branch=$(awk "{print \$$branch_idx}" <(echo $branches))
# if [ "$branch" = "" ]
# then
# branch_finished=true
# else
# git -C "$REPO_DIR$repo_name" rebase $branch --onto origin/$branch
# fi
#done