From cb2a1b19d13ce0a85053b6cae1bf58cbf564c611 Mon Sep 17 00:00:00 2001 From: Johannes Wesch Date: Fri, 12 Apr 2024 10:29:45 +0200 Subject: [PATCH] WIP: jira client integration --- .../issue_dispatcher/jira_adapter.py | 12 +++++ .../test/test_jira_adapter.py | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/examples/issue_classification_user_journey/issue_dispatcher/jira_adapter.py diff --git a/src/examples/issue_classification_user_journey/issue_dispatcher/jira_adapter.py b/src/examples/issue_classification_user_journey/issue_dispatcher/jira_adapter.py new file mode 100644 index 000000000..1a2861d96 --- /dev/null +++ b/src/examples/issue_classification_user_journey/issue_dispatcher/jira_adapter.py @@ -0,0 +1,12 @@ +import os +from dotenv import load_dotenv +from jira import JIRA + + +def jira_client() -> JIRA: + load_dotenv() + url = os.environ.get("JIRA_API_URL") + token = os.environ.get("JIRA_API_TOKEN") + mail = os.environ.get("JIRA_API_MAIL") + + return JIRA(url, basic_auth=(mail, token)) \ No newline at end of file diff --git a/src/examples/issue_classification_user_journey/test/test_jira_adapter.py b/src/examples/issue_classification_user_journey/test/test_jira_adapter.py index 74cdfd3d8..1729769ec 100644 --- a/src/examples/issue_classification_user_journey/test/test_jira_adapter.py +++ b/src/examples/issue_classification_user_journey/test/test_jira_adapter.py @@ -1,4 +1,10 @@ +import os +from typing import Sequence +from dotenv import load_dotenv +from jira import JIRA +from jira import Issue as JiraIssue from examples.issue_classification_user_journey.issue_dispatcher.issue_dispatcher import ClassifierPort, Issue, IssueAssigner +from examples.issue_classification_user_journey.issue_dispatcher.jira_adapter import jira_client class AlwaysSameClass(ClassifierPort): @@ -7,11 +13,52 @@ def __init__(self, label: str): def classify(self, _issue: Issue) -> str: return self.label + + + + + +def board_with_tickets(jira_client: JIRA, project_id: str, issue_titles: list[str]) -> Sequence[Issue]: + old_issues = jira_client.search_issues(f"project={project_id}") + for issue in old_issues: + assert isinstance(issue, JiraIssue) + issue.delete() # type: ignore + + issues = [] + for title in issue_titles: + issues.append( + jira_client.create_issue( + project=project_id, + summary=title, + issuetype={"name": "Bug"}, + ) + ) + return issues def test_should_send_new_issues_to_issue_assigner(): # Given classifier_stub = AlwaysSameClass("IT") issue_assigner = IssueAssigner(classifier_stub) + issue_titles = ["My monitor is not working."] + project_id = os.environ.get("JIRA_PROJECT_ID") + jira = jira_client() + created_issues = board_with_tickets(jira, project_id, issue_titles) + + # When + issues = jira.search_issues(f"project={project_id}") + issue = issues[0] + issue = Issue(issue.id, issue.fields.summary) + label = issue_assigner.assign_issues([issue])[issue.id] + + jira.issue(issue.id).update(fields={"labels": [label]}) + + + +def test_should_connect_to_jira(): + jira = jira_client() + boards = jira.boards() + assert len(boards) >= 1 + \ No newline at end of file