0

我们正在使用 python 构建一个自动化过程,其中我们克隆了一个基本源代码存储库并对其添加必要的更改,并将新代码添加到新的 git 存储库并将其推送到我们的私有 gitlab 服务器。

到目前为止,我正在使用 git 库进行克隆,并初始化一个新的存储库并进行初始提交。但是我无法弄清楚“如何创建新存储库并将其推送到我们的私有 gitlab 服务器。

import gitlab
import git
import json
import shutil
"""GitLab API"""
gl = gitlab.Gitlab('gitlab url of company', private_token='the token', api_version=4)
gl.auth()

"""Cloning the Base Code"""
git.Repo.clone_from("url","path to save")


"""Getting data"""
data_json = getData()

""" Copy Base Code to New Folder with Doctor Name"""
repo_name = "new_repo"

shutil.copytree("./base_code/public", "{}/public".format(repo_name))
shutil.copytree("./base_code/src", "{}/src".format(repo_name),dirs_exist_ok=True)
shutil.copy("./base_code/.gitignore", "{}/".format(repo_name))
shutil.copy("./base_code/package-lock.json", "{}/".format(repo_name))
shutil.copy("./base_code/package.json", "{}/".format(repo_name))
shutil.copy("./base_code/README.md", "{}/".format(repo_name))

"""Generate JSON File and save it new folder"""

with open("./{}/src/data.json".format(repo_name), 'w') as fout:
    json_dumps_str = json.dumps(data_json, indent=4)
    print(json_dumps_str, file=fout)

"""Create new git repo in the new folder"""
new_repo = git.Repo.init('{}'.format(repo_name))

"""Adding all the files to Staged Scenario"""    
new_repo.index.add(['.'])

"""Commit the changes"""
new_repo.index.commit('Initial commit.')

"""Create Project of the new Repository"""

如何创建项目并将代码推送到新项目?

4

1 回答 1

0

这就是我使用 python-gitlab 库在 python 中的 gitlab 中创建一个新项目的方式。

gl = gitlab.Gitlab('gitlab website url', private_token='token', api_version=4)
gl.auth()
gl.projects.list()

"""Create Project of the new Repository"""
response = gl.projects.create({"name":"project name","namespace_id":"group-id if required"})

python-gitlab 的文档没有此用例的任何直接示例代码。

于 2021-10-17T16:00:46.283 回答