Improve task import, by assuming input is an issue key

This commit is contained in:
Fabian 2023-01-19 21:30:47 +01:00
parent 72a3291d6b
commit d177f7d95d
2 changed files with 40 additions and 1 deletions

View File

@ -1,10 +1,12 @@
import os import os
import re
import threading import threading
from enum import Enum, auto from enum import Enum, auto
from functools import reduce, partial from functools import reduce, partial
from queue import Queue, Empty from queue import Queue, Empty
from loguru import logger from loguru import logger
from requests import Response
try: try:
from PySide6 import QtCore, QtWidgets from PySide6 import QtCore, QtWidgets
@ -31,6 +33,8 @@ class TaskCompleter(QtWidgets.QCompleter):
self.config = config self.config = config
self.picker_url = None self.picker_url = None
self.search_url = None self.search_url = None
self.issue_url_tmpl = None
self.issue_key_regex = re.compile(r"^[a-zA-Z0-9]+-[0-9]+")
self.update_urls() self.update_urls()
self.text = "" self.text = ""
self.response_text = "" self.response_text = ""
@ -48,6 +52,7 @@ class TaskCompleter(QtWidgets.QCompleter):
def update_urls(self): def update_urls(self):
self.picker_url = os.path.join(self.config.jira_url, "rest/api/2/issue/picker") self.picker_url = os.path.join(self.config.jira_url, "rest/api/2/issue/picker")
self.search_url = os.path.join(self.config.jira_url, "rest/api/2/search") self.search_url = os.path.join(self.config.jira_url, "rest/api/2/search")
self.issue_url_tmpl = os.path.join(self.config.jira_url, "rest/api/2/issue/{}")
@QtCore.Slot() @QtCore.Slot()
def process_response(self): def process_response(self):
@ -80,6 +85,7 @@ class TaskCompleter(QtWidgets.QCompleter):
return return
if self.escalate: if self.escalate:
self.update_search() self.update_search()
self.update_issue()
if not self.update_timer.isActive(): if not self.update_timer.isActive():
self.update_timer.start() self.update_timer.start()
future = self.session.get( future = self.session.get(
@ -113,6 +119,7 @@ class TaskCompleter(QtWidgets.QCompleter):
logger.debug("No picker results. Escalating") logger.debug("No picker results. Escalating")
self.escalate = True self.escalate = True
self.update_search() self.update_search()
self.update_issue()
except Exception: except Exception:
logger.exception("Ignoring exception, as it only breaks autocompletion") logger.exception("Ignoring exception, as it only breaks autocompletion")
return return
@ -150,3 +157,35 @@ class TaskCompleter(QtWidgets.QCompleter):
except Exception: except Exception:
logger.exception("Ignoring exception, as it only breaks autocompletion") logger.exception("Ignoring exception, as it only breaks autocompletion")
return return
def update_issue(self):
stripped = self.text.strip()
if not self.issue_key_regex.match(stripped):
return
future = self.session.get(
self.issue_url_tmpl.format(stripped.upper()),
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
)
with self.rif_counter_lock:
self.rif_counter += 1
future.add_done_callback(partial(self.issue_response_callback, self.text))
def issue_response_callback(self, text: str, future):
with self.rif_counter_lock:
self.rif_counter -= 1
try:
resp: Response = future.result()
if resp.status_code < 199 or resp.status_code >= 300:
return
json_result = resp.json()
extracted = f'{text.strip().upper()} {json_result["fields"]["summary"]}'
self.queue.put({
"response_text": text,
"result": extracted,
})
except Exception:
logger.exception("Ignoring exception, as it only breaks autocompletion")
return