-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira.py
167 lines (138 loc) · 6.56 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
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
import base64
import datetime
import requests
from jirameta import populate_meta
from mapping import map_issue_type, map_issue_priority, map_issue_state, map_issue_link
class Jira:
url = ""
headers = {}
meta = {}
issue_map = {}
def __init__(self, url):
self.url = url
def connect(self, user, password):
self.headers = {'Authorization': "Basic " + base64.b64encode((user + ":" + password).encode()).decode()}
r = requests.get(self.url + "/rest/auth/1/session", headers=self.headers)
if r.status_code == 200:
return self
else:
raise Exception("Error logging in: {}".format(r.status_code))
def does_project_exist(self, id):
r = requests.get(self.url + "/rest/api/2/project/" + id, headers=self.headers)
if r.status_code == 200:
return True
else:
return False
def get_field_id(self, name):
return self.meta['fields'][name]['id']
def transition_issue(self, project, issue):
r = requests.get(self.url + "/rest/api/2/issue/{}/transitions?expand=transitions.fields".format(issue.jira_id),
headers=self.headers)
if r.status_code != 200:
raise Exception("Error getting issue transitions: {} - {}".format(r.status_code, r.text))
transitions = r.json()['transitions']
new_state = map_issue_state(issue.State)
transition_id = None
for transition in transitions:
if transition['to']['name'] == new_state:
transition_id = transition['id']
if transition_id is None:
raise Exception("Error: No transition found to state {} (mapped from {})".format(new_state, issue.State))
r = requests.post(self.url + "/rest/api/2/issue/{}/transitions".format(issue.jira_id),
json={"transition": {"id": transition_id}},
headers=self.headers)
if r.status_code != 204:
raise Exception("Error changing issue state: {} - {}".format(r.status_code, r.text))
def create_comments(self, project, issue):
for comment in issue.comments:
text = comment['authorFullName'] + " (Youtrack) commented on " \
+ datetime.datetime.fromtimestamp(int(comment['created'][:-3])-10800).isoformat() + " (GMT):\n\n" \
+ comment['text']
r = requests.post(self.url + "/rest/api/2/issue/{}/comment".format(issue.jira_id),
json={'body': text},
headers=self.headers)
if r.status_code != 201:
raise Exception("Error creating comment: {} - {}".format(r.status_code, r.text))
project.no_comments += 1
def create_worklog(self, project, issue):
for log in issue.work:
data = {
"started": datetime.datetime.fromtimestamp(int(log['date'][:-3])).isoformat()+".000+0300",
"timeSpentSeconds": int(log['duration'])*60,
"comment": log['description']
}
r = requests.post(self.url + "/rest/api/2/issue/{}/worklog".format(issue.jira_id),
json=data,
headers=self.headers)
if r.status_code != 201:
raise Exception("Error creating worklog: {} - {}".format(r.status_code, r.text))
project.no_worklogs += 1
def create_issue(self, project, issue):
print(issue.__dict__)
data = {
"fields": {
"project": {
"key": project.id
},
"issuetype": {
"name": map_issue_type(issue.Type)
},
"summary": issue.summary,
"description": issue.description if 'description' in issue.__dict__.keys() else "",
"priority": {
"name": map_issue_priority(issue.Priority)
},
self.get_field_id("Youtrack ID"): issue.id,
self.get_field_id("Youtrack State"): {
"value": issue.State
},
self.get_field_id("Youtrack Created"):
datetime.datetime.fromtimestamp(int(issue.created[:-3])).isoformat()+".000+0300",
self.get_field_id("Youtrack Updated"):
datetime.datetime.fromtimestamp(int(issue.updated[:-3])).isoformat()+".000+0300",
self.get_field_id("Billed"): str(int(issue.Billedtime)/60) + " h"
if 'Billedtime' in issue.__dict__.keys() else ""
}
}
if 'resolved' in issue.__dict__.keys():
data['fields'][self.get_field_id("Youtrack Resolved")] = datetime.datetime.fromtimestamp(
int(issue.resolved[:-3])).isoformat() + ".000+0300"
r = requests.post(self.url + "/rest/api/2/issue", json=data, headers=self.headers)
if r.status_code != 201:
raise Exception("Error creating issue: {} - {}".format(r.status_code, r.text))
issue.jira_id = r.json()['id']
self.issue_map[issue.id] = issue.jira_id
project.no_issues += 1
self.transition_issue(project, issue)
self.create_comments(project, issue)
self.create_worklog(project, issue)
def create_links(self, project):
links = {}
for issue in project.issues:
if issue.links is None:
continue
for link in issue.links:
if (link['issue'], issue.id) not in links.keys():
links[(issue.id, link['issue'])] = {**link, 'from': issue.id}
for pair in links:
link = links[pair]
if map_issue_link(link['role'])[1]:
inwards = link['issue']
outwards = link['from']
else:
inwards = link['from']
outwards = link['issue']
data = {
"type": {"name": map_issue_link(link['role'])[0]},
"inwardIssue": {"id": self.issue_map[inwards]},
"outwardIssue": {"id": self.issue_map[outwards]}
}
r = requests.post(self.url + "/rest/api/2/issueLink", json=data, headers=self.headers)
if r.status_code != 201:
raise Exception("Error creating issue link: {} - {}".format(r.status_code, r.text))
project.no_links += 1
def sync_project(self, project):
populate_meta(self, project)
for issue in project.issues:
self.create_issue(project, issue)
self.create_links(project)