-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjira.py
67 lines (57 loc) · 2.52 KB
/
jira.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
import getpass
import math
import os
import arrow
from arrow import Arrow
import requests as rest
import util
class Jira:
def __init__(self):
self.PAGE_SIZE = 50
self.HOST = os.environ.get("JIRA_HOST") or input("Jira Host: ")
assert self.HOST is not None, "You must specify a jira host!"
self.URL = f"https://{self.HOST}"
self.API_URL = self.URL + '/rest/api/2'
self.USERNAME = os.environ.get("JIRA_USER") or input("Jira User: ")
assert self.USERNAME is not None, "You must specify a jira user!"
self.PASSWD = os.environ.get("JIRA_PASSWD") or getpass.getpass("Jira Password: ")
self.AUTH = (self.USERNAME, self.PASSWD)
def get_issues(self, jql: str) -> list[dict[str, any]]:
util.debug_print(f'jql="{jql}"')
search_params = {
'jql': jql,
'fields': ['summary', 'status', 'resolution']
}
data = self.get_all('/search', 'issues', search_params)
issues = [{
'key': i['key'],
'summary': i['fields']['summary'],
'resolution': util.get_field(i, 'fields', 'resolution', 'name'),
'status': util.get_field(i, 'fields', 'status', 'name')
} for i in data['issues']]
return issues
def attach_worklogs(self, issues, start: Arrow, end: Arrow = arrow.now()):
for issue in issues:
data = self.get_all(f"/issue/{issue['key']}/worklog", 'worklogs')
worklogs = data['worklogs']
issue['timeSpentSeconds'] = 0
for record in worklogs:
if util.get_field(record, 'author', 'name') == self.USERNAME:
time_started = arrow.get(record['started'])
if start < time_started < end:
issue['timeSpentSeconds'] = issue['timeSpentSeconds'] + record['timeSpentSeconds']
def get_all(self, endpoint: str, what: str, params: dict[str, any] = None):
if params is None:
params = {}
url = self.API_URL + endpoint
data = rest.get(url, auth=self.AUTH, params=params).json()
util.debug_print('OK GET', endpoint)
if data['total'] > data['maxResults']:
for page in range(1, math.ceil(data['total'] / self.PAGE_SIZE)):
temp = rest.get(url, auth=self.AUTH, params={
**params,
'startAt': page * self.PAGE_SIZE
}).json()
util.debug_print('OK GET', endpoint)
data[what] += temp[what]
return data