Skip to content

Instantly share code, notes, and snippets.

@sergioisidoro
Last active February 17, 2022 14:47
Show Gist options
  • Save sergioisidoro/fe6c25ac0afef5fd13c079e8389708cd to your computer and use it in GitHub Desktop.
Save sergioisidoro/fe6c25ac0afef5fd13c079e8389708cd to your computer and use it in GitHub Desktop.
Export Github PR statistics to CSV for easy analysis
import csv
from github import Github
# api settings for github
git_api_token = 'HERE IS A TOKEN'
git_repos = ['myRepo']
# csv name
csv_name = "git_hub_issues.csv"
def run_csv():
"""
Export github issues into a csv format
"""
output_csv = csv.writer(open(csv_name, 'w'), delimiter=',')
g = Github(git_api_token)
# csv headers
headers = [
'id',
'title',
'state',
'creator',
'labels',
'created_at',
'updated_at',
'closed_at',
'merged',
'merged_at',
'additions',
'deletions',
'commits'
]
# write header rows
output_csv.writerow(headers)
for repo in g.get_user().get_repos():
if repo.name in git_repos:
# get the git issues and write the rows to the csv
git_issues = repo.get_pulls(state='closed')
for git_issue in git_issues:
labels = ' '.join([lab.name for lab in git_issue.labels])
# alot of these are blank because they are not really
# needed but if you need them just fill them out
issue = [
git_issue.number,
git_issue.title.encode('utf8'),
git_issue.state,
git_issue.user.login,
labels.encode('utf8'),
git_issue.created_at,
git_issue.updated_at,
git_issue.closed_at,
git_issue.merged,
git_issue.merged_at,
git_issue.additions,
git_issue.deletions,
git_issue.commits
]
print (issue)
output_csv.writerow(issue)
if __name__ == '__main__':
run_csv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment