How to retrieve the list of all GitHub repositories of a person?
Solution 1:
You can use the github api for this. Hitting https://api.github.com/users/USERNAME/repos
will list public repositories for the user USERNAME.
Solution 2:
Use the Github API:
/users/:user/repos
This will give you all the user's public repositories. If you need to find out private repositories you will need to authenticate as the particular user. You can then use the REST call:
/user/repos
to find all the user's repos.
To do this in Python do something like:
USER='AUSER'
API_TOKEN='ATOKEN'
GIT_API_URL='https://api.github.com'
def get_api(url):
try:
request = urllib2.Request(GIT_API_URL + url)
base64string = base64.encodestring('%s/token:%s' % (USER, API_TOKEN)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
result.close()
except:
print 'Failed to get api request from %s' % url
Where the url passed in to the function is the REST url as in the examples above. If you don't need to authenticate then simply modify the method to remove adding the Authorization header. You can then get any public api url using a simple GET request.
Solution 3:
Try the following curl
command to list the repositories:
GHUSER=CHANGEME; curl "https://api.github.com/users/$GHUSER/repos?per_page=100" | grep -o 'git@[^"]*'
To list cloned URLs, run:
GHUSER=CHANGEME; curl -s "https://api.github.com/users/$GHUSER/repos?per_page=1000" | grep -w clone_url | grep -o '[^"]\+://.\+.git'
If it's private, you need to add your API key (access_token=GITHUB_API_TOKEN
), for example:
curl "https://api.github.com/users/$GHUSER/repos?access_token=$GITHUB_API_TOKEN" | grep -w clone_url
If the user is organisation, use /orgs/:username/repos
instead, to return all repositories.
To clone them, see: How to clone all repos at once from GitHub?
See also: How to download GitHub Release from private repo using command line
Solution 4:
Here is a full spec for the repos API:
https://developer.github.com/v3/repos/#list-repositories-for-a-user
GET /users/:username/repos
Query String Parameters:
The first 5 are documented in the API link above. The parameters for page
and per_page
are documented elsewhere and are useful in a full description.
-
type
(string): Can be one ofall
,owner
,member
. Default:owner
-
sort
(string): Can be one ofcreated
,updated
,pushed
,full_name
. Default:full_name
-
direction
(string): Can be one ofasc
ordesc
. Default:asc
when usingfull_name
, otherwisedesc
-
page
(integer): Current page -
per_page
(integer): number of records per page
Since this is an HTTP GET API, in addition to cURL, you can try this out simply in the browser. For example:
https://api.github.com/users/grokify/repos?per_page=2&page=2