diff --git a/src/fime/task_completer.py b/src/fime/task_completer.py index d539ddf..ad1471d 100644 --- a/src/fime/task_completer.py +++ b/src/fime/task_completer.py @@ -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 diff --git a/src/fime/worklog_rest.py b/src/fime/worklog_rest.py index 9a788fc..a04a2e9 100644 --- a/src/fime/worklog_rest.py +++ b/src/fime/worklog_rest.py @@ -59,7 +59,7 @@ class WorklogRest: ret.append((*self._issue_state[issue_key], prev_comment)) return ret - def _req_issue(self, issue_key: str, pdate :date): + def _req_issue(self, issue_key: str, pdate: date): future = self.session.get( self.issue_url.format(issue_key), headers={