-
Notifications
You must be signed in to change notification settings - Fork 6
/
libtempo.py
117 lines (100 loc) · 3.63 KB
/
libtempo.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
import json
import requests
ENDPOINTS = {
"login": "/rest/gadget/1.0/login",
"worklogs": "/rest/tempo-rest/1.0/worklogs/",
"remaining_estimate": "/rest/tempo-rest/1.0/worklogs/remainingEstimate/calculate/",
}
class JiraTempoTimelogsDriver:
def __init__(self, jira_url):
self.jira_url = jira_url
self.session = requests.Session()
self.username = ""
def get_url(self, endpoint):
return self.jira_url + ENDPOINTS[endpoint]
def login(self, username, password):
request = self.session.post(
self.get_url("login"),
data={
"os_username": username,
"os_password": password,
},
)
# print the html returned or something more intelligent to see if it's a successful login page.
response = json.loads(request.text)
if not response.get("loginSucceeded"):
raise "Failed to log in."
self.username = username
@staticmethod
def _format_date(date):
return date.strftime("%d/%b/%y")
@staticmethod
def _format_time(time):
hours, mod = divmod(time.seconds, 3600)
minutes, sec = divmod(mod, 60)
if sec > 30:
minutes += 1
output = ""
if hours:
output += "{}h".format(hours)
if minutes:
output += " {}m".format(minutes)
return output
def get_remaining_estimate(self, timelog):
"""
Get remaining estimate from Jira
"""
url_data = "{ticket}/{today}/{today}/{duration}?username={username}"
extra_data = url_data.format(
ticket=timelog.ticket,
today=timelog.date.strftime("%Y-%m-%d"),
# Then we have this ugly thing here
duration="{}m".format(int(timelog.time.seconds / 60)),
username=self.username,
)
# I need to start using proper methods to join urls
response = self.session.get(self.get_url("remaining_estimate") + extra_data)
# Check if answer is good, calculate and return remaining
if response.status_code == 200:
remaining = response.content.decode("UTF-8")
if not remaining:
return "0h"
return remaining
return False
def add_timelog(self, timelog):
"""
Timelog structured like this:
{
"ticket": "BB-0000",
"date": DateObject,
"time": TimeDelta,
"description": "I did some work"
}
"""
remaining_estimate = self.get_remaining_estimate(timelog)
payload = {
"type": "issue",
"actionType": "logTime",
"use-ISO8061-week-numbers": False,
"ansidate": timelog.date.strftime("%Y-%m-%d"),
"ansienddate": timelog.date.strftime("%Y-%m-%d"),
"tracker": False,
"issue": "BB-282",
"planning": False,
"selectedUser": self.username,
"date": self._format_date(timelog.date),
"enddate": self._format_date(timelog.date),
"time": self._format_time(timelog.time),
"remainingEstimate": remaining_estimate,
"comment": timelog.description,
}
response = self.session.post(
self.get_url("worklogs") + timelog.ticket, data=payload
)
response_details = response.content.decode("UTF-8")
if 'valid="true"' in response_details:
return True
else:
raise RuntimeError(
f"Unable to log time entry on {timelog.ticket} - see response from tempo: \n {response_details}"
)