-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
182 lines (142 loc) · 6.47 KB
/
github.py
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
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# Package: Tragit
# Author: Abhishek Shrivastava <i.abhi27[at]gmail[dot]com>.
# License: BSD License
# TODO: NEED TO FIND A WAY TO ACCESS GITHUB VIA API TOKEN WITHOUT PASSWORD
# GITHUBLOGIN = os.popen('git config --global github.user').read().strip()
# GITHUBTOKEN = os.popen('git config --global github.token').read().strip()
# TODO: NEED TO ALLOW FOR COMMENTS IMPORT TOO
import os,sys
import base64
from httplib import HTTPSConnection
from json import JSONDecoder, JSONEncoder
GITHUBAPI = 'api.github.com'
class Github(object):
def __init__(self, username, password, project, projectsource):
self._username = username
self._password = password
self._project = project
self._projectsource = projectsource
self.labels = []
self.milestones = {}
self.collaborators = {}
def get_collaborators(self):
if not self.collaborators:
x = GithubRequest(self._username, self._password)
data = x.request('GET','/repos/%s/%s/collaborators' % (self._projectsource, self._project))
if 'message' in data:
self._error(data)
return False
self.collaborators = [y['login'] for y in data]
return self.collaborators
def get_milestones(self):
if not self.milestones:
x = GithubRequest(self._username, self._password)
data = x.request('GET','/repos/%s/%s/milestones?state=open' % (self._projectsource, self._project))
if 'message' in data:
self._error(data)
return False
self.milestones = dict([(y['title'], y['number']) for y in data])
data = x.request('GET','/repos/%s/%s/milestones?state=closed' % (self._projectsource, self._project))
if 'message' in data:
self._error(data)
return False
self.milestones.update(dict([(y['title'], y['number']) for y in data]))
return self.milestones
def create_milestone(self, ms_title, ms_desc = None, ms_dueon = None, ms_state = None):
x = GithubRequest(self._username, self._password)
milestone = {}
milestone['title'] = ms_title
if ms_desc != None:
milestone['description'] = ms_desc
if ms_state != None:
milestone['state'] = ms_state
if ms_dueon != None:
milestone['due_on'] = ms_dueon
print "Creating milestone : "+str(milestone)
data = x.request('POST','/repos/%s/%s/milestones' % (self._projectsource, self._project), milestone)
if 'title' in data and data['title'] == ms_title:
self.milestones[ms_title] = data['number']
return data['number']
self._error(data)
return False
def get_labels(self):
if not self.labels:
x = GithubRequest(self._username, self._password)
data = x.request('GET','/repos/%s/%s/labels' % (self._projectsource, self._project))
if 'message' in data:
self._error(data)
return False
self.labels = [x['name'] for x in data]
return self.labels
def create_label(self, lab_name, lab_color = '0000DD'):
x = GithubRequest(self._username, self._password)
label = {}
label['name'] = lab_name
label['color'] = lab_color
print "Creating label : "+str(label)
data = x.request('POST','/repos/%s/%s/labels' % (self._projectsource, self._project), label)
if 'name' in data and data['name'] == lab_name:
self.labels.append(lab_name)
return True
self._error(data)
return False
def create_issue(self, iss_title, iss_body = None, iss_assignee = None, iss_milestone = None, iss_labels = None):
x = GithubRequest(self._username, self._password)
issue = {}
issue['title'] = iss_title
if iss_body != None:
issue['body'] = iss_body
if iss_assignee != None and iss_assignee != '':
issue['assignee'] = iss_assignee
if iss_milestone != None and type(iss_milestone) == type(1):
issue['milestone'] = iss_milestone
if iss_labels != None and type(iss_labels) == type([]):
issue['labels'] = iss_labels
data = x.request('POST','/repos/%s/%s/issues' % (self._projectsource, self._project), issue)
if 'title' in data and data['title'] == iss_title:
return data['number']
self._error(data)
return False
def close_issue(self, iss_id):
x = GithubRequest(self._username, self._password)
issue = {}
issue['state'] = 'closed'
data = x.request('PATCH','/repos/%s/%s/issues/%d' % (self._projectsource, self._project, iss_id), issue)
if 'state' in data and data['state'] == 'closed':
return True
self._error(data)
return False
def get_error(self):
return self._last_error_data
def _error(self, data):
self._last_error_data = data
print "----------------ERROR--------------"
print data
print "----------------ERROR--------------"
class GithubRequest(object):
def __init__(self, username, password):
self._username = username
self._password = password
self._create_connection()
self._create_auth_header()
self._decoder = JSONDecoder()
self._encoder = JSONEncoder()
def _create_auth_header(self):
userpass = '%s:%s' % (self._username, self._password)
authkey = base64.b64encode(userpass).replace('\n','')
self._auth_header = {}
self._auth_header['Authorization'] = 'Basic %s' % authkey
def _create_connection(self):
self._connection = HTTPSConnection(GITHUBAPI)
self._connection.connect()
def request(self, method, url, params = None):
if params != None:
jsondata = self._encoder.encode(params)
else:
jsondata = None
self._connection.request(method,url,jsondata,self._auth_header)
response = self._connection.getresponse()
jsonresponse = response.read()
textresponse = self._decoder.decode(jsonresponse)
return textresponse