Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4. import csv
  5. import json
  6.  
  7. from github import Github
  8.  
  9.  
  10. def get_org_stats():
  11. """
  12. Get stats on each contributor in an organization on github.
  13. """
  14. username = raw_input('GH Username: ')
  15. password = raw_input('GH Password: ')
  16. org = raw_input('GH Org: ')
  17. g = Github(username, password)
  18. contributors = dict()
  19.  
  20. for repo in g.get_organization(org).get_repos():
  21. if repo.fork:
  22. print('skipping ' + repo.full_name)
  23. continue
  24. repo_contributors = repo.get_stats_contributors()
  25. if not repo_contributors:
  26. continue
  27.  
  28. for contributor in repo_contributors:
  29. login = contributor.author.login
  30. cont_data = contributors.get(login, dict())
  31. for stat in ('total_commits', 'additions', 'deletions'):
  32. cont_data[stat] = cont_data.get(stat, 0)
  33. cont_data['repos'] = cont_data.get('repos', [])
  34. cont_data['repos'].append(repo.full_name)
  35. cont_data['total_commits'] += contributor.total
  36.  
  37. for week in contributor.weeks:
  38. for key, ghkey in (('additions', 'a'), ('deletions', 'd')):
  39. cont_data[key] += getattr(week, ghkey)
  40. contributors[login] = cont_data
  41. # Dump JSON
  42. with open('data.json', 'w') as outfile:
  43. json.dump(contributors, outfile, indent=2)
  44.  
  45. # Dump CSV
  46. user_list = []
  47. for user in contributors:
  48. additions = contributors[user]['additions']
  49. deletions = contributors[user]['deletions']
  50. user_list.append(
  51. (
  52. user,
  53. contributors[user]['total_commits'],
  54. additions,
  55. deletions,
  56. additions + deletions,
  57. len(contributors[user]['repos'])
  58. )
  59. )
  60. with open('data.csv', 'wb') as csvfile:
  61. csv_writer = csv.writer(csvfile)
  62. csv_writer.writerow(
  63. [
  64. 'username',
  65. 'commits',
  66. 'additions',
  67. 'deletions',
  68. 'changes',
  69. 'number of repos'
  70. ]
  71. )
  72. for row in user_list:
  73. csv_writer.writerow(row)
  74.  
  75. if __name__ == '__main__':
  76. get_org_stats()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement