Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Last active March 4, 2019 05:20
Show Gist options
  • Save charlesreid1/1e7bf1c179040e5133cee5538cbeb3ec to your computer and use it in GitHub Desktop.
Save charlesreid1/1e7bf1c179040e5133cee5538cbeb3ec to your computer and use it in GitHub Desktop.
Walk through each issue in a Github repository and extract the date that each issue was closed.

Walk a Github Repo

This uses the Github API, via the PyGithub Python library, to list all closed issues and print info about when the issue was closed and who closed it.

To run, you need a Github API access token:

GITHUB_TOKEN="XXX" python issue_close_dates.py

Overview of script:

  • get API instance
  • get repo
  • iterate over each issue
  • print issue information
  • store information in a CSV file
import requests
from github import Github
import os, re
import base64
import logging
import re
from datetime import datetime, timedelta
"""
Issue Close Dates
Walk through each issue in a repository
and extract the date that the issue was
closed.
"""
LOG_FILE = 'issue_close_dates.log'
CSV_FILE = 'issue_close_dates.csv'
logging.basicConfig(level=logging.INFO,
filename=LOG_FILE,
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
REPO = 'dcppc/dcppc-milestones'
def main():
print_issue_close_dates(REPO)
def print_issue_close_dates(reponame):
"""
iterate through all closed issues
and print out their closed dates.
"""
if '/' not in reponame:
err = "No slash in repository name provided: %s\n"%(reponame)
err += "Provide repo names the format org-name/repo-name"
raise Exception(err)
which_org, which_repo = re.split('/',reponame)
logging.debug("-"*40)
logging.debug("Setting up github api")
access_token = os.environ['GITHUB_TOKEN']
# Github -> get organization -> get repository
g = Github(access_token)
org = g.get_organization(which_org)
repo = org.get_repo(which_repo)
logging.debug("Iterating through issues.")
logging.debug("Begin report on issue close dates:")
logging.debug("-"*40)
csv_head = "issue_number,closed_on,closed_by,issue_title"
csv_content = [csv_head]
for issue in repo.get_issues(state="closed"):
issue_title = issue.title
issue_title = issue_title.strip()
issue_title = re.sub('\r\n','',issue_title)
issue_title = re.sub('\n','',issue_title)
issue_number = issue.number
issue_labels = [j.name for j in issue.labels]
closed_on = issue.closed_at.strftime("%Y-%m-%d %H:%M:%S")
closed_by = issue.closed_by.login
msg = "On %s Github user @%s "%(closed_on, closed_by)
msg += "closed issue #%d \"%s\" "%(issue_number, issue_title)
msg += "(LABELS: %s)"%(", ".join(issue_labels))
logging.info(msg)
csv = "%s,%s,%s,\"%s\""%(issue_number,closed_on,closed_by,issue_title)
csv_content.append(csv)
logging.debug("Opening csv file to write: %s"%(CSV_FILE))
with open(CSV_FILE,'w') as f:
f.write("\n".join(csv_content))
logging.debug("Finished writing csv file.")
logging.debug("-"*40)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment