如何在GitPython中创建Git Pull请求

14

我正在尝试在Jenkins作业中使用Python,该作业会下载并更新项目中的某一行代码,然后提交并创建拉取请求。我尽力阅读GitPython的文档,但我的智商不足以理解其中的内容。

import git
import os
import os.path as osp


path = "banana-post/infrastructure/"
repo = git.Repo.clone_from('https://github.myproject.git',
                           osp.join('/Users/monkeyman/PycharmProjects/projectfolder/', 'monkey-post'), branch='banana-refresh')
os.chdir(path)

latest_banana = '123456'
input_file_name = "banana.yml"
output_file_name = "banana.yml"
with open(input_file_name, 'r') as f_in, open(output_file_name, 'w') as f_out:
    for line in f_in:
        if line.startswith("banana_version:"):
            f_out.write("banana_version: {}".format(latest_banana))
            f_out.write("\n")
        else:
            f_out.write(line)
os.remove("deploy.yml")
os.rename("deploy1.yml", "banana.yml")
files = repo.git.diff(None, name_only=True)
for f in files.split('\n'):
    repo.git.add(f)
repo.git.commit('-m', 'This an Auto banana Refresh, contact bannana@monkey.com',
                author='moneky@banana.com')

在提交这个更改后,我正在尝试push这个更改并从branch='banana-refresh'创建一个pull requestbranch='banana-integration'


我不了解GitPython,但是你可以通过命令行完成:https://git-scm.com/docs/git-request-pull。我想GitPython只是其外层封装。 - SwiftsNamesake
也许这个 Git PR 包可以帮助你,它的链接在这里:https://github.com/Mergifyio/git-pull-request。 - Timo
3个回答

9

GitPython 只是 Git 的封装器。我假设您想在 Git 托管服务(Github/Gitlab 等)中创建一个 pull request。

你无法使用标准的 git 命令行创建 pull request。例如,git request-pull 只会生成未决更改的摘要,它不会在 GitHub 中创建 pull request。

如果你想在 GitHub 中创建一个 pull request,则可以使用 PyGithub 库。

或者使用 requests 库向 Github API 发送简单的 HTTP 请求:

import json
import requests

def create_pull_request(project_name, repo_name, title, description, head_branch, base_branch, git_token):
    """Creates the pull request for the head_branch against the base_branch"""
    git_pulls_api = "https://github.com/api/v3/repos/{0}/{1}/pulls".format(
        project_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json"}

    payload = {
        "title": title,
        "body": description,
        "head": head_branch,
        "base": base_branch,
    }

    r = requests.post(
        git_pulls_api,
        headers=headers,
        data=json.dumps(payload))

    if not r.ok:
        print("Request Failed: {0}".format(r.text))

create_pull_request(
    "<your_project>", # project_name
    "<your_repo>", # repo_name
    "My pull request title", # title
    "My pull request description", # description
    "banana-refresh", # head_branch
    "banana-integration", # base_branch
    "<your_git_token>", # git_token
)

这个功能使用GitHub OAuth2 Token AuthGitHub pull request API endpoint,用于将分支banana-refresh创建一个针对banana-integration的拉取请求。


2
请求失败:必须启用Cookie才能使用GitHub。当我使用这个脚本时会出现这个错误。 - Aditya Sinha

3

看起来这个库没有包装拉取请求。

您可以根据文档直接调用git命令行

repo.git.pull_request(...)


我在文档中找不到这个命令,你能否更具体地说明参数,例如feature_branch和target_branch应该放在哪里? - Shek
1
该命令不受GitPython支持。您需要想办法使用Git CLI(第一个链接)运行该命令,并使用您的参数将其转换为GitPython直接调用(第二个链接)。 - Milk

1
我已经按照frederix的答案操作,并使用了https://api.github.com/repos/而不是https://github.com/api/v3/repos/。
同时,使用owner_name而不是project_name。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接