Skip to content

Commit

Permalink
Remove get_assigned() & get_created()
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Nov 8, 2023
1 parent b7e7f36 commit 6da47ca
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 466 deletions.
75 changes: 9 additions & 66 deletions services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,77 +182,20 @@ def _not_found(self, url: str, tag: str) -> Issue:
raw={},
)

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

futures = []
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
if assigned:
futures.append(executor.submit(self.get_assigned, **kwargs))
if created:
futures.append(executor.submit(self.get_created, **kwargs))
for future in concurrent.futures.as_completed(futures):
issues.extend(future.result())

return list(set(issues))

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

def _get_user_issues_x(self, queries: list[Any], **kwargs) -> list[Issue]:
issues = []
futures = []
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
if assigned:
for pr in (False, True):
futures.append(
executor.submit(self.get_assigned, pull_requests=pr, **kwargs)
)
if created:
for pr in (False, True):
futures.append(
executor.submit(self.get_created, pull_requests=pr, **kwargs)
)
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(queries)
) as executor:
for query in queries:
futures.append(executor.submit(self._get_user_issues, query, **kwargs))
for future in concurrent.futures.as_completed(futures):
issues.extend(future.result())

return list(set(issues))

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

@abstractmethod
def get_created(
self, username: str = "", **_ # pylint: disable=unused-argument
) -> list[Issue]:
"""
Get created issues
"""
def _get_user_issues(self, query: dict[str, Any], **kwargs) -> list[Issue]:
raise NotImplementedError("_get_user_issues()")

@abstractmethod
def get_user_issues(self, **_) -> list[Issue]:
Expand Down
60 changes: 13 additions & 47 deletions services/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,69 +35,38 @@ def __init__(self, url: str, creds: dict):
self.client._session._session.hooks["response"].append(debugme)

def close(self):
"""
Close session
"""
try:
self.client.disconnect()
except (AttributeError, BugzillaError):
pass

def get_assigned(
self, username: str = "", closed: bool = False, **_
) -> list[Issue]:
"""
Get assigned issues
"""
def _get_user_issues(self, query: dict[str, Any], **kwargs) -> list[Issue]:
closed = kwargs.get("closed", False)
try:
username = username or self.client.user
user = self.client.getuser(username)
issues = self.client.query({"assigned_to": user.email})
issues = self.client.query(query)
except (AttributeError, BugzillaError, RequestException) as exc:
logging.error("Bugzilla: %s: get_assigned(%s): %s", self.url, username, exc)
logging.error("Bugzilla: %s: get_user_issues(): %s", self.url, exc)
return []
if not closed:
issues = [issue for issue in issues if issue.is_open]
return [self._to_issue(issue) for issue in issues]

def get_created(self, username: str = "", closed: bool = False, **_) -> list[Issue]:
"""
Get created issues
"""
def get_user_issues(self, username: str = "", **kwargs) -> list[Issue]:
try:
username = username or self.client.user
user = self.client.getuser(username)
issues = self.client.query({"reporter": user.email})
except (AttributeError, BugzillaError, RequestException) as exc:
logging.error("Bugzilla: %s: get_created(%s): %s", self.url, username, exc)
logging.error(
"Bugzilla: %s: get_user_issues(%s): %s", self.url, username, exc
)
return []
if not closed:
issues = [issue for issue in issues if issue.is_open]
return [self._to_issue(issue) for issue in issues]

def get_user_issues( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = False,
created: bool = False,
involved: bool = True,
**kwargs,
) -> list[Issue]:
"""
Get user issues
"""
return self._get_user_issues2(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)
queries = [
{"assigned_to": user.email},
{"reporter": user.email},
]
return self._get_user_issues_x(queries, **kwargs)

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
Get Bugzilla issue
"""
try:
return self._to_issue(self.client.getbug(issue_id))
except IndexError:
Expand All @@ -110,9 +79,6 @@ def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
return None

def get_issues(self, issues: list[dict]) -> list[Issue | None]:
"""
Get Bugzilla issues
"""
try:
found = [
self._to_issue(info)
Expand Down
76 changes: 17 additions & 59 deletions services/gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ def _get_paginated(self, url: str, params: dict[str, str] | None) -> list[dict]:
entries.extend(self._get_paginated2(url, params, last_page))
return entries

# Get pages 2 to last using threads
def _get_paginated2(
self, url: str, params: dict[str, str], last_page: int
) -> list[dict]:
"""
Get pages 2 to last using threads
"""
entries: list[dict] = []

def get_page(page: int) -> list[dict]:
Expand All @@ -80,68 +78,28 @@ def get_page(page: int) -> list[dict]:

return entries

# Not possible to filter issues by username because of:
# https://github.com/go-gitea/gitea/issues/25979

def _get_issues( # pylint: disable=too-many-arguments
self,
assigned: bool = False,
created: bool = False,
closed: bool = False,
) -> list[Issue]:
params: dict[str, Any] = {
"state": "closed" if closed else "open",
}
# Missing: mentioned, review_requested & reviewed
if assigned:
params["assigned"] = True
if created:
params["created"] = True
def _get_user_issues(self, query: dict[str, Any], **kwargs) -> list[Issue]:
closed = kwargs.get("closed", False)
query["state"] = "closed" if closed else "open"
issues = self._get_paginated(
f"{self.url}/api/v1/repos/issues/search", params=params
f"{self.url}/api/v1/repos/issues/search", params=query
)
return [
self._to_issue(issue, is_pr=bool(issue["pull_request"])) for issue in issues
]

def get_assigned(self, username: str = "", state: str = "open", **_) -> list[Issue]:
"""
Get assigned issues
"""
try:
return self._get_issues(assigned=True, closed=bool(state != "open"))
except RequestException as exc:
logging.error("Gitea: %s: get_assigned(%s): %s", self.url, username, exc)
return []

def get_created(self, username: str = "", state: str = "open", **_) -> list[Issue]:
"""
Get created issues
"""
try:
return self._get_issues(created=True, closed=bool(state != "open"))
except RequestException as exc:
logging.error("Gitea: %s: get_created(%s): %s", self.url, username, exc)
return []

def get_user_issues( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = False,
created: bool = False,
involved: bool = True,
**kwargs,
) -> list[Issue]:
"""
Get user issues
"""
return self._get_user_issues2(
username=username,
assigned=assigned,
created=created,
involved=involved,
**kwargs,
)
def get_user_issues(self, username: str = "", **kwargs) -> list[Issue]:
# Not possible to filter issues by username because of:
# https://github.com/go-gitea/gitea/issues/25979
_ = username
queries = [
{"assigned": True},
{"created": True},
{"mentioned": True},
{"reviewed": True},
{"review_requested": True},
]
return self._get_user_issues_x(queries, **kwargs)

def _to_issue(self, info: Any, **kwargs) -> Issue:
repo = kwargs.get("repo", info["repository"]["full_name"])
Expand Down
48 changes: 4 additions & 44 deletions services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,61 +31,24 @@ def __init__(self, url: str, creds: dict):
logging.getLogger("github").setLevel(logging.DEBUG)

def close(self):
"""
Close session
"""
try:
self.client.close()
except (AttributeError, GithubException):
pass

def get_assigned(self, username: str = "", state: str = "open", **_) -> list[Issue]:
"""
Get assigned issues
"""
return self.get_user_issues(
username, assigned=True, involved=False, state=state
)

def get_created(self, username: str = "", state: str = "open", **_) -> list[Issue]:
"""
Get created issues
"""
return self.get_user_issues(username, created=True, involved=False, state=state)

def get_user_issues( # pylint: disable=too-many-arguments
self,
username: str = "",
assigned: bool = False,
created: bool = False,
involved: bool = True,
state: str = "open",
**_,
) -> list[Issue]:
"""
Get user issues
"""
filters = f"state:{state} "
if involved:
filters += "involves:"
elif assigned:
filters += "assignee:"
elif created:
filters += "author:"
def get_user_issues(self, username: str = "", **kwargs) -> list[Issue]:
state = kwargs.get("state", "open")
filters = f"state:{state} involves:"
try:
user = (
self.client.get_user(username) if username else self.client.get_user()
)
filters += user.login
except (GithubException, RequestException) as exc:
logging.error("Github: get_user_issues(%s): %s", username, exc)
return []

try:
issues = self.client.search_issues(filters)
except (GithubException, RequestException) as exc:
logging.error("Github: get_user_issues(%s): %s", username, exc)
return []

return [
self._to_issue(
issue, is_pr=bool(issue.html_url.rsplit("/", 2)[1] == "pull")
Expand All @@ -94,9 +57,6 @@ def get_user_issues( # pylint: disable=too-many-arguments
]

def get_issue(self, issue_id: str = "", **kwargs) -> Issue | None:
"""
Get Github issue
"""
repo: str = kwargs.pop("repo")
is_pr: bool = kwargs.pop("is_pr")
mark = "!" if is_pr else "#"
Expand Down
Loading

0 comments on commit 6da47ca

Please sign in to comment.