nixos/sync/sync.sh

40 lines
No EOL
1.2 KiB
Bash

#!/bin/bash
Mirror-Clear(){
# existing remotes
local response=$(curl -s -X "GET" "https://gitlab.skynet.ie/api/v4/projects/$1/remote_mirrors" --header "PRIVATE-TOKEN: $TOKEN")
# https://stackoverflow.com/a/67638584
readarray -t local my_array < <(jq -c '.[]' <<< $response)
# iterate through the Bash array
for item in "${my_array[@]}"; do
local id=$(jq --raw-output '.id' <<< "$item")
curl -s -X "DELETE" "https://gitlab.skynet.ie/api/v4/projects/$1/remote_mirrors/$id" --header "PRIVATE-TOKEN: $TOKEN"
done
}
Mirror-Create(){
# make sure the values are clean of extra characters
local ID=${1}
local REPO_TMP=$(tr -d '\n\t\r ' <<<"${2}" )
local REPO=${REPO_TMP#"https://"}
local body="url=https://oauth2:$TOKEN_REMOTE@$REPO&enabled=true&only_protected_branches=false&keep_divergent_refs=false"
local uri="https://gitlab.skynet.ie/api/v4/projects/$id/remote_mirrors"
echo $uri
local tmp=""
curl -sS -X "POST" "$uri" --header "PRIVATE-TOKEN: $TOKEN" --data $body > tmp
}
Main() {
# for local dev
source .env
while IFS="," read -r id remote
do
Mirror-Clear $id
Mirror-Create $id $remote
done < <(tail -n +2 ./repos.csv)
}
Main