forked from hershwg/github-migrate-trac-tickets
-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.py
71 lines (64 loc) · 2.63 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
import base64
import urllib2
try:
import json
except ImportError:
import simplejson as json
class GitHub(object):
"""Connections, queries and posts to GitHub.
"""
def __init__(self, username, password, repo):
"""Username and password for auth; repo is like 'myorg/myapp'.
"""
self.username = username
self.password = password
self.repo = repo
self.url = "https://api.github.com/repos/%s" % self.repo
self.auth = base64.encodestring('%s:%s' % (self.username, self.password))[:-1]
def access(self, path, query=None, data=None):
"""Append the API path to the URL GET, or POST if there's data.
"""
if not path.startswith('/'):
path = '/' + path
if query:
path += '?' + query
url = self.url + path
req = urllib2.Request(url)
req.add_header("Authorization", "Basic %s" % self.auth)
try:
if data:
req.add_header("Content-Type", "application/json")
res = urllib2.urlopen(req, json.dumps(data))
else:
res = urllib2.urlopen(req)
return json.load(res)
except (IOError, urllib2.HTTPError), e:
raise RuntimeError("Error on url=%s e=%s" % (url, e))
def issues(self, id_=None, query=None, data=None):
"""Get issues or POST and issue with data.
Query for specifics like: issues(query='state=closed')
Create a new one like: issues(data={'title': 'Plough', 'body': 'Plover'})
You ca NOT set the 'number' param and force a GitHub issue number.
"""
path = 'issues'
if id_:
path += '/' + str(id_)
return self.access(path, query=query, data=data)
def issue_comments(self, id_, query=None, data=None):
"""Get comments for a ticket by its number or POST a comment with data.
Example: issue_comments(5, data={'body': 'Is decapitated'})
"""
# This call has no way to get a single comment
#TODO: this is BROKEN
return self.access('issues/%d/comments' % id_, query=query, data=data)
def labels(self, query=None, data=None):
"""Get labels or POST a label with data.
Post like: labels(data={'name': 'NewLabel'})
"""
return self.access('labels', query=query, data=data)
def milestones(self, query=None, data=None):
"""Get milestones or POST if data.
Post like: milestones(data={'title':'NEWMILESTONE'})
There are many other attrs you can set in the API.
"""
return self.access('milestones', query=query, data=data)