Forking a GitHub repo using from the command line with bash, cURL, and the GitHub API

∥☆過路亽.° 提交于 2019-12-06 07:12:01

问题


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?)


回答1:


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).




回答2:


The documentation shows the owner and repo being part of the request URI

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

https://developer.github.com/v3/repos/forks/

This appears to work fine.




回答3:


It's much easier to create or fork a repo using the hub command line tool.

Installation instructions: https://github.com/github/hub#installation

It can do much more, but here's how to fork a online repo using the command line.
However, there is conflicting information out there, so it can be a bit confusing.

To fork a online repo owned by somebody else do:

  1. clone the repo using git clone

git clone ssh://git@github.com/keras-users/keras.git

  1. change into the cloned repo

cd keras

  1. create the fork online

hub fork

To fork your own repo that is already hosted on github:

Github doesn't allow you to fork your own repo,
So you need to first create a clone of your own repo on your computer,
Then you can create the cloned repo on the github website

  1. clone your repo to your computer

git clone ssh://git@github.com/alpha_beta_gamma/clone_repo.git

  1. clone the repo on your computer to another repo (also on your computer)

git clone clone_repo clone_repo2

  1. change directory to the new cloned repo

cd clone_repo2

  1. create the cloned repo on Github

git create

This will create a new repo on GitHub.

  1. set the URL of the remote repo:

git remote set-url https://github.com/username/clone_repo2

  1. push the local repo to the online repo

git push




回答4:


I use hub. I have also set git as alias for it so that I don't have to worry everytime what command comes under git and what under hub, so my code looks just like I an running git. Follow installations here.

$ git clone <github_repo>
$ cd <github_repo>
$ git fork

PS: For the above code to work, before executing it in your .bashrc or .bash_profile you need to put the following

alias git=hub



回答5:


I used this command to fork on github enterprise curl -vX POST https://git.redacted.com/api/v3/repos/<org-to-fork-from>/<repo-to-fork>/forks?access_token=<api-token> -d @gh-fork.json --header "Content-Type: application/json"

gh-fork.json is just

{ "organization": "org-to-fork-to", "description": "", "homepage": "https://git.redacted.com", "private": false }



来源:https://stackoverflow.com/questions/44274188/forking-a-github-repo-using-from-the-command-line-with-bash-curl-and-the-githu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!