noamoss
8/6/2017 - 6:04 PM

Export Open Labeled Issues and Time Estimation / Timebox to a CSV

Export Open Labeled Issues and Time Estimation / Timebox to a CSV


"""
Exports open Issues with a specific label and extract time box from a specified repository to a CSV file
Uses basic authentication (Github username + password) to retrieve Issues
from a repository that username has access to. Supports Github API v3.
Base on this original gists: https://gist.github.com/unbracketed/3380407
usage: run [gh username] [gh password] [repo (i.e. gizra/danel)] [label to include (i.e. '1st priority')]
"""
import csv
import requests
import sys
import re


GITHUB_USER = sys.argv[1]
GITHUB_PASSWORD = sys.argv[2]
REPO = sys.argv[3]  # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)

def extract_time_box(title):
    # search for a [digit+h] to extract time estimation as integer/ "No estimation"
    extract = re.search(r'\[([\d\.]+)h\]', title)
    if extract != None:
        timebox = str(extract.group(1))
    else:
        return "No Estimation"
    return timebox

def write_issues(r):
    """output a list of issues to csv"""
    if not r.status_code == 200:
        raise Exception(r.status_code)
    for issue in r.json():
        labels = issue['labels']
        for label in labels:                              # filter by label, not a case sensitive
            if label['name'].lower() == sys.argv[4].lower() and issue['state'] == 'open':
                title = issue['title']
                csvout.writerow([issue['number'], title, extract_time_box(title)])
                print(title)


def get_headers(r):
    """extract pages from headers"""
    pages = dict(
        [(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
            [link.split(';') for link in
                r.headers['link'].split(',')]])
    return pages


csvfile = '%s-issues-%s.csv' % (REPO.replace('/', '-'), sys.argv[4])
with open(csvfile, 'w', encoding='utf8', newline='') as csvfile:
    csvout =  csv.writer(csvfile, delimiter=',')
    header = (('id', 'Title', 'Time Estimation'))
    csvout.writerow(header)

    r = requests.get(ISSUES_FOR_REPO_URL, auth=AUTH) # first call
    print("getting 1")
    pages = get_headers(r) # get next/last info
    write_issues(r)
    counter = 2
    #more pages? examine the 'link' header returned
    if 'link' in r.headers:
        pages = get_headers(r) # get next/last info
        while 'next' in pages:
            print ("getting ",counter)
            counter +=1
            print(pages['next'])
            r = requests.get(pages['next'], auth=AUTH)
            pages = get_headers(r) # get next/last info
            write_issues(r)
            pages = get_headers(r)