def write_issues(r, issues_csvout, comments_csvout, repo, auth):
    "output a list of issues to csv"
    if not r.status_code == 200:
        raise Exception(r.status_code)
    for issue in r.json():
        labels = []
        labels_lowercase = []
        for label in issue['labels']:
            label_name = label.get('name')
            if not label_name:
                continue
            labels.append(label_name)
            labels_lowercase.append(label_name)

        # TODO: Join writerow
        #labels = csvClient.VALUE_DELIMITER.join([str(x) for x in labels])

        assignee = issue['assignee']
        if assignee:
            assignee = assignee.get('login')
        else:
            assignee = ""

        created = issue['created_at']
        updated = issue.get('updated_at', '')
        resolved = issue.get('closed_at', '')

        author = issue['user'].get('login')
        if not author:
            author = get_last_part_of_url(issue['user'].get('url'))

        project = get_last_part_of_url(repo)

        milestone = issue.get('milestone')
        if milestone:
            milestone = milestone['title']
        else:
            milestone = ''

        state = issue['state'].lower()
        if state == 'closed':
            if 'wontfix' in labels_lowercase or 'invalid' in labels_lowercase:
                state = "Won't fix"
            else:
                state = "Fixed"

        issue_type = 'Task'
        if 'bug' in labels_lowercase:
            issue_type = 'Bug'

        issue_row = [project, project, issue['number'], state, issue['title'],
                     issue['body'], created, updated, resolved, author or 'guest',
                     assignee, csvClient.VALUE_DELIMITER.join(labels),
                     issue_type, milestone]
        issues_csvout.writerow([utf8encode(e) for e in issue_row])
        
        if int(issue.get('comments', 0)) > 0 and 'comments_url' in issue:
            rc = requests.get(issue['comments_url'], auth=auth)
            if not rc.status_code == 200:
                raise Exception(r.status_code)
            for comment in rc.json():
                author = comment['user'].get('login')
                if not author:
                    author = get_last_part_of_url(comment['user'].get(u'url'))
                comment_row = [project, issue['number'], author or 'guest',
                               comment['created_at'], comment['body']]
                comments_csvout.writerow([utf8encode(e) for e in comment_row])
def write_issues(r, issues_csvout, comments_csvout, repo, auth):
    """output a list of issues to csv"""
    if not r.status_code == 200:
        raise Exception(r.status_code)
    for issue in r.json():
        labels = []
        labels_lowercase = []
        for label in issue['labels']:
            label_name = label.get('name')
            if not label_name:
                continue
            labels.append(label_name)
            labels_lowercase.append(label_name)

        # TODO: Join writerow
        #labels = csvClient.VALUE_DELIMITER.join([str(x) for x in labels])

        assignee = issue['assignee']
        if assignee:
            assignee = assignee.get('login')
        else:
            assignee = ""

        created = issue['created_at']
        updated = issue.get('updated_at', '')
        resolved = issue.get('closed_at', '')

        author = issue['user'].get('login')
        if not author:
            author = get_last_part_of_url(issue['user'].get('url'))

        project = re.sub(r'[^\w]', '_', get_last_part_of_url(repo))

        milestone = issue.get('milestone')
        if milestone:
            milestone = milestone['title']
        else:
            milestone = ''

        state = issue['state'].lower()
        if state == 'closed':
            if 'wontfix' in labels_lowercase or 'invalid' in labels_lowercase:
                state = "Won't fix"
            else:
                state = "Fixed"

        issue_type = 'Task'
        if 'bug' in labels_lowercase:
            issue_type = 'Bug'

        issue_row = [
            project, project, issue['number'], state, issue['title'],
            issue['body'], created, updated, resolved, author or 'guest',
            assignee,
            youtrackutils.csvClient.VALUE_DELIMITER.join(labels), issue_type,
            milestone
        ]
        issues_csvout.writerow([utf8encode(e) for e in issue_row])

        if int(issue.get('comments', 0)) > 0 and 'comments_url' in issue:
            rc = requests.get(issue['comments_url'], auth=auth)
            if not rc.status_code == 200:
                raise Exception(r.status_code)
            for comment in rc.json():
                author = comment['user'].get('login')
                if not author:
                    author = get_last_part_of_url(comment['user'].get(u'url'))
                comment_row = [
                    project, issue['number'], author or 'guest',
                    comment['created_at'], comment['body']
                ]
                comments_csvout.writerow([utf8encode(e) for e in comment_row])
def write_issues(r, issues_csvout, comments_csvout, gitlab_api_endpoint,
                 gitlab_project_owner, gitlab_project_name, gitlab_project_id,
                 headers):
    """output a list of issues to csv"""
    if not r.status_code == 200:
        raise Exception(r.status_code)
    for issue in r.json():
        labels = []
        labels_lowercase = []
        for label in issue['labels']:
            label_name = label
            if not label_name:
                continue
            labels.append(label_name)
            labels_lowercase.append(label_name)

        # TODO: Join writerow
        #labels = csvClient.VALUE_DELIMITER.join([str(x) for x in labels])

        assignee = issue['assignee']
        if assignee:
            assignee = assignee.get('username')
        else:
            assignee = issue['assignees']
            if assignee:
                assignee = assignee.get('username')

        created = issue['created_at']
        updated = issue.get('updated_at', '')
        resolved = issue.get('closed_at', '')

        author = issue['author'].get('username')
        if not author:
            # I'm not sure if the line below applies to Gitlab or just Github.
            author = get_last_part_of_url(issue['user'].get('url'))

        project = re.sub(r'[^\w]', '_',
                         get_last_part_of_url(gitlab_project_name))

        milestone = issue.get('milestone')
        if milestone:
            milestone = milestone['title']
        else:
            milestone = ''

        state = issue['state'].lower()
        if state == 'closed':
            if 'wontfix' in labels_lowercase or 'invalid' in labels_lowercase:
                state = "Won't fix"
            else:
                state = "Fixed"

        issue_type = 'Task'
        if 'bug' in labels_lowercase:
            issue_type = 'Bug'

        issue_row = [
            project, project, issue['iid'], state, issue['title'],
            issue['description'], created, updated, resolved, author
            or 'guest', assignee,
            youtrackutils.csvClient.VALUE_DELIMITER.join(labels), issue_type,
            milestone,
            str(issue['weight']) + "d",
            issue['time_stats']['human_total_time_spent']
        ]
        issues_csvout.writerow([utf8encode(e) for e in issue_row])

        # Handle migrating issue comments from GitLab to YouTrack.
        if int(issue.get('user_notes_count', 0)) > 0:
            gitlab_comments_url = "%s/projects/%s/issues/%s/notes?sort=asc&order_by=updated_at" % (
                gitlab_api_endpoint, gitlab_project_id, issue['iid'])
            rc = requests.get(gitlab_comments_url, headers=headers)
            if not rc.status_code == 200:
                raise Exception(r.status_code)
            for comment in rc.json():
                author = comment['author'].get('username')
                if not author:
                    author = "guest"
                reg1 = re.compile('api/v4', re.VERBOSE)
                gitlab_base = reg1.sub(r'', gitlab_api_endpoint)
                comment_body = ''
                try:
                    reg2 = re.compile("mentioned in commit (\w+)",
                                      re.MULTILINE)
                    comment_body = reg2.sub(
                        r"mentioned in commit [\1](" + gitlab_base +
                        gitlab_project_owner + "/" + gitlab_project_name +
                        r"/commit/\1) in GitLab.", str(comment['body']))
                    reg3 = re.compile("mentioned in merge request \!(\d+)",
                                      re.MULTILINE)
                    comment_body = reg3.sub(
                        r"mentioned in merge request [!\1](" + gitlab_base +
                        gitlab_project_owner + "/" + gitlab_project_name +
                        r"/merge_requests/\1) in GitLab.", comment_body)
                    #print str(comment['body'])
                    #print comment_body
                except (UnicodeEncodeError, RuntimeError, TypeError,
                        NameError):
                    pass
                comment_row = [
                    project, issue['iid'], author or 'guest',
                    comment['created_at'], comment_body
                ]
                comments_csvout.writerow([utf8encode(e) for e in comment_row])