Created
November 22, 2016 13:45
-
-
Save stalniy/6990dd37f0ae91f00a5640c12669479f to your computer and use it in GitHub Desktop.
Creates and pushes projects from one gitlab to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Put proper URLs here | |
SOURCE_GITLAB_URL=http://source.example.net/api/v3/ | |
DESTINATION_GITLAB_URL=http://dest.example.net/api/v3/ | |
[email protected] | |
SOURCE_PRIVATE_KEY= | |
DESTINATION_PRIVATE_KEY= | |
TMP_DIR=`mktemp -d` | |
read -s -p "Enter Your Private-Token for gitlab which you want to export: " SOURCE_PRIVATE_KEY | |
echo "" | |
read -s -p "Enter Your Private-Token for gitlab which you want to sync: " DESTINATION_PRIVATE_KEY | |
projects_of() { | |
URL=$1 | |
PROJECT_GROUP=$2 | |
PRIVATE_KEY=$3 | |
response=`curl -k -H "PRIVATE-TOKEN: ${PRIVATE_KEY}" "${URL}/projects?per_page=100&simple=1&archived=0"` | |
if [ "$?" != "0" ]; then | |
echo "Unable to retrieve projects from: ${SOURCE_GITLAB_URL}" | |
echo "Response: ${response}" | |
return $? | |
fi | |
echo ' | |
const projects = '$response'.filter(project => project.http_url_to_repo.indexOf("/'$PROJECT_GROUP'/") !== -1); | |
console.log( | |
projects.map(p => { | |
const name = p.name.trim().replace(/\s+/g, "-").replace(/-+/g, "-") | |
return [name, p.ssh_url_to_repo].join("|") | |
}).join("\n") | |
)' | node | |
} | |
get_group_id() { | |
name=$1 | |
url=$2 | |
token=$3 | |
group=`curl -k -H "PRIVATE-TOKEN: ${token}" "$url/groups/$name"` | |
echo "console.log(${group}.id)" | node | |
} | |
create_project() { | |
DEST_URL=$1 | |
PRIVATE_KEY=$3 | |
IFS='|' read project_group project_name project_url <<< "$2" | |
project_code="${project_url#*/}" | |
group_id=`get_group_id "$project_group" "$DEST_URL" "$PRIVATE_KEY"` | |
curl -X POST -k -H "PRIVATE-TOKEN: ${PRIVATE_KEY}" -H "Content-Type: application/json" -d '{"name": "'$project_name'", "namespace_id": '$group_id', "visibility_level": 10, "default_branch": "master", "path": "'${project_code/.git/}'" }' "${DEST_URL}/projects" | |
echo "" | |
echo "clone: $project_url" | |
git clone --bare "$project_url" "$TMP_DIR/$project_code" && cd "$TMP_DIR/$project_code" | |
if [ "$?" = "0" ]; then | |
remote_url="$DESTINATION_GIT_URL:${project_group}/${project_code}" | |
echo "add remote: $remote_url" | |
git remote rm gitlab 2> /dev/null | |
git remote add gitlab "$remote_url" | |
git push -f gitlab master | |
git push --all | |
git push --tags gitlab | |
else | |
echo "Unable to clone or navigate $project_url" | |
fi | |
} | |
for project in `projects_of "$SOURCE_GITLAB_URL" "project-x" "$SOURCE_PRIVATE_KEY"`; do | |
echo "create project: $project" | |
create_project "$DESTINATION_GITLAB_URL" "project-x|$project" "$DESTINATION_PRIVATE_KEY" | |
if [ "$?" != "0" ]; then | |
echo "Aborted due to errors above" | |
exit 1 | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment