-
Notifications
You must be signed in to change notification settings - Fork 0
/
Github.py
172 lines (129 loc) · 4.31 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-08-20 14:45:16
# @Author : Tom Hu ([email protected])
# @Link : http://h1994st.com
# @Version : 1.0
import github
import Config
from timeout import timeout
TIMEOUT = int(Config.Global('timeout'))
class Github(github.Github):
'''
Default repository: covertsan/Test-Py
Default user: covertsan ([email protected])
'''
def __init__(self,
username=Config.Github('username'),
password=Config.Github('password')):
# username = Config.Github('username')
# password = Config.Github('password')
repo = Config.Github('repo')
super(Github, self).__init__(username, password)
self._user = self.get_user()
self._repo = self.user.get_repo(repo)
@property
def user(self):
return self._user
@property
def repo(self):
return self._repo
@property
def issues(self):
return self.get_issues(readable=True)
def _issue_to_dict(self, issue):
return {
'id': issue.id,
'number': issue.number,
'title': issue.title,
'body': issue.body,
'created_at': issue.created_at
}
def get_issues(self, readable=False):
issues = self.repo.get_issues()
if readable:
return map(self._issue_to_dict, issues)
return issues
# Write
@timeout(TIMEOUT)
def create_issue(self, title, body=github.GithubObject.NotSet,
assignee=github.GithubObject.NotSet):
return self.repo.create_issue(title, body=body, assignee=assignee)
# Read
@timeout(TIMEOUT)
def get_issue(self, number, readable=False):
issue = self.repo.get_issue(number)
if readable:
return self._issue_to_dict(issue)
return issue
# Delete (impossible)
def delete_issue(self, number=None, issue=None):
self.close_issue(number=number, issue=issue)
# Close
@timeout(TIMEOUT)
def close_issue(self, number=None, issue=None):
assert (number is None or
isinstance(number, int)), number
assert (issue is None or
isinstance(issue, github.Issue.Issue)), issue
if number is None and issue is None:
return
if issue is not None:
issue.edit(state='closed')
return
# issue is None, but number is not None
self.get_issue(number).edit(state='closed')
# Create a new repository
def create_repo(self, name=Config.Github('repo')):
self._repo = self.user.create_repo(
name, description='HEHEH', has_issues=True,
has_wiki=False, has_downloads=False, auto_init=True)
return self._repo
# Get a repository
def change_repo(self, name=None):
assert name is None or isinstance(name, (str, unicode)), name
if name is None:
# Change to default
name = Config.Github('repo') # Default repository name
self._repo = self.user.get_repo(name)
return self._repo
# Delete a repository
def delete_repo(self, name=None):
assert name is None or isinstance(name, (str, unicode)), name
if name is None or name == self.repo.name:
# Current repo
repo = self.repo
else:
repo = self.user.get_repo(name)
if repo is None:
# no repo
print 'The repository you designate does not exist.'
return
# Delete
repo.delete()
if name is None or name == self.repo.name:
# Current repo
self._repo = None
# Delete all issues
def delete_all_issues(self, name=None):
assert name is None or isinstance(name, (str, unicode)), name
issues = self.get_issues()
for issue in issues:
self.delete_issue(issue=issue)
if __name__ == '__main__':
g = Github()
print g.repo
# Change repository
print g.change_repo('Yinxiang')
# Create repository
repository = g.create_repo(name='Test')
# Write
issue = g.create_issue('title', body='body')
print issue
# Read
print g.get_issue(issue.number)
# Delete (Close)
g.delete_issue(issue=issue)
# Delete repository
g.delete_repo()
print g.repo