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 re
import threading
from enum import Enum, auto
from functools import reduce, partial
from queue import Queue, Empty
from loguru import logger
from requests import Response
try:
from PySide6 import QtCore, QtWidgets
@ -31,6 +33,8 @@ class TaskCompleter(QtWidgets.QCompleter):
self.config = config
self.picker_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.text = ""
self.response_text = ""
@ -48,6 +52,7 @@ class TaskCompleter(QtWidgets.QCompleter):
def update_urls(self):
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.issue_url_tmpl = os.path.join(self.config.jira_url, "rest/api/2/issue/{}")
@QtCore.Slot()
def process_response(self):
@ -80,6 +85,7 @@ class TaskCompleter(QtWidgets.QCompleter):
return
if self.escalate:
self.update_search()
self.update_issue()
if not self.update_timer.isActive():
self.update_timer.start()
future = self.session.get(
@ -113,6 +119,7 @@ class TaskCompleter(QtWidgets.QCompleter):
logger.debug("No picker results. Escalating")
self.escalate = True
self.update_search()
self.update_issue()
except Exception:
logger.exception("Ignoring exception, as it only breaks autocompletion")
return
@ -150,3 +157,35 @@ class TaskCompleter(QtWidgets.QCompleter):
except Exception:
logger.exception("Ignoring exception, as it only breaks autocompletion")
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