Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR adds Jira as an option #61

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
jira_actions.py contains the functions like snow.py
  • Loading branch information
mattdough committed Nov 21, 2021
commit fd2631d2b1502a0774b7ae860a69ba320070a77f
1 change: 1 addition & 0 deletions actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# TODO read helpdesk service from endpoints.yml
localmode = snow.localmode
# TODO instantiante either snow or jira or nothing for local
# TODO
snow = SnowAPI()

logger.debug(f"Local mode: {snow.localmode}")
Expand Down
109 changes: 109 additions & 0 deletions actions/jira_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import logging
import pathlib
import ruamel.yaml
from typing import Dict, Text, Any
from jira import (
JIRA,
) # requires jira 3.1.0rc1 for the search_user function to work
from jira.resources import Customer, Priority, User

logger = logging.getLogger(__name__)

here = pathlib.Path(__file__).parent.absolute()


class JiraPy(object):
def __init__(self):
jira_config = (
ruamel.yaml.safe_load(open(f"{here}/jira_credentials.yml", "r"))
or {}
)

self.jira_user = jira_config.get("jira_user")
self.jira_token = jira_config.get("jira_token")
self.jira_url = jira_config.get("jira_url")
self.project = jira_config.get("project")
self.jira_obj = JIRA(
self.jira_url, basic_auth=(self.jira_user, self.jira_token)
)

def handle_request(self):
# TODO This might not be needed. In the service now version it isn't called directly by actions
True

def email_to_sysid(self, email) -> Dict[Text, Any]:
result = {}
email_result = self.jira_obj.search_users(
None, 0, 10, True, False, email
)
if len(email_result) == 1:
result["account_id"] = vars(email_result[0]).get("accountId")
else:
result["account_id"] = []
result["error"] = (
f"Could not retreive account id; "
f"Multiple records found for email {email}"
)

return result

def retrieve_incidents(self, email) -> Dict[Text, Any]:
result = {}
incidents = {}
email_result = self.email_to_sysid(email)
account_id = email_result.get("account_id")
issues = self.jira_obj.search_issues(
f"reporter in ({account_id}) order by created DESC"
)
if account_id:
for issue in issues:
incidents[issue.key] = issue.fields.summary
elif isinstance(issues, list):
result["error"] = f"No incidents on record for {email}"

result["account_id"] = account_id
result["incidents"] = incidents
return result

def create_incident(
self, description, short_description, priority, email
) -> Dict[Text, Any]:
project = self.project
account_id = self.email_to_sysid(email).get("account_id")
print(account_id)
issue = self.jira_obj.create_issue(
project=project,
summary=short_description,
description=description,
issuetype={"id": "10002"},
priority={"id": priority},
reporter={"accountId": account_id},
)
return issue

# TODO need to use this for setting priority
@staticmethod
def priority_db() -> Dict[str, int]:
"""Database of supported priorities"""
priorities = {"low": 4, "medium": 3, "high": 2}
return priorities


jira = JiraPy()

id = jira.email_to_sysid("[email protected]")
print((id))

new_issue = jira.create_incident(
"function call with email", "test out eamil", "3", "[email protected]"
)

print(new_issue.fields.project.key)
print(new_issue)
print(new_issue.fields.issuetype.name)
print(new_issue.fields.reporter)
print(new_issue.fields.summary)
print(new_issue.fields.comment.comments)

issues = jira.retrieve_incidents("[email protected]")
print(issues)
5 changes: 5 additions & 0 deletions actions/jira_credentials.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
jira_user: [email protected]
jira_token: q514H7eXTyXDIp7D8X2vB222
jira_url: https://rcbot.atlassian.net
project:
id : '10000'
2 changes: 2 additions & 0 deletions endpoints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ action_endpoint:
# username: username
# password: password
# queue: queue
helpdeask_service:
service: localmode #snow, jira
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rasa~=2.8.0
rasa-sdk~=2.8.0 # if you change this, make sure to change the Dockerfile to match
rasa~=2.8.12
rasa-sdk~=2.8.2 # if you change this, make sure to change the Dockerfile to match
-r actions/requirements-actions.txt
jira~=3.1.0rc1