Files
dotfiles/private_dot_local/bin/executable_auto_clone
2025-11-04 23:28:37 +01:00

79 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
project_dir="$HOME/projects"
function process_repo() {
local json="$1"
local remote_name="$2"
local checkout="$project_dir"
if [[ "$(echo "$json" | jq -r '.name')" == 'dotfiles' ]]; then
return # do not clone dotfiles
fi
local full_name="$(echo "$json" | jq -r '.full_name')"
local clone_url="$(echo "$json" | jq -r '.clone_url')"
local ssh_url="$(echo "$json" | jq -r '.ssh_url')"
echo "$full_name"
if [[ "$(echo "$json" | jq -r '.archived')" == 'true' ]]; then
echo " archived"
checkout="$checkout/archive"
elif [[ "$(echo "$json" | jq -r '.template')" == 'true' ]]; then
echo " template"
checkout="$checkout/templates"
fi
if [ -d "$checkout/$full_name" ]
then
echo " already exists"
if [[ "$(git -C "$checkout/$full_name" remote | grep "^$remote_name\$")" == "" ]]
then
git -C "$checkout/$full_name" remote add "$remote_name" "$ssh_url"
else
git -C "$checkout/$full_name" remote set-url "$remote_name" "$ssh_url"
fi
else
echo " clone to $checkout/$full_name"
url="$(echo "$clone_url" | sed "s|://|://$token@|")"
git clone "$url" "$checkout/$full_name"
git -C "$checkout/$full_name" remote set-url origin "$ssh_url"
git -C "$checkout/$full_name" remote add "$remote_name" "$ssh_url"
fi
}
function fetch_repos() {
local base_url="$1"
local remote_name="$2"
local token="$3"
local page=1
while [[ $page -lt 50 ]]
do
# echo
echo "get repo list page $page"
repos="$(curl -X 'GET' "$base_url/user/repos?page=$page&token=$token" \
-H 'accept: application/json')"
if [[ "$repos" != "[]" ]]
then
for repo in $(echo "$repos" | jq -c '.[]' | sed -e 's/ /%20;/g')
do
repo="$( echo "$repo" | sed -e 's/%20;/ /g' )"
process_repo "$repo"
done
else
echo "No more repositories found."
page=999
fi
page="$(( $page + 1 ))"
done
}
fetch_repos https://gitea.finnvanreenen.nl/api/v1 tea "$(pass show gitea.finnvanreenen.nl/autoclone)"
fetch_repos https://git.gay/api/v1 gay "$(pass show git.gay/autoclone)"