Skip to content

Instantly share code, notes, and snippets.

@derme302
Created November 13, 2023 07:04
Show Gist options
  • Save derme302/1ddcf55a6f9b2604f555492052f0aa69 to your computer and use it in GitHub Desktop.
Save derme302/1ddcf55a6f9b2604f555492052f0aa69 to your computer and use it in GitHub Desktop.
[Bing AI/ChatGPT] Mirror a GitLab group to a GitLab group on another instance
import gitlab
from git import Repo
import subprocess
# Source GitLab instance
source_url = 'https://source.gitlab.com'
source_token = 'your-source-token'
source_group_id = 1234
# Destination GitLab instance
destination_url = 'https://destination.gitlab.com'
destination_token = 'your-destination-token'
destination_group_id = 5678
# Connect to GitLab instances
source_gl = gitlab.Gitlab(source_url, private_token=source_token)
destination_gl = gitlab.Gitlab(destination_url, private_token=destination_token)
# Get source group and destination group
source_group = source_gl.groups.get(source_group_id)
destination_group = destination_gl.groups.get(destination_group_id)
# Get projects in source group
projects = source_group.projects.list(all=True)
# Clone each project and push to destination group
for project in projects:
# Clone project
repo = Repo.clone_from(project.http_url_to_repo, project.name)
# Push to destination group
new_project = destination_group.projects.create({'name': project.name})
new_project.repository.create_file(
'README.md',
'Initial commit',
'Hello World!'
)
origin = repo.remote(name='origin')
destination = repo.create_remote('destination', new_project.http_url_to_repo)
destination.push(refspec='refs/remotes/origin/*:refs/heads/*')
# Push LFS objects to destination group
subprocess.run(['git', 'lfs', 'fetch', '--all'], cwd=project.name)
subprocess.run(['git', 'lfs', 'push', '--all', 'destination'], cwd=project.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment