-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira_api_handler.py
65 lines (51 loc) · 2.08 KB
/
jira_api_handler.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
import requests
import json
import base64
from requests.auth import HTTPBasicAuth
# Inorder to encrypt/decrypt your credentials using base64 module as below.
# To encode --> base64.b64encode(bytes("random", "utf-8"))
# To decode --> base64.b64decode("cmFuZG9t").decode("utf-8")
class JiraHandler:
def __init__(self, username):
print('Loading Instnace variables...')
self.username = username
self.securestring = base64.b64decode("replaceItwithYourCredential").decode("utf-8")
self.url = "https://jira.yourownjiradomain.com/rest/api/2/issue/"
def get_jira_details(self,jira_ticket):
try :
auth = HTTPBasicAuth(self.username, self.securestring)
get_details_url = self.url + jira_ticket
headers = {
"Accept": "application/json"
}
print("retrieveing", jira_ticket, "details...")
response = requests.request(
"GET",
get_details_url,
headers=headers,
auth=auth
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
except Exception as e:
return e
def create_jira_cr_ticket(self, filename):
get_details_url = self.url
auth = HTTPBasicAuth(self.username, self.securestring)
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
try:
with open(filename, "r") as re_read_file:
payload = re_read_file.read()
print("Creating new JIRA...")
response = requests.request(
"POST",
get_details_url,
data=payload,
headers=headers,
auth=auth
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
except Exception as filenotfound:
print("Can't load file..", filenotfound)