Skip to content

Commit

Permalink
Move duplicate code to Service class
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 19, 2023
1 parent 307f45d commit 89fafff
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 130 deletions.
79 changes: 77 additions & 2 deletions services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,94 @@ def _not_found(self, url: str, tag: str) -> Issue:
raw={},
)

def _get_user_issues2( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = False,
created: bool = False,
involved: bool = True,
**kwargs,
) -> list[Issue] | None:
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_assigned_issues() -> list[Issue] | None:
return self.get_assigned(username, **kwargs)

def get_created_issues() -> list[Issue] | None:
return self.get_created(username, **kwargs)

with ThreadPoolExecutor(max_workers=2) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_assigned_issues))
if created:
futures.append(executor.submit(get_created_issues))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))

def _get_user_issues4( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = False,
created: bool = False,
involved: bool = True,
**kwargs,
) -> list[Issue] | None:
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_issues(is_pr: bool, is_assigned: bool) -> list[Issue] | None:
if is_assigned:
return self.get_assigned(username, pull_requests=is_pr, **kwargs)
return self.get_created(username, pull_requests=is_pr, **kwargs)

with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_issues, False, True))
futures.append(executor.submit(get_issues, True, True))
if created:
futures.append(executor.submit(get_issues, False, False))
futures.append(executor.submit(get_issues, True, False))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
This method must be overriden if get_issues() isn't overriden
"""
raise NotImplementedError(f"{self.__class__.__name__}: get_issue()")

def get_assigned(self, **_) -> list[Issue] | None:
def get_assigned(
self, username: str = "", **_ # pylint: disable=unused-argument
) -> list[Issue] | None:
"""
Get assigned issues
"""
return []

def get_created(self, **_) -> list[Issue] | None:
def get_created(
self, username: str = "", **_ # pylint: disable=unused-argument
) -> list[Issue] | None:
"""
Get created issues
"""
Expand Down
35 changes: 9 additions & 26 deletions services/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import logging
import os
from concurrent.futures import ThreadPoolExecutor
from typing import Any

from bugzilla import Bugzilla # type: ignore
Expand Down Expand Up @@ -78,37 +77,21 @@ def get_created(
def get_user_issues( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = True,
created: bool = True,
assigned: bool = False,
created: bool = False,
involved: bool = True,
**kwargs,
) -> list[Issue] | None:
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_assigned_issues() -> list[Issue] | None:
return self.get_assigned(username, **kwargs)

def get_created_issues() -> list[Issue] | None:
return self.get_created(username, **kwargs)

with ThreadPoolExecutor(max_workers=2) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_assigned_issues))
if created:
futures.append(executor.submit(get_created_issues))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))
return self._get_user_issues2(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
Expand Down
44 changes: 17 additions & 27 deletions services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import logging
import os
from concurrent.futures import ThreadPoolExecutor
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -47,7 +46,11 @@ def __del__(self):
pass

def get_assigned(
self, username: str = "", pull_requests: bool = False, state: str = "opened", **_
self,
username: str = "",
pull_requests: bool = False,
state: str = "opened",
**_,
) -> list[Issue] | None:
"""
Get assigned issues
Expand All @@ -74,7 +77,11 @@ def get_assigned(
return [self._to_issue(issue) for issue in issues]

def get_created(
self, username: str = "", pull_requests: bool = False, state: str = "opened", **_
self,
username: str = "",
pull_requests: bool = False,
state: str = "opened",
**_,
) -> list[Issue] | None:
"""
Get created issues
Expand Down Expand Up @@ -109,30 +116,13 @@ def get_user_issues( # pylint: disable=too-many-arguments
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_issues(is_pr: bool, is_assigned: bool) -> list[Issue] | None:
if is_assigned:
return self.get_assigned(username, pull_requests=is_pr, **kwargs)
return self.get_created(username, pull_requests=is_pr, **kwargs)

with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_issues, False, True))
futures.append(executor.submit(get_issues, True, True))
if created:
futures.append(executor.submit(get_issues, False, False))
futures.append(executor.submit(get_issues, True, False))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))
return self._get_user_issues4(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
Expand Down
33 changes: 8 additions & 25 deletions services/launchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import logging
from concurrent.futures import ThreadPoolExecutor
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -81,34 +80,18 @@ def get_user_issues( # pylint: disable=too-many-arguments
assigned: bool = False,
created: bool = False,
involved: bool = True,
**_,
**kwargs,
) -> list[Issue] | None:
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_assigned_issues() -> list[Issue] | None:
return self.get_assigned(username)

def get_created_issues() -> list[Issue] | None:
return self.get_created(username)

with ThreadPoolExecutor(max_workers=2) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_assigned_issues))
if created:
futures.append(executor.submit(get_created_issues))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))
return self._get_user_issues2(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
if not kwargs.get("repo"):
Expand Down
32 changes: 7 additions & 25 deletions services/pagure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import logging
from concurrent.futures import ThreadPoolExecutor
from typing import Any

from requests.exceptions import RequestException
Expand Down Expand Up @@ -125,30 +124,13 @@ def get_user_issues( # pylint: disable=too-many-arguments
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_issues(is_pr: bool, is_assigned: bool) -> list[Issue] | None:
if is_assigned:
return self.get_assigned(username, pull_requests=is_pr, **kwargs)
return self.get_created(username, pull_requests=is_pr, **kwargs)

with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_issues, False, True))
futures.append(executor.submit(get_issues, True, True))
if created:
futures.append(executor.submit(get_issues, False, False))
futures.append(executor.submit(get_issues, True, False))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))
return self._get_user_issues4(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)

def _to_issue(self, info: Any, **kwargs) -> Issue:
repo = kwargs.get("repo", "") or info["project"]["fullname"]
Expand Down
33 changes: 8 additions & 25 deletions services/redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import logging
import os
from concurrent.futures import ThreadPoolExecutor
from typing import Any

from redminelib import Redmine # type: ignore
Expand Down Expand Up @@ -67,34 +66,18 @@ def get_user_issues( # pylint: disable=too-many-arguments
assigned: bool = False,
created: bool = False,
involved: bool = True,
**_,
**kwargs,
) -> list[Issue] | None:
"""
Get user issues
"""
if involved:
assigned = created = True
all_issues: list[Issue] = []

def get_assigned_issues() -> list[Issue] | None:
return self.get_assigned(username)

def get_created_issues() -> list[Issue] | None:
return self.get_created(username)

with ThreadPoolExecutor(max_workers=2) as executor:
futures = []
if assigned:
futures.append(executor.submit(get_assigned_issues))
if created:
futures.append(executor.submit(get_created_issues))
for future in futures:
issues = future.result()
if issues is None:
return None
all_issues.extend(issues)

return list(set(all_issues))
return self._get_user_issues2(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
Expand Down

0 comments on commit 89fafff

Please sign in to comment.