From 01784ab7d065e2955df6cc8c1693c4b1f1a37e6b Mon Sep 17 00:00:00 2001 From: Mathieu Dubois-Briand Date: Thu, 17 Oct 2024 16:07:34 +0200 Subject: [PATCH] Allow to get title of any bug --- swattool/bugzilla.py | 29 ++++++++++++++++++++++++++++- swattool/userdata.py | 5 ++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/swattool/bugzilla.py b/swattool/bugzilla.py index 2179b52..992a6e8 100644 --- a/swattool/bugzilla.py +++ b/swattool/bugzilla.py @@ -5,6 +5,7 @@ import urllib import logging import json +from typing import Optional from .webrequests import Session @@ -17,6 +18,8 @@ class Bugzilla: """Bugzilla server interaction class.""" + CACHE_TIMEOUT_S = 60 * 10 + known_abints: dict[int, str] = {} @classmethod @@ -47,7 +50,7 @@ def get_abints(cls) -> dict[int, str]: fparams = urllib.parse.urlencode(params, doseq=True) req = f"{REST_BASE_URL}bug?{fparams}" - data = Session().get(req, 600) + data = Session().get(req, cls.CACHE_TIMEOUT_S) cls.known_abints = {bug['id']: bug['summary'] for bug in json.loads(data)['bugs']} @@ -59,6 +62,30 @@ def get_bug_url(cls, bugid: int) -> str: """Get the bugzilla URL corresponding to a given issue ID.""" return f"{BASE_URL}/show_bug.cgi?id={bugid}" + @classmethod + def get_bug_title(cls, bugid: int) -> Optional[str]: + """Get bugzilla bug title.""" + abints = cls.get_abints() + if bugid in abints: + return abints[bugid] + + # order=order=bug_id DESC&query_format=advanced&bug_id=15614 + params = { + 'order': 'order=bug_id%20DESC', + 'query_format': 'advanced', + 'bug_id': bugid, + } + + fparams = urllib.parse.urlencode(params, doseq=True) + req = f"{REST_BASE_URL}bug?{fparams}" + data = Session().get(req, cls.CACHE_TIMEOUT_S) + + jsondata = json.loads(data)['bugs'] + if len(jsondata) != 1: + return None + + return jsondata[0]['summary'] + @classmethod def add_bug_comment(cls, bugid: int, comment: str): """Publish a new comment to a bugzilla issue.""" diff --git a/swattool/userdata.py b/swattool/userdata.py index 4366207..4150389 100644 --- a/swattool/userdata.py +++ b/swattool/userdata.py @@ -57,7 +57,6 @@ def __str__(self): def format_description(self) -> str: """Get info on one given Triage in a pretty way.""" - abints = Bugzilla.get_abints() statusfrags = [] statusname = self.status.name.title() @@ -65,8 +64,8 @@ def format_description(self) -> str: if self.status == swatbotrest.TriageStatus.BUG: bugid = int(self.comment) - if bugid in abints: - bugtitle = abints[bugid] + bugtitle = Bugzilla.get_bug_title(bugid) + if bugtitle: statusfrags.append(f", {bugtitle}") bzcomment = self.extra.get('bugzilla-comment')