diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 285b964..a431299 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ # borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml stages: - - flake + - misc - test - deploy - deploy_gitlab @@ -11,7 +11,7 @@ stages: # $PACKAGE_NAME = name of the flake that needs to be updated # $UPDATE_FLAKE = flag to update the flake update: - stage: flake + stage: misc tags: - nix # from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5 @@ -40,6 +40,17 @@ update: variables: - $UPDATE_FLAKE == "yes" +sync_repos: + stage: misc + image: registry.gitlab.com/gitlab-ci-utils/curl-jq:2.0.0 + script: + - cd sync + - chmod +x ./sync.sh + - ./sync.sh + rules: + - changes: + - sync/repos.csv + .scripts_base: &scripts_base # load nix environment - git pull origin $CI_COMMIT_REF_NAME diff --git a/sync/.gitignore b/sync/.gitignore new file mode 100644 index 0000000..6b8afe3 --- /dev/null +++ b/sync/.gitignore @@ -0,0 +1,2 @@ +/.idea +.env \ No newline at end of file diff --git a/sync/README.md b/sync/README.md new file mode 100644 index 0000000..6e7c3f7 --- /dev/null +++ b/sync/README.md @@ -0,0 +1,16 @@ +# Repo Sync +This subdir is intended for syncing repos on with + +## CSV file +This file is in the format of local id and remote link. +It must end on a newline + +## Tokens +Tokens have a lifetime of a year. + +| Site | User | Location | Scopes | Expiry | +|--------|-----------|-------------------------------------------------------------------|--------|------------| +| Gitlab | ulcompsoc | https://gitlab.com/-/user_settings/personal_access_tokens | api | 2024-12-26 | +| Skynet | compsoc | https://gitlab.skynet.ie/groups/compsoc1/-/settings/access_tokens | api | 2024-12-26 | + +They are then stored in https://gitlab.skynet.ie/compsoc1/skynet/nixos/-/settings/ci_cd as ``TOKEN`` and ``TOKEN_REMOTE`` \ No newline at end of file diff --git a/sync/repos.csv b/sync/repos.csv new file mode 100644 index 0000000..53ca74b --- /dev/null +++ b/sync/repos.csv @@ -0,0 +1,13 @@ +id_local, remote_url +4, https://gitlab.com/compsoc1/skynet/nixos.git +9, https://gitlab.com/compsoc1/skynet/ldap/backend.git +10, https://gitlab.com/compsoc1/skynet/ldap/frontend.git +13, https://gitlab.com/compsoc1/skynet/website/2023.git +14, https://gitlab.com/compsoc1/skynet/website/2016.git +17, https://gitlab.com/compsoc1/skynet/website/alumni-renew.git +18, https://gitlab.com/compsoc1/compsoc/constitution.git +20, https://gitlab.com/compsoc1/compsoc/presentations/presentations.git +21, https://gitlab.com/compsoc1/skynet/discord-bot.git +22, https://gitlab.com/compsoc1/skynet/scripts.git +29, https://gitlab.com/compsoc1/skynet/website/games.skynet.ie.git +44, https://gitlab.com/compsoc1/compsoc/presentations/python_catchup.git diff --git a/sync/sync.sh b/sync/sync.sh new file mode 100644 index 0000000..ba42cf4 --- /dev/null +++ b/sync/sync.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +Mirror-Clear(){ + # existing remotes + local id=$(tr -d '\n\t\r ' <<<"${1}" ) + local token=$(tr -d '\n\t\r ' <<<"$TOKEN" ) + local response=$(curl -s -X "GET" "https://gitlab.skynet.ie/api/v4/projects/$id/remote_mirrors" --header "PRIVATE-TOKEN: $token") + + # https://stackoverflow.com/a/67638584 + readarray -t my_array < <(jq -c '.[]' <<< $response) + # iterate through the Bash array + for item in "${my_array[@]}"; do + local id_mirror=$(jq --raw-output '.id' <<< "$item") + curl -s -X "DELETE" "https://gitlab.skynet.ie/api/v4/projects/$id/remote_mirrors/$id_mirror" --header "PRIVATE-TOKEN: $token" + done +} + +Mirror-Create(){ + # make sure the values are clean of extra characters + local id=$(tr -d '\n\t\r ' <<<"${1}" ) + local REPO_TMP=$(tr -d '\n\t\r ' <<<"${2}" ) + local REPO=${REPO_TMP#"https://"} + local token=$(tr -d '\n\t\r ' <<<"$TOKEN" ) + local token_remote=$(tr -d '\n\t\r ' <<<"$TOKEN_REMOTE" ) + + 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 + curl -s -X "POST" "$uri" --header "PRIVATE-TOKEN: $token" --data $body + # to put output on a new line + echo "" +} + +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 \ No newline at end of file