-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrom_github.py
More file actions
executable file
·174 lines (130 loc) · 4.78 KB
/
from_github.py
File metadata and controls
executable file
·174 lines (130 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python2.7
#
# Copyright (C) 2012 Travis Brown (travisb@travisbrown.ca)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# A script to export Github issues into the portable bug interchange format.
#
# Usage: from_github.py repoURL - export all issues for repository
# from_github.py issueURL - export particular issue
# from_github.py commentURL - export particular issue comment
import httplib
import urllib
import json
import sys
import pprint
def github_get(url):
conn = httplib.HTTPSConnection('api.github.com')
conn.request('GET', url)
response = conn.getresponse()
result = json.loads(response.read())
conn.close()
return result
# Returns a tuple of (key, object)
def format_comment(comment):
commentid = comment['url']
comments = {}
comments['name'] = comment['user']['login']
comments['created_at'] = comment['created_at']
comments['in-reply-to'] = 'issue' # Github doesn't support threaded comments
comments['comment'] = comment['body']
return (commentid, comments)
def get_comments(issue_url):
args = issue_url.split('/')
user = args[3]
repo = args[4]
issue = args[6]
url = '/repos/%s/%s/issues/%s/comments' % (user, repo, issue)
result = github_get(url)
comments = {}
for comment in result:
export = format_comment(comment)
comments[export[0]] = export[1]
return comments
def export_comment(comment_url):
args = comment_url.split('/')
user = args[3]
repo = args[4]
issue = args[6].split('#')[0]
comment = args[6].split('-')[1]
url = '/repos/%s/%s/issues/comments/%s' % (user, repo, comment)
result = github_get(url)
comment = format_comment(result)
# Comments must be contained within a bug, but we have to reconstruct the bug URL
bugid = 'https://github.com/%s/%s/issues/%s' % (user, repo, issue)
export = {'format' : 'http://travisbrown.ca/projects/bug_interchange.txt'}
export[bugid] = {}
export[bugid][comment[0]] = comment[1]
print json.dumps(export, sort_keys=True, indent=4)
# Returns a tuple of (key, object)
def format_issue(bug, repository_url):
repo = repository_url.split('/')[4]
bugid = bug['html_url']
export = {}
export['metadata'] = {}
export['metadata']['title'] = bug['title']
export['metadata']['created_at'] = bug['created_at']
export['metadata']['metadata_modified_at'] = bug['updated_at']
export['metadata']['project_name'] = repo
export['metadata']['project_id'] = repository_url
export['metadata']['status'] = bug['state']
export['metadata']['severity'] = ''
export['metadata']['component'] = ''
export['metadata']['reporter'] = bug['user']['login']
export['metadata']['seen_in'] = ''
if bug['assignee'] == None:
export['metadata']['owner'] = 'Unassigned'
else:
export['metadata']['owner'] = bug['assignee']['login']
export['metadata']['description'] = bug['body']
if bug['milestone'] != None:
export['metadata']['_fixby'] = bug['milestone']['title']
comments = get_comments(bugid)
for comment in comments.keys():
export[comment] = comments[comment]
return (bugid, export)
def export_issue(issue_url):
args = issue_url.split('/')
user = args[3]
repo = args[4]
issue = args[6]
url = '/repos/%s/%s/issues/%s' % (user, repo, issue)
result = github_get(url)
bug = format_issue(result, issue_url)
export = {'format' : 'http://travisbrown.ca/projects/bug_interchange.txt'}
export[bug[0]] = bug[1]
print json.dumps(export, sort_keys=True, indent=4)
def export_repository(repository_url):
args = repository_url.split('/')
user = args[3]
repo = args[4]
url = '/repos/%s/%s/issues' % (user, repo)
result = github_get(url)
export = {'format' : 'http://travisbrown.ca/projects/bug_interchange.txt'}
for bug in result:
formatted = format_issue(bug, repository_url)
export[formatted[0]] = formatted[1]
print json.dumps(export, sort_keys=True, indent=4)
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Usage: from_github.py repoURL - export all issues for repository'
print ' from_github.py issueURL - export particular issue'
print ' from_github.py commentURL - export particular issue comment'
sys.exit(1)
if 'issuecomment' in sys.argv[1]:
export_comment(sys.argv[1])
elif 'issues' in sys.argv[1]:
export_issue(sys.argv[1])
else:
export_repository(sys.argv[1])