Skip to content

Commit

Permalink
Support tags in output
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Sep 11, 2023
1 parent 87a34a9 commit 27b6561
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
39 changes: 18 additions & 21 deletions bugme.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def get_item(string: str) -> Item | None:
item_id=int(issue),
host=CODE_TO_HOST[code],
repo=repo,
tag=string,
)
except KeyError:
logging.warning("Unsupported %s", string)
Expand Down Expand Up @@ -166,26 +167,14 @@ def get_item(self, item_id: int = -1, **kwargs) -> Item | None:
"""
Get Bugzilla item
"""
tag = kwargs.get("tag", "")
try:
return self._to_item(self.client.getbug(item_id))
return self._to_item(self.client.getbug(item_id), tag)
except (AttributeError, BugzillaError, RequestException) as exc:
logging.error("Bugzilla: %s: get_item(%d): %s", self.url, item_id, exc)
return None

def get_items(self, items: list[dict]) -> list[Item | None]:
"""
Get Bugzilla items
"""
try:
return [
self._to_item(info)
for info in self.client.getbugs([item["item_id"] for item in items])
]
except (AttributeError, BugzillaError, RequestException) as exc:
logging.error("Bugzilla: %s: get_items(): %s", self.url, exc)
return []

def _to_item(self, info: Any) -> Item | None:
def _to_item(self, info: Any, tag: str) -> Item | None:
return Item(
id=info.id,
status=info.status,
Expand All @@ -194,6 +183,7 @@ def _to_item(self, info: Any) -> Item | None:
updated=info.last_change_time,
url=f"{self.url}/show_bug.cgi?id={info.id}",
extra=info.__dict__,
tag=tag,
)


Expand All @@ -214,14 +204,15 @@ def get_item(self, item_id: int = -1, **kwargs) -> Item | None:
Get Github issue
"""
repo = kwargs.pop("repo")
tag = kwargs.get("tag", "")
try:
info = self.client.get_repo(repo, lazy=True).get_issue(item_id)
except (GithubException, RequestException) as exc:
logging.error("Github: get_issue(%s, %s): %s", repo, item_id, exc)
return None
return self._to_item(info, repo)
return self._to_item(info, repo, tag)

def _to_item(self, info: Any, repo: str) -> Item | None:
def _to_item(self, info: Any, repo: str, tag: str) -> Item | None:
return Item(
id=info.number,
status=info.state,
Expand All @@ -230,6 +221,7 @@ def _to_item(self, info: Any, repo: str) -> Item | None:
updated=info.updated_at,
url=f"{self.url}/{repo}/issues/{info.number}",
extra=info.__dict__["_rawData"],
tag=tag,
)


Expand All @@ -254,16 +246,17 @@ def get_item(self, item_id: int = -1, **kwargs) -> Item | None:
Get Gitlab issue
"""
repo = kwargs.pop("repo")
tag = kwargs.get("tag", "")
try:
info = self.client.projects.get(repo, lazy=True).issues.get(item_id)
except (GitlabError, RequestException) as exc:
logging.error(
"Gitlab: %s: get_issue(%s, %s): %s", self.url, repo, item_id, exc
)
return None
return self._to_item(info, repo)
return self._to_item(info, repo, tag)

def _to_item(self, info: Any, repo: str) -> Item | None:
def _to_item(self, info: Any, repo: str, tag: str) -> Item | None:
return Item(
id=info.iid,
status=info.state,
Expand All @@ -272,6 +265,7 @@ def _to_item(self, info: Any, repo: str) -> Item | None:
updated=info.updated_at,
url=f"{self.url}/{repo}/-/issues/{info.iid}",
extra=info.asdict(),
tag=tag,
)


Expand All @@ -288,14 +282,15 @@ def get_item(self, item_id: int = -1, **kwargs) -> Item | None:
"""
Get Redmine ticket
"""
tag = kwargs.get("tag", "")
try:
info = self.client.issue.get(item_id)
except (BaseRedmineError, RequestException) as exc:
logging.error("Redmine: %s: get_issue(%d): %s", self.url, item_id, exc)
return None
return self._to_item(info)
return self._to_item(info, tag)

def _to_item(self, info: Any) -> Item | None:
def _to_item(self, info: Any, tag: str) -> Item | None:
return Item(
id=info.id,
status=info.status.name,
Expand All @@ -304,6 +299,7 @@ def _to_item(self, info: Any) -> Item | None:
updated=info.updated_on,
url=f"{self.url}/issues/{info.id}",
extra=info.raw(),
tag=tag,
)


Expand Down Expand Up @@ -398,6 +394,7 @@ def main() -> None: # pylint: disable=too-many-branches
continue
item.created = dateit(item.created, args.time)
item.updated = dateit(item.updated, args.time)
item.tag = item.tag or item.url
if args.output == "text":
if args.format:
print(Template(args.format).render(item.__dict__))
Expand Down
16 changes: 11 additions & 5 deletions test_bugme.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,43 @@ def test_Item():
def test_get_item_with_bsc_format():
string = "bsc#1213811"
item = get_item(string)
expected_item = Item(item_id=1213811, host="bugzilla.suse.com", repo="")
expected_item = Item(item_id=1213811, host="bugzilla.suse.com", repo="", tag=string)
assert item.__dict__ == expected_item.__dict__


def test_get_item_with_gh_format():
string = "gh#containers/podman#19529"
item = get_item(string)
expected_item = Item(item_id=19529, host="github.com", repo="containers/podman")
expected_item = Item(
item_id=19529, host="github.com", repo="containers/podman", tag=string
)
assert item.__dict__ == expected_item.__dict__


def test_get_item_with_gl_format():
string = "gl#gitlab-org/gitlab#424503"
item = get_item(string)
expected_item = Item(item_id=424503, host="gitlab.com", repo="gitlab-org/gitlab")
expected_item = Item(
item_id=424503, host="gitlab.com", repo="gitlab-org/gitlab", tag=string
)
assert item.__dict__ == expected_item.__dict__


def test_get_item_with_gsd_format():
string = "gsd#qac/container-release-bot#7"
item = get_item(string)
expected_item = Item(
item_id=7, host="gitlab.suse.de", repo="qac/container-release-bot"
item_id=7, host="gitlab.suse.de", repo="qac/container-release-bot", tag=string
)
assert item.__dict__ == expected_item.__dict__


def test_get_item_with_poo_format():
string = "poo#133910"
item = get_item(string)
expected_item = Item(item_id=133910, host="progress.opensuse.org", repo="")
expected_item = Item(
item_id=133910, host="progress.opensuse.org", repo="", tag=string
)
assert item.__dict__ == expected_item.__dict__


Expand Down

0 comments on commit 27b6561

Please sign in to comment.