Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Created June 7, 2013 23:17
Show Gist options
  • Save ralphbean/5733076 to your computer and use it in GitHub Desktop.
Save ralphbean/5733076 to your computer and use it in GitHub Desktop.
Script to list all repos for a github organization
#!/usr/bin/env python
""" Print all of the clone-urls for a GitHub organization.
It requires the pygithub3 module, which you can install like this::
$ sudo yum -y install python-virtualenv
$ mkdir scratch
$ cd scratch
$ virtualenv my-virtualenv
$ source my-virtualenv/bin/activate
$ pip install pygithub3
Usage example::
$ python list-all-repos.py
Advanced usage. This will actually clone all the repos for a
GitHub organization or user::
$ for url in $(python list-all-repos.py); do git clone $url; done
"""
import pygithub3
gh = None
def gather_clone_urls(organization, no_forks=True):
all_repos = gh.repos.list(user=organization).all()
for repo in all_repos:
# Don't print the urls for repos that are forks.
if no_forks and repo.fork:
continue
yield repo.clone_url
if __name__ == '__main__':
gh = pygithub3.Github()
clone_urls = gather_clone_urls("FOSSRIT")
for url in clone_urls:
print url
@bhaktipriya
Copy link

I am getting results only when I type in a "users" name. No results with organizations. Can you please help?

@bhaktipriya
Copy link

For the latest version use:
gh.repos.list_by_org('FOSSRIT',type='all').all()

Copy link

ghost commented May 17, 2017

repo.ssh_url for ssh url

@kcbaltz
Copy link

kcbaltz commented Aug 10, 2017

If you need to list private repos, use bhaktipriya's change and update this:
gh = pygithub3.Github()

to

gh = pygithub3.Github(token="abc123") # where abc123 is an OAUTH token for GH

@tomwj
Copy link

tomwj commented Sep 6, 2018

Thanks

@itsmekarthikreddy
Copy link

Hi , I am trying to list for my private organization repos

i get the error
<
Traceback (most recent call last):
File "C:/pycharm/list_all_repos.py", line 35, in
for url in clone_urls:
File "C:/pycharm/list_all_repos.py", line 21, in gather_clone_urls
all_repos = gh.repos.list(user=organization).all()
AttributeError: 'Github' object has no attribute 'repos'>

Copy link

ghost commented Nov 29, 2018

This can be handled much "easier". A quick hack, taken from an case in a shell script:

printf '%s\n' $(curl https://api.github.com/users/$2/repos | grep 'html_url' | awk '{print $2}' | sed 's/"//g' | sed 's/,//g' | sort | uniq -u)

@Singhal-Vaibhav
Copy link

If you have jq:

curl -s https://api.github.com/orgs/<org_name>/repos?per_page=200 | jq '.[]|.html_url'

@RichardBronosky
Copy link

RichardBronosky commented Dec 19, 2018

With python 3 I cannot install pygithub3. So, this seems to be python2.7 only. On a Mac...

# get pip if you don't have it
sudo python -m ensurepip
# In macos versions El Capitan and later, you must deal with System Integrity Protection
pip2 install --user pygithub3

@RichardBronosky
Copy link

In my fork of this gist you can print all of the (git/ssh or http) urls for all repos (public or private+public with personal_token) in a GitHub account (user or organization).

The usage looks like:

[HTTP_URLS=1] python2.7 list-all-repos.py account_name [personal_token]

By default it gives git@github: ssh URLs, but you can optionally get https://github.com URLs by setting a one-shot environment variable of HTTP_URLS. If you supply a personal_token you will get private+public repos, otherwise only public.

Enjoy.

@kad-meedel
Copy link

If you have jq:

curl -s https://api.github.com/orgs/<org_name>/repos?per_page=200 | jq '.[]|.html_url'

This is the easiest way to do it, but the api call is limited to max 100. You have to script is to get the rest of the repo's in the organzition.

@zmariscal
Copy link

I can confirm jq is the easiest way to get all this information

@sephethus
Copy link

^ No it isn't, not if you log in via ssh key. It doesn't accept my user/pass otherwise.

@devharness
Copy link

AttributeError: 'Github' object has no attribute 'repos'>

I am getting the same error

@harishnehru123
Copy link

Along with the Repo names, is it possible to get the Repo owners or the admin name in our organization's private repos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment