-
Notifications
You must be signed in to change notification settings - Fork 13
/
mlxredmine.py
88 lines (66 loc) · 2.49 KB
/
mlxredmine.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
#!/usr/bin/python
import requests
from datetime import datetime
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.exceptions import ConnectionError
from requests.exceptions import ReadTimeout
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
REDMINE_URL = 'http://redmine.mtl.com'
# tracker ids
TRACKER_BUG_SW = 28
# redmine status codes
STATUS_IN_PROGRESS = 2
STATUS_FIXED = 16
STATUS_RELEASE_NOTES = 14
STATUS_WONT_FIX = 11
STATUS_REJECTED = 6
STATUS_CLOSED = 5
STATUS_CLOSED_REJECTED = 38
STATUS_CLOSED_EXTERNAL = 74
REDMINE_TIMESTAMP_FMT = '%Y-%m-%dT%H:%M:%S'
def parse_redmine_time(value):
parts = value.split('.')
return datetime.strptime(parts[0], REDMINE_TIMESTAMP_FMT)
class MlxRedmine(object):
def __init__(self, api_key=''):
self.fail_all_calls = False
self.cache = {}
self.api_key = api_key
def get_url(self, url, params=None):
headers = {
'X-Redmine-API-Key': self.api_key
}
return requests.get(url, headers=headers, params=params, verify=False, timeout=1)
def get_issue(self, issue_id, retry=0):
if self.fail_all_calls:
raise ConnectionError("Redmine connection issue")
if issue_id in self.cache:
return self.cache[issue_id]
loops = max(1, retry + 1)
for i in range(loops):
try:
r = self.get_url(REDMINE_URL + '/issues/%s.json' % issue_id)
break
except (ConnectionError, ReadTimeout):
if i+1 == loops:
self.fail_all_calls = True
raise
continue
j = r.json()
task = j['issue']
self.cache[issue_id] = task
return task
def is_issue_wont_fix_or_release_notes(self, task):
return task['status']['id'] in (STATUS_WONT_FIX, STATUS_RELEASE_NOTES)
def is_issue_closed(self, task):
return task['status']['id'] in (STATUS_FIXED, STATUS_CLOSED, STATUS_CLOSED_REJECTED, STATUS_CLOSED_EXTERNAL)
def is_issue_open(self, task):
return not self.is_issue_closed(task)
def is_tracker_bug(self, task):
return task['tracker']['id'] == TRACKER_BUG_SW
def created_days_ago(self, task):
created = parse_redmine_time(task['created_on'])
return (datetime.now() - created).days
def updated_days_ago(self, task):
updated = parse_redmine_time(task['updated_on'])
return (datetime.now() - updated).days