forked from alourie/pyred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyred.py
204 lines (172 loc) · 7.28 KB
/
pyred.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# redmine.py -- a python library that allows working with
# redmine - a project management software
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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.
# Author: Alex Lourie <djay.il@gmail.com> @alourie
# Redmine: Copyright (C) 2006-2013 Jean-Philippe Lang
import os
import requests
import json
from base64 import b64decode
class Redmine:
def __init__(self, url=None, auth=None, pass_file=None):
self.url = url
self.session = requests.Session()
self.session.auth = auth or self.get_redmine_auth(pass_file=pass_file)
if not self.session.auth:
raise Exception(
"Error! No auth nor password file for redmine were given!"
)
self.session.verify = False
self.session.headers = {'content-type': 'application/json'}
def get_redmine_auth(self, pass_file=None):
"""docstring for redmine_auth"""
if not os.path.exists(pass_file):
raise OSError("Auth file %s doesn't exist." % pass_file)
username = None
password = None
with open(pass_file) as f:
for line in f.readlines():
if line.startswith("username"):
username = b64decode(line.split(" ")[1].strip())
if line.startswith("password"):
password = b64decode(line.split(" ")[1].strip())
if username == None or password == None:
raise TypeError("pass_file missing lines 'username=XX' and 'password=XX'")
return (username, password)
def get_project_url(self, project_id=None):
if project_id:
url = self.url + "/projects/%s.json" % project_id
else:
url = self.url + "/projects.json"
return url
def get_issue_url(self, issue_id=None):
if issue_id:
url = self.url + "/issues/%s.json" % issue_id
else:
url = self.url + "/issues.json"
return url
def get_time_entry_url(self, time_entry_id=None):
''' Get http://REDMINE_HOST/time_entries/[ISSUE_ID].xml url '''
if time_entry_id:
url = self.url + "/time_entries/%s.json" % time_entry_id
else:
url = self.url + "/time_entries.json"
return url
def getProject(self, project_id=None, name=None):
if not project_id and not name:
return None
if project_id and name:
raise TypeError("Please specify id or name, not both.")
project_id = project_id or name
r = self.session.get(self.get_project_url(project_id=project_id))
return self.Project(r.json())
def getProjects(self):
r = self.session.get(self.get_project_url(), data=json.dumps({'limit': 999}))
try:
return [self.Project(data) for data in r.json()['projects']]
except KeyError:
raise TypeError(r.json()['errors'])
def getIssue(self, issue_id):
r = self.session.get(self.get_issue_url(issue_id))
return self.Issue(r.json())
def getTimeEntry(self, time_entry_id):
''' Get a particular Time Entry. HTTP return code 200 ok, 404 error '''
r = self.session.get(self.get_time_entry_url(time_entry_id))
return self.TimeEntry(r.json())
def getIssues(self, criteria=None):
if criteria and not 'limit' in criteria:
criteria.update({'limit': 100})
elif not criteria:
criteria = ({'limit': 100})
r = self.session.get(self.get_issue_url(),
data=json.dumps(criteria))
try:
return [self.Issue(data) for data in r.json()['issues']]
except KeyError:
raise TypeError(r.json()['errors'])
def getTimeEntries(self, criteria=None):
''' Get Time Entries of a particular Issue filtered by criteria '''
if criteria and not 'limit' in criteria:
criteria.update({'limit': 100})
elif not criteria:
criteria = ({'limit': 100})
r = self.session.get(self.get_time_entry_url(),
data=json.dumps(criteria))
try:
return [self.TimeEntry(data) for data in r.json()['time_entries']]
except KeyError:
raise TypeError(r.json()['errors'])
def getTimeEntries(self, criteria=None):
''' Get Time Entries of a particular Issue filtered by criteria '''
if criteria and not 'limit' in criteria:
criteria.update({'limit': 100})
elif not criteria:
criteria = ({'limit': 100})
r = self.session.get(self.get_time_entry_url(),
data=json.dumps(criteria))
return [self.TimeEntry(data) for data in r.json()['time_entries']]
def updateIssue(self, issue_id, data):
print "Updating issue {id} with data:{data}".format(
id=issue_id,
data=data,
)
r = self.session.put(self.get_issue_url(issue_id), data=json.dumps(data))
return r
def createIssue(self, data):
r = self.session.post(self.get_issue_url(), data=json.dumps(data))
return r
def createTimeEntry(self, data):
''' Creates a Time Entry attached to a given Issue or Project '''
r = self.session.post(self.get_time_entry_url(), data=json.dumps(data))
return r
class RedmineObj(object):
def __init__(self, data, objType):
if not isinstance(data, dict):
raise TypeError("Data must be dict!")
self.raw_data = data
self.objType = objType
self.to_obj(data)
def to_obj(self, data):
if self.objType in data:
self.__dict__.update(**data[self.objType])
else:
self.__dict__.update(**data)
def __repr__(self):
t = self.get_data()
output = "Redmine %s object:\n" % self.objType
output = output + "{\n"
for k, v in t.iteritems():
output = output + " '%s': '%s',\n" % (k, v)
output = output + "}"
return output
def __getitem__(self, item):
t = self.get_data()
if item in t:
return t[item]
return None
def get_data(self):
ndata = self.__dict__.copy()
del ndata['raw_data']
return ndata
class Project(RedmineObj):
def __init__(self, data):
super(Redmine.Project, self).__init__(data, 'project')
class Issue(RedmineObj):
def __init__(self, data):
super(Redmine.Issue, self).__init__(data, 'issue')
class TimeEntry(RedmineObj):
def __init__(self, data):
super(Redmine.TimeEntry, self).__init__(data, 'time_entry')