-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira_actions.py
67 lines (53 loc) · 2.18 KB
/
jira_actions.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
from jira import JIRA
from .exceptions import JiraException, ActiveJiraIssue
JIRA_BASE_URL = "<JIRA_BASE_URL>"
PERSONAL_ACCESS_TOKEN = "<YOUR-PERSONAL-ACCESS-TOKEN>"
PROJECT_KEY = "<YOUR_PROJECT_KEY>"
# Replace with your Jira instance URL and Personal Access Token (PAT)
def set_issue_data(description, client_id, issue_type, creation_date):
issue_summary = f"{PROJECT_KEY}, Desc: {description.split()[0]}... by user {client_id}" \
f" in {creation_date}"
issue_data = {
"project": {"key": PROJECT_KEY},
"summary": issue_summary,
"description": description,
"issuetype": {"name": issue_type},
}
return issue_data
class JiraInstance:
def __init__(self):
self.host = JIRA_BASE_URL
self.pat = PERSONAL_ACCESS_TOKEN
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
headers["Authorization"] = f"Bearer {self.pat}"
jira = JIRA(server=self.host, options={"headers": headers})
self.jira = jira
def create_new_issue(self, issue_data):
try:
# Create the issue
new_issue = self.jira.create_issue(fields=issue_data)
# Print the key of the created issue
print("Issue created successfully!")
print("Issue Key:", new_issue.key)
except Exception as e:
print("Failed to create issue:", str(e))
def create_issue_from_backend(self, description, client_id, issue_type, creation_date):
issue_data = set_issue_data(description, client_id, issue_type, creation_date)
try:
self.create_new_issue(issue_data)
except Exception as e:
raise JiraException
def load_issue_by_user_and_status(jira_instance, user, status):
try:
issues = jira_instance.jira.search_issues(f'assignee = {user} AND status = {status}')
if issues:
print(f"In Jira Actions, The user is {user} and the status is {status}")
raise ActiveJiraIssue
else:
return issues
except ActiveJiraIssue as e:
print(e)
raise JiraException
except Exception as other_jira_exception:
print(other_jira_exception)
raise JiraException