Merge branch '#50_mirror_using_gitlab.com' into 'main'

feat: setup script to sync repos

Closes #50

See merge request compsoc1/skynet/nixos!29
This commit is contained in:
silver 2023-12-27 22:22:17 +00:00
commit 1057bae86e
5 changed files with 89 additions and 2 deletions

View file

@ -1,7 +1,7 @@
# borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml # borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml
stages: stages:
- flake - misc
- test - test
- deploy - deploy
- deploy_gitlab - deploy_gitlab
@ -11,7 +11,7 @@ stages:
# $PACKAGE_NAME = name of the flake that needs to be updated # $PACKAGE_NAME = name of the flake that needs to be updated
# $UPDATE_FLAKE = flag to update the flake # $UPDATE_FLAKE = flag to update the flake
update: update:
stage: flake stage: misc
tags: tags:
- nix - nix
# from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5 # from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5
@ -40,6 +40,17 @@ update:
variables: variables:
- $UPDATE_FLAKE == "yes" - $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 .scripts_base: &scripts_base
# load nix environment # load nix environment
- git pull origin $CI_COMMIT_REF_NAME - git pull origin $CI_COMMIT_REF_NAME

2
sync/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.idea
.env

16
sync/README.md Normal file
View file

@ -0,0 +1,16 @@
# Repo Sync
This subdir is intended for syncing repos on <gitlab.skynet.ie> with <gitlab.com>
## 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``

13
sync/repos.csv Normal file
View file

@ -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
1 id_local remote_url
2 4 https://gitlab.com/compsoc1/skynet/nixos.git
3 9 https://gitlab.com/compsoc1/skynet/ldap/backend.git
4 10 https://gitlab.com/compsoc1/skynet/ldap/frontend.git
5 13 https://gitlab.com/compsoc1/skynet/website/2023.git
6 14 https://gitlab.com/compsoc1/skynet/website/2016.git
7 17 https://gitlab.com/compsoc1/skynet/website/alumni-renew.git
8 18 https://gitlab.com/compsoc1/compsoc/constitution.git
9 20 https://gitlab.com/compsoc1/compsoc/presentations/presentations.git
10 21 https://gitlab.com/compsoc1/skynet/discord-bot.git
11 22 https://gitlab.com/compsoc1/skynet/scripts.git
12 29 https://gitlab.com/compsoc1/skynet/website/games.skynet.ie.git
13 44 https://gitlab.com/compsoc1/compsoc/presentations/python_catchup.git

45
sync/sync.sh Normal file
View file

@ -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