第四章.python脚本gitLab多个账号下,所属源项目导入到目标项目

一、背景

备注:原创不易,里面都是技巧,请破费支持,若需技术支持,请留言,看到则回复!
日常工作中,代码管理会用到gitLab,而因种种原因,可能会涉及从gitLab将代码迁移的情景发生。针对这种情况,特整理如下代码迁移方案,该方案只针对代码从一个服务器gitLab迁移到其它gitLab的代码迁移方案。该方案利用python语言,python-gitlab库实现。该方案通过两步实现,第一步:获取源账号下所属的项目信息。第二步:将确认获取的信息导入到目标账号对应的组下。如果你已经操作第三章,第一步,则下面两步可以省略。

二、插件安装

  pip install python-gitlab pandas

API文档:
Deployments - python-gitlab v3.15.0

三、 登录源gitLab账号获取token

在这里插入图片描述
全部勾选,创建对应的token即可。同样目标账户也这样操作,并且,本地设置SSHKey,如下图所示
SSHkey设置

ssh-keygen -t rsa -C "你的邮箱"

上面,源账号和目标账号均要设置。设置完成之后,即可进入下一步,传输原理操作

四、传输原理

从一个账号,想要原封不动的将所有分支包括,提交记录信息也同步到新的分支,则最好基于**(command方式)** 进行代码迁移操作,核心如下:

#从源gitlib拉取裸仓库,并在本地文件系统创建gitbook-demo.git文件夹
git clone --bare ssh://git@sourcegitlab:port/group/gitproject-demo.git
#进入代码目录
cd gitproject-demo.git
#向新git推送镜像
git push --mirror ssh://git@newgitlab:port/group/gitproject-demo.git

–bare是裸仓库的意思,区别与git clone,他不是一个工作空间,不可以在目录下进行变更操作,即使操作了也不被接受, 执行命令后会复制全部的分支、标签,并且在命令执行过程中的所有远端的变动均被忽略。

–bare Make a bare Git repository. That is, instead of creating
and placing the administrative files in
/.git, make the itself the $GIT_DIR. This
obviously implies the --no-checkout because there is nowhere to check
out the working tree. Also the branch heads at the remote are copied
directly to corresponding local branch heads, without mapping them
torefs/remotes/origin/. When this option is used, neither
remote-tracking branches nor the related configuration variables are
created.

–mirror镜像代码仓库,完完整整的内容

Instead of naming each ref to push, specifies that all refs under
refs/ (which includes but is not limited to refs/heads/,
refs/remotes/, and refs/tags/) be mirrored to the remote repository.
Newly created local refs will be pushed to the remote end, locally
updated refs will be force updated on the remote end, and deleted refs
will be removed from the remote end. This is the default if the
configuration option remote…mirror is set.

五、代码实现

实现流程主要是,先基于第四步原理,将gitLab镜像文件导入到本地,然后再通过执行git push --mirror 镜像代码仓库的形式,将代码进行上传操作,下面token获取方式,第二步即可获取。

import gitlab
import pandas as pd
import os
import time

def get_projects_info(gl):
    """
    获取账号下所有项目列表信息
    """
    projects = gl.projects.list
  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值