简体   繁体   English

如何使用 GitHub API 以编程方式创建更改文件的拉取请求

[英]How to programatically create pull request with changed file using GitHub API

How can I create PR on Github using their API?如何使用 API 在 Github 上创建 PR? Let's imagine I have package.json file as a string and I want to make changes to this file.假设我有package.json文件作为字符串,我想对此文件进行更改。 So I parse it, make changes to it and then what exactly I need to do to make it look like I made those changes locally after checking out the new branch, making changes, commiting them to new branch and the pushing them to remote?所以我解析它,对其进行更改,然后我需要做什么才能使它看起来像我在签出新分支后在本地进行了这些更改,进行更改,将它们提交到新分支并将它们推送到远程? I see that they have POST API endpoint to create a commit and POST API endpoint to create a PR, but I don't see where I put the file I changed.我看到他们有 POST API 端点来创建提交和 POST API 端点来创建 PR,但我看不到我将更改的文件放在哪里。

Using something like GitJS could be a solution.使用像GitJS这样的东西可能是一个解决方案。

This library is a simple wrapper around the git command line.这个库是 git 命令行的简单包装器。

Instead of using the Github API you could just work the git commands programatically with a library like this.除了使用 Github API 之外,您还可以使用这样的库以编程方式使用 git 命令。

That way you don't have to work with the much more complicated API and have the benefit of supporting other source control sites too.这样您就不必使用更复杂的 API 并且还可以支持其他源代码控制站点。

Looking at their example we can see it's incredibly easy to commit and push a file using javascript:查看他们的示例,我们可以看到使用 javascript 提交和推送文件非常容易:

require('simple-git')()
   .add('./*')
   .commit("first commit!")
   .addRemote('origin', 'some-repo-url')
   .push(['-u', 'origin', 'master'], () => console.log('done'));

Make sure you refer to the usage documentation before trying that example to ensure you configure everything correctly.请确保在尝试该示例之前参考使用文档,以确保正确配置所有内容。

Alternatively, you can use the package Octokit to more easily interface with the Github API.或者,您可以使用 package Octokit更轻松地与 Github API 连接。

When adding, committing and pushing a file via the API you must first start by creating a tree.通过 API 添加、提交和推送文件时,您必须首先创建一个树。 Then you use that tree as part of the commit request, update the references and finally push the commit.然后将该树用作提交请求的一部分,更新引用并最终推送提交。

You can find a working example on this Github issue .您可以在此 Github 问题上找到一个工作示例。

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

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