Skip to content

Instantly share code, notes, and snippets.

@ryanwilsonperkin
Last active January 9, 2018 23:55
Show Gist options
  • Save ryanwilsonperkin/9658b36a6142e55e43131d7d839b5c7c to your computer and use it in GitHub Desktop.
Save ryanwilsonperkin/9658b36a6142e55e43131d7d839b5c7c to your computer and use it in GitHub Desktop.
Use the GitHub API to generate a CSV or your organization's repositories.
#!/usr/local/bin/python
"""
Generate a CSV file of your organization's GitHub repositories.
Results in a CSV file with the following columns:
- name: The name of the repository
- private: True if this is a private repository, else False
- fork: True if this repository is the result of a fork, else False
- created_at: The datetime this repository was created
- updated_at: The datetime this repository was last updated
- size: The size in kB of this repository
- has_wiki: True if this repository has a wiki, else False
- language: The main language used in this repository, as inferred by GitHub
NOTE: This script depends on the pygithub package. Install it with `pip install pygithub`.
Usage: GITHUB_ACCESS_TOKEN=foobar github_repos.py my_organization
"""
import csv
import os
import sys
import github
KEYS = ('name', 'private', 'fork', 'created_at', 'updated_at', 'size', 'has_wiki', 'language')
def get_repos(organization, access_token):
session = github.Github(access_token)
organization = session.get_organization(organization)
return list(organization.get_repos())
def repo_to_dict(repo):
return {
key: getattr(repo, key)
for key in KEYS
}
def write_repos(repos):
writer = csv.DictWriter(sys.stdout, KEYS)
writer.writeheader()
for repo in repos:
writer.writerow(repo_to_dict(repo))
if __name__ == "__main__":
organization = sys.argv[1]
access_token = os.getenv('GITHUB_ACCESS_TOKEN')
repos = get_repos(organization, access_token)
write_repos(repos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment