Python Git和Git- lab创建项目和推送代码/提交



我们正在使用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"""

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

这是我如何使用python-gitlab库在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的文档没有任何直接的示例代码对于这个用例。

最新更新