GitHub
The git URL is then git@github.com:userid/reponame.git
Pull request refs
GitHub stores refs to pull requests on the original repository under refs/pull/<number>/head
.
Therefore, if you want to get a pull request locally to try it out on a local branch you can do:
git fetch origin pull/<pr-number>/head
git checkout -b <local-branch-name> FETCH_HEAD
GitHub also offers the merge request directly at refs/pull/<pull-request-id>/merge
.
Create branches for all pull requests on git fetch
:
git config --local --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'
GitHub API v3 via cURL
GitHub has an HTTP REST API, which allows you to:
-
programmatically access and modify GitHub data
-
overcome certain web interface limitations.
For example, on the web interface, you can only see up to 30 results for the starred repos of other people.
With the API, you can get all of them at once and grep away by playing with
per_page
.
curl
is a convenient way to use the API manually.
Vars:
USER=user
REPO=repo
PASS=
GET is the default request type:
curl https://api.github.com/users/$USER/starred?per_page=9999909 | grep -B1 "description" | less
Make a POST request with curl
:
echo '{
"text": "Hello world github/linguist#1 **cool**, and #1!",
"mode": "gfm",
"context": "github/gollum"
}' | curl --data @- https://api.github.com/markdown
GitHub API Authentication
Many methods that take a user can use the authenticated user instead if present.
Basic with user password pair:
curl -u 'cirosantilli' https://api.github.com/user/orgs
Or:
curl -u 'cirosantilli:password' https://api.github.com/user/orgs
GitHub API OAuth
OAuth: generate a large random number called the access token. Which you can only get once.
There are two ways to get the token:
-
personal tokens, generated by a logged in user from: https://github.com/settings/tokens/new
Useful if you need to generate only a few tokens for personal use.
-
application obtained token.
A way for applications to interact with GitHub and obtain a token.
User is first redirected to GitHub, inputs his password only there, and the token is sent back to the application.
Useful if you are building an application that must interact with GitHub, and don’t want to store the user’s password.
Each user gets a single token per application in case multiple token requests are made.
Tokens are safer than storing the password directly because:
- it is possible to restrict what can be done with each token, thus increasing confidence users have on your application.
- users can revoke tokens at any time, without changing their passwords.
Once you get the token, make an authenticated request with:
curl https://api.github.com/user?access_token=$TOKEN
or:
curl -H "Authorization: token $TOKEN" https://api.github.com
Rate limiting
- authenticated: 60 requests per hour
- unauthenticated requests: 5000 requests per hour
http://developer.github.com/v3/#rate-limiting
per_page
Get given number of results. Default is 30. Allows you to beat web API limitations. List all starred repos of a user:
curl https://api.github.com/users/$USER/starred?per_page=9999909 | grep -B1 "description" | less
Get repo info
Lots of info:
curl -i https://api.github.com/users/$USER/repos
Create repo
USER=
REPO=
curl -u "$USER" https://api.github.com/user/repos -d '{"name":"'$REPO'"}'
Repo name is the very minimal you must set, but you could also set other params such as:
curl -u "$USER" https://api.github.com/user/repos -d '{
"name": "'"$REPO"'",
"description": "This is your first repo",
"homepage": "https://github.com",
"private": false,
"has_issues": true,
"has_wiki": true,
"has_downloads": true
}'
Its just JSON (remember, last item cannot end in a comma).
Delete repo
curl -u "$USER" -X DELETE https://api.github.com/repos/$USER/$REPO
Careful, it works!
hub
Powerful CLI interface: https://github.com/github/hub
gem install hub
Open URL of current branch / commit in browser:
hub browse
Create repository with same name as current dir:
hub create
Give a name and a description:
hub create name -d 'Description'