Skip to content

Instantly share code, notes, and snippets.

@bennylope
Created March 22, 2016 13:13
Show Gist options
  • Save bennylope/08597fb3190da8d3c8c4 to your computer and use it in GitHub Desktop.
Save bennylope/08597fb3190da8d3c8c4 to your computer and use it in GitHub Desktop.
Export GitHub issues to a Pivotal Tracker-ready CSV format
#!/usr/bin/env python3
"""
Script for exporting GitHub Issues to an import ready CSV format.
Outputs to stdout for redirection.
It is hard coded (sorry) to include up to 2 comments from an issue.
Usage:
./export_issues.py -u lisa -p b4rtsux -m springfieldElementary/mathcamp
./export_issues.py -u lisa -p b4rtsux -m springfieldElementary/mathcamp > pivotal-tracker-import.csv
Dependencies:
- github3.py
"""
import argparse
import csv
import sys
from github3 import login
def repo_issues(repo):
csvwriter = csv.writer(sys.stdout, quotechar='"', quoting=csv.QUOTE_ALL)
csvwriter.writerow(['Title', 'Labels', 'Type', 'Created at', 'Description', 'Comment', 'Comment'])
for issue in repo.issues(state="open"):
comments = []
if issue.comments_count:
for comment in issue.comments():
value = "{body}\n\nFrom {user} on {time}\n{url}".format(
body=comment.body,
time=comment.created_at,
user=comment.user,
url=comment.url,
)
comments.append(value)
if not comments:
comments = ["", ""]
elif len(comments) == 1:
comments.append("")
labels = ",".join([label.name for label in issue.labels()])
csvwriter.writerow([
issue.title,
labels,
"bug" if "bug" in labels else "feature",
issue.created_at,
issue.body + "\n\n" + issue.url,
comments[0],
comments[1],
])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", help="Username")
parser.add_argument("-p", "--password", help="Password")
parser.add_argument("-r", "--repo", help="Repository: account/repo")
args = parser.parse_args()
gh = login(args.username, password=args.password)
repo = gh.repository(*args.repo.split("/"))
repo_issues(repo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment