Guest User

Untitled

a guest
Jan 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. """
  3. Generate a CSV file of your organization's GitHub repositories.
  4.  
  5. Results in a CSV file with the following columns:
  6. - name: The name of the repository
  7. - private: True if this is a private repository, else False
  8. - fork: True if this repository is the result of a fork, else False
  9. - created_at: The datetime this repository was created
  10. - updated_at: The datetime this repository was last updated
  11. - size: The size in kB of this repository
  12. - has_wiki: True if this repository has a wiki, else False
  13. - language: The main language used in this repository, as inferred by GitHub
  14.  
  15. NOTE: This script depends on the pygithub package. Install it with `pip install pygithub`.
  16.  
  17. Usage: GITHUB_ACCESS_TOKEN=foobar github_repos.py my_organization
  18. """
  19.  
  20. import csv
  21. import os
  22. import sys
  23.  
  24. import github
  25.  
  26. KEYS = ('name', 'private', 'fork', 'created_at', 'updated_at', 'size', 'has_wiki', 'language')
  27.  
  28.  
  29. def get_repos(organization, access_token):
  30. session = github.Github(access_token)
  31. organization = session.get_organization(organization)
  32. return list(organization.get_repos())
  33.  
  34.  
  35. def repo_to_dict(repo):
  36. return {
  37. key: getattr(repo, key)
  38. for key in KEYS
  39. }
  40.  
  41.  
  42. def write_repos(repos):
  43. writer = csv.DictWriter(sys.stdout, KEYS)
  44. writer.writeheader()
  45. for repo in repos:
  46. writer.writerow(repo_to_dict(repo))
  47.  
  48.  
  49. if __name__ == "__main__":
  50. organization = sys.argv[1]
  51. access_token = os.getenv('GITHUB_ACCESS_TOKEN')
  52. repos = get_repos(organization, access_token)
  53. write_repos(repos)
Add Comment
Please, Sign In to add comment