使用bash,cURL和GitHub API从命令行分叉GitHub repo [英] Forking a GitHub repo using from the command line with bash, cURL, and the GitHub API

查看:177
本文介绍了使用bash,cURL和GitHub API从命令行分叉GitHub repo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的bash脚本无法使用cUrl分叉GitHub库。

创建一个分支



我已经尝试了很多变体:

curl -u $ my_user_name https: //api.github.com/repos/forks -d{\owner \:\$ upstream_repo_username \,\repo \:\$ upstream_repo_name \}





curl -u $ my_user_name https://api.github.com / repos /'$ upstream_repo_username'/'$ upstream_repo_name'/ forks



产生以下错误:

{
message:Not Found,
documentation_url:https://developer.github.com/v3
}



在对比中,如下所示创建一个新的空github回购库:

curl -u $ my_user_name https://api.github.com/user/repos -d{\name \:\$ upstream_repo_name\}



关于如何从命令行创建一个repo分支的想法?

我有一个bash脚本:
- 用nam在github上创建一个空的回购回购的e要克隆,
- 克隆本地其他用户的回购,
- 将我的克隆回购推入我在github帐户中创建的空回购
- 集适当的起源和上游远程交易

然而,这种方法并没有将GitHub内的连接保留到源(分叉)回购。我特别喜欢分支链接的便利性,出现在我自己的repo名称下面; - )



目标是从命令行执行所有我的克隆(和分支)。

我不想打开浏览器,导航到我希望分叉的存储库,只需访问该分叉按钮即可。只返回到命令行来完成这个过程。



或者,我可以从命令行将克隆的repo变成分叉的repo吗? (即一些命令行API命令,将重新创建那些叉子拥有的内部github链接?)

解决方案

这是我的工作bash脚本:

  curl -u $ my_user_name https://api.github.com/repos/$upstream_repo_username/$upstream_repo_name/ forks -d''

使用硬编码字符串而非bash变量的示例:

  curl -u'SherylHohman'https://api.github.com/repos/octocat/Hello-World/forks -d''

注意我将 -d''移至末尾以避免登录错误。

请求需要身份验证。

我通过curl的 -u 参数提供这个参数(与使用OAuth2)。
当我使用 -u $ my_user_name 选项时,

我必须将 -d''之后 URI
- 如果放置在 -u'usernam之间,则会导致登录错误e'和URI。



结果发现我的脚本中的主要错误是bash语法。

我有引号围绕bash变量,应该不会在那里。

(..just在没有真正知道bash或curl的情况下解决一个痛点)



另外,正如#YuriSchimke指出的那样,这个特定的URI需要在​​URI中传递参数。将这些选项作为json传递并不是一个选项,与创建新空白的repo的URI不同。



这就是为什么我对如何在URI中发送这些数据感到困惑的原因:

使用curl, 默认请求是GET

curl,POST请求是通过添加 -d (相当于 - data )标志,后面是要发送的数据。

我需要发送POST请求。

GitHub API的格式是GET(和POST 例如CreateRepo )请求有时可以一些参数作为json或查询字符串发送

注意:GitHub API的文档看起来有点不完整,因为我没有看到提及允许json的API,只有查询字符串。

我想在这种情况下,数据夹在两个静态URI部分之间,这使得无法发送json值。



我不知道如何使用 -d flag without data

如果我简单地将它关闭,则API调用将作为GET处理。

它返回了有关 我想要分叉的回购信息,
而不是分叉回购到我的账户。


@ YuriSchimke's 帖子给出了我说阿哈!。谢谢!我在笑,它并没有跨过我的脑海。我很感激尤里的做法如此明显! (再次感谢)。

I am having trouble with my bash script to fork a GitHub repo using cUrl.
The gitHub API doc for creating a fork.

I've tried many variations:

curl -u $my_user_name https://api.github.com/repos/forks -d "{\"owner\":\"$upstream_repo_username\",\"repo\":\"$upstream_repo_name\"}"

and
curl -u $my_user_name https://api.github.com/repos/'$upstream_repo_username'/'$upstream_repo_name'/forks

yield the following error: { "message": "Not Found", "documentation_url": "https://developer.github.com/v3" }

In Contrast, the following Creates a new empty github repo, as expected:
curl -u $my_user_name https://api.github.com/user/repos -d "{\"name\":\"$upstream_repo_name\"}"

Any ideas on how to create a fork of a repo from the command line?

I have a bash script that: - creates an empty repo on github with the name of the repo I'm going to clone, - clones a repo from another user locally, and - pushes my cloned repo into the empty repo I created in my github account - sets origin and upstream remotes appropriately

However, this method does not keep a connection within GitHub to the source (forked) repo. I particularly like the convenience of the forked link appearing below my own repo name ;-)

The goal is to do all my cloning (and forking) from the command line.

I do not want to open a browser, navigate to the repository I wish to fork, just to access that "Fork" button.. only return back to the command line to finish the process.

Alternatively, can I turn a cloned repo into a forked one from the command line? (ie some command line api command that will re-create those internal github links that forks possess?)

解决方案

Here is my working bash script:

curl -u $my_user_name https://api.github.com/repos/$upstream_repo_username/$upstream_repo_name/forks -d ''

Example using hard-coded strings instead of bash variables:

curl -u 'SherylHohman' https://api.github.com/repos/octocat/Hello-World/forks -d ''

Notice I moved -d '' to the end to avoid login errors.
The request requires authentication.
I provide this via curl's -u parameter (as opposed to using OAuth2).
When I used the -u $my_user_name option,
I had to move the -d '' to after the URI
- it resulted in login errors if placed between -u 'username' and the URI.

It turns out the Main source of errors in my script with bash-syntax.
I had quotation marks surrounding bash variables, that should Not have been there.
(..just Solving a pain point without really knowing bash or curl)

Additionally, as #YuriSchimke pointed out, this particular URI required parameters to be passed in the URI. Passing these options as json is not an option, unlike the URI for Creating a New Blank repo.

Here is why I was baffled over how to send this data in the URI:

Using curl, the default request is a GET.
In curl, POST requests are made by adding the -d (equivalent to --data) flag followed by the data to be sent.

I needed to send a POST request.
The format for GitHub API is that GET (and POST eg. CreateRepo) requests can sometimes send some parameters as json or query strings
NOTE: documentation for GitHub API appears to be slightly incomplete, as I do not see any mention of the API allowing json, only query string.
I suppose in this case, the data is sandwiched between two static URI parts, making it impossible to send as json values.

I was at a loss how to use the -d flag without data:

If I simply left it off, the API call was processed as a GET.
It returned information about the repo I wanted to fork,
instead of forking the repo to my account.

@YuriSchimke's post gave me that "Ahaa!". Thanks! I'm laughing that it didn't cross my mind. I'm grateful Yuri's made this so obvious! (Thanks Again).

这篇关于使用bash,cURL和GitHub API从命令行分叉GitHub repo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆