|
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() |
|
|