-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjira_helper.py
72 lines (59 loc) · 2.56 KB
/
jira_helper.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
import json
import os
import sys
import time
import re
try:
from jira import JIRA
except:
pass
class Jira():
def __init__(self):
self.connect()
def connect(self):
username = os.getenv("JIRA_USER")
password = os.getenv("JIRA_PWD")
if not username or not password:
print("get jira user info error, please configure jira user credential")
return
try:
self.jira = JIRA(server='https://jira.realtek.com', basic_auth=(username, password))
print("Connect jira successed")
except Exception as e:
print('ERROR: connect jira error {}'.format(e))
def disconnect(self):
try:
self.jira.kill_session()
except Exception as e:
print('ERROR: disconnect jira error {}'.format(e))
def download_attachements_by_jira_id(self, jira_id, file_name, download_path, check_status=True):
task_issue = self.jira.issue(jira_id)
attachments = sorted(task_issue.fields.attachment, key=lambda attachment: attachment.created, reverse=True)
for attachment in attachments:
if attachment.filename == file_name:
break
else:
if check_status:
sys.exit('cannot found {} in {}'.format(file_name, task_issue.key))
else:
print('cannot found {} in {}'.format(file_name, task_issue.key))
dest_file_full_path = os.path.join(download_path, attachment.filename)
with open(dest_file_full_path, 'wb') as f:
print('download attachment {}, created in {}.'.format(attachment.filename, attachment.created))
f.write(attachment.get())
return dest_file_full_path
def find_packages_from_jira(self, jira_id, pattern):
package_list = list()
task_issue = self.jira.issue(jira_id)
attachments = sorted(task_issue.fields.attachment, key=lambda attachment: attachment.created, reverse=True)
for attachment in attachments:
if re.search(pattern, attachment.filename):
package_list.append(attachment.filename)
return package_list
def get_attachment_link(self, jira_id, file_name):
task_issue = self.jira.issue(jira_id)
attachments = sorted(task_issue.fields.attachment, key=lambda attachment: attachment.created, reverse=True)
for attachment in attachments:
if file_name == attachment.filename:
return attachment.content
return ""