From 0e283fad0fc7f511928b9a38f73eaabe85818fe8 Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Wed, 7 Apr 2021 16:23:10 -0400 Subject: [PATCH 1/3] Add user mapping feature Signed-off-by: Francis Giraldeau --- migrate.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) mode change 100644 => 100755 migrate.sh diff --git a/migrate.sh b/migrate.sh old mode 100644 new mode 100755 index d432fca..3c63c3b --- a/migrate.sh +++ b/migrate.sh @@ -21,6 +21,12 @@ printf "Migrate badges? (yes/no): " read -r MIGRATE_BADGES printf "Migrate hooks? (yes/no): " read -r MIGRATE_HOOKS +printf "Apply user mapping? (yes/no): " +read -r USER_MAPPING +if [[ "$USER_MAPPING" == "yes" ]]; then + printf "User mapping file: " + read -r USER_MAPPING_FILE +fi #SOURCE_PATH="" #TARGET_PATH="" #ARCHIVE_AFTER_MIGRATION="no" @@ -30,6 +36,8 @@ read -r MIGRATE_HOOKS #MIGRATE_PROJECT_VARIABLES="no" #MIGRATE_BADGES="no" #MIGRATE_HOOKS="no" +#USER_MAPPING="no" +#USER_MAPPING_FILE="user-mapping.txt" CURL_PARAMS="" unset -v sourceGitlabPrivateAccessToken targetGitlabPrivateAccessToken @@ -288,6 +296,11 @@ function migrateProject() { echo " Done ${exportStatus[1]}" local fileName fileName=$(downloadFile "${exportStatus[1]}") + + if [[ "$USER_MAPPING" == "yes" ]]; then + modifyUserMapping "${USER_MAPPING_FILE}" "${fileName}" + fi + importProject "${project}" "${fileName}" break fi @@ -314,6 +327,30 @@ function downloadFile () { echo "$tempPath" } +function modifyUserMapping () { + local userMappingFile=$1 + local archiveFile=$2 + local workDir="work" + + mapfile -t userMap < ${userMappingFile} + + mkdir -p ${workDir} + tar -C ${workDir} -xzf "$archiveFile" + + projectMembersFile=${workDir}/tree/project/project_members.ndjson + tmpFile="$(basename ${projectMembersFile}).tmp" + + for item in "${userMap[@]}"; do + read -r search replace <<< "$item" + jq -c --arg search ${search} --arg replace ${replace} \ + '. | if (.user.email | test($search; "i")) then .user.email |= $replace else . end' \ + ${projectMembersFile} > ${tmpFile} && \ + mv ${tmpFile} ${projectMembersFile} + done + + tar -C ${workDir} -czf "${archiveFile}" . +} + function importProject () { local project=$1 local fileName=$2 From 4832891da39e9ecf08f55e630ee19456204f320d Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Wed, 7 Apr 2021 16:45:02 -0400 Subject: [PATCH 2/3] Add doc on user mapping feature Signed-off-by: Francis Giraldeau --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index a66117b..3798725 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,19 @@ You can always change the settings back after the migration manually or edit the * Add link to new location in source project description * Archive source projects after migration +## User mapping ## + +Gitlab use email to match users between instances. If the username are not the same between instances but emails are the same, then all user associations are preserved. However, if users email changed, associations are lost after import. You can fix this issue by supplying a user mapping file to make the correspondance. + +First, create a `user-mapping.txt` file containing the old and new email address, one record per line, like this: + +``` +alice@example.com alice.foo@example.com +bob@example.com bob.bar@example.com +``` + +To enable user mapping, set the variables `USER_MAPPING=true` and `USER_MAPPING_FILE=user-mapping.txt`. After downloading the export archive, the file `project_members.ndjson` inside it is modified with the new emails. The case is non-sensitive. The importation proceeds as usual. + # License # Copyright (c) 2020 K - Mail Order GmbH & Co. KG From 5ed0c187075dd3c8c08c90a162ee97555af327a7 Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Wed, 7 Apr 2021 17:00:45 -0400 Subject: [PATCH 3/3] Delete workDir after use Otherwise, archive might contain unrelated files from previous runs. Signed-off-by: Francis Giraldeau --- migrate.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/migrate.sh b/migrate.sh index 3c63c3b..dbaa960 100755 --- a/migrate.sh +++ b/migrate.sh @@ -349,6 +349,7 @@ function modifyUserMapping () { done tar -C ${workDir} -czf "${archiveFile}" . + rm -rf ${workDir} } function importProject () {