Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. Script for exporting GitHub Issues to an import ready CSV format.
  5.  
  6. Outputs to stdout for redirection.
  7.  
  8. It is hard coded (sorry) to include up to 2 comments from an issue.
  9.  
  10. Usage:
  11.  
  12. ./export_issues.py -u lisa -p b4rtsux -m springfieldElementary/mathcamp
  13.  
  14. ./export_issues.py -u lisa -p b4rtsux -m springfieldElementary/mathcamp > pivotal-tracker-import.csv
  15.  
  16. Dependencies:
  17.  
  18. - github3.py
  19.  
  20. """
  21.  
  22. import argparse
  23. import csv
  24. import sys
  25. from github3 import login
  26.  
  27.  
  28. def repo_issues(repo):
  29.  
  30. csvwriter = csv.writer(sys.stdout, quotechar='"', quoting=csv.QUOTE_ALL)
  31. csvwriter.writerow(['Title', 'Labels', 'Type', 'Created at', 'Description', 'Comment', 'Comment'])
  32.  
  33. for issue in repo.issues(state="open"):
  34.  
  35. comments = []
  36.  
  37. if issue.comments_count:
  38. for comment in issue.comments():
  39. value = "{body}\n\nFrom {user} on {time}\n{url}".format(
  40. body=comment.body,
  41. time=comment.created_at,
  42. user=comment.user,
  43. url=comment.url,
  44. )
  45. comments.append(value)
  46.  
  47. if not comments:
  48. comments = ["", ""]
  49. elif len(comments) == 1:
  50. comments.append("")
  51.  
  52. labels = ",".join([label.name for label in issue.labels()])
  53.  
  54. csvwriter.writerow([
  55. issue.title,
  56. labels,
  57. "bug" if "bug" in labels else "feature",
  58. issue.created_at,
  59. issue.body + "\n\n" + issue.url,
  60. comments[0],
  61. comments[1],
  62. ])
  63.  
  64.  
  65. if __name__ == '__main__':
  66. parser = argparse.ArgumentParser()
  67. parser.add_argument("-u", "--username", help="Username")
  68. parser.add_argument("-p", "--password", help="Password")
  69. parser.add_argument("-r", "--repo", help="Repository: account/repo")
  70. args = parser.parse_args()
  71.  
  72. gh = login(args.username, password=args.password)
  73.  
  74. repo = gh.repository(*args.repo.split("/"))
  75. repo_issues(repo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement