简体   繁体   English

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

[英]Python Git and Git-Lab create project and push code/commit

We are building an automation process using python in which we clone a base source code repository and add necessary changes to it and add the new code to a new git repository and push it to our private gitlab server.我们正在使用 python 构建一个自动化过程,在其中我们克隆一个基本源代码存储库并向其添加必要的更改,并将新代码添加到新的 git 存储库并将其推送到我们的私有 gitlab 服务器。

So far I'm using git library to clone, and initial a new repository and make an initial commit.到目前为止,我正在使用 git 库来克隆,并初始化一个新的存储库并进行初始提交。 however I'm not able to figure out "how to create and push the new repository to our private gitlab server.但是我无法弄清楚“如何创建新存储库并将其推送到我们的私有 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"""

How to create project and push the code to the new project?如何创建项目并将代码推送到新项目?

This is how I created a new project in gitlab in python using python-gitlab libary.这就是我使用 python-gitlab libary 在 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"})

The documentation of python-gitlab doesnt have any direct sample code for this use-case. python-gitlab 的文档没有针对此用例的任何直接示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM