import requests
if __name__ == "__main__":
headers = {"Private-Token": "XkKw4nD1z2eZSSZ6Ax7Q"}
pages = 74
pageSize = 20
for i in range(pages):
page = i + 1
url = f"http://git.dev.xx.com/api/v4/projects?per_page={pageSize}&page={str(page)}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(response.text)
else:
records = response.json()
for rec in records:
print(rec["namespace"]["full_path"], rec["name"], rec["web_url"], rec["ssh_url_to_repo"],
rec["http_url_to_repo"])
if page >= pages:
print(f"最后一页[{page}]已结束")
输出各个项目信息,其中http_url_to_repo
和ssh_url_to_repo
对应不同类型仓库地址。
git clone --mirror <源仓库URL>
git remote add <目标仓库名称> <目标仓库URL>
git push --mirror <目标仓库名称>
综上可得:
import subprocess
import requests
origin_access_token = "XkKw4nD1z2eZSSZ6Ax7Q"
target_git_base_url = ""
def get_origin_git_repo_urls():
headers = {"Private-Token": f"{origin_access_token}"}
pages = 74
page_size = 20
for i in range(pages):
page = i + 1
git_url = f"http://git.dev.xx.com/api/v4/projects?per_page={page_size}&page={str(page)}"
response = requests.get(git_url, headers=headers)
if response.status_code != 200:
print(response.text)
else:
records = response.json()
for rec in records:
#print(rec["namespace"]["full_path"], rec["name"], rec["web_url"], rec["ssh_url_to_repo"], rec["http_url_to_repo"])
trans_git(rec["namespace"]["full_path"], rec["name"], rec["http_url_to_repo"])
if page >= pages:
print(f"最后一页[{page}]已结束")
def proc_group(path):
# TODO
pass
def proc_repo(group_path, repo_name, repo_url):
# TODO
pass
# 执行linux命令
def proc_command(command):
# 使用subprocess执行Linux命令
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 等待命令执行完成
process.wait()
# 获取命令的返回值
return_code = process.returncode
# 判断命令是否执行成功
if return_code == 0:
print(f"命令执行成功:{command}")
return True
else:
print(f"命令执行失败:{command}")
return False
# 迁移操作
def trans_git(origin_group_path, origin_repo_name, origin_git_url):
# git clone命令
clone_command = f'git clone --mirror {origin_git_url}'
if not proc_command(clone_command):
return
if not proc_group(origin_group_path):
return
repo_url = f"{target_git_base_url}/{origin_group_path}/{origin_repo_name}.git"
if not proc_repo(origin_group_path, origin_repo_name, repo_url):
return
cd_in_command = f'cd {origin_repo_name}'
if not proc_command(cd_in_command):
return
push_command = f'git push --mirror {repo_url}'
proc_command(push_command)
cd_out_command = f'cd ..'
proc_command(cd_out_command)