Skip to content

Commit

Permalink
Add issue/pr user state filters to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Sep 2, 2024
1 parent 14231c4 commit a8d28fe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
16 changes: 14 additions & 2 deletions lazy_github/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@

_CONFIG_FILE_LOCATION = CONFIG_FOLDER / "config.json"

PR_STATE_FILTER = Literal["all"] | Literal["open"] | Literal["closed"]
ISSUE_STATE_FILTER = Literal["all"] | Literal["open"] | Literal["closed"]
ISSUE_OWNER_FILTER = Literal["mine"] | Literal["all"]


class ApiConfig(BaseModel):
base_url: str = "https://api.github.com"


class PullRequestSettings(BaseModel):
state_filter: PR_STATE_FILTER = "all"
"""Changes how PRs are retrieved from the Github API"""

state_filter: ISSUE_STATE_FILTER = "all"
owner_filter: ISSUE_OWNER_FILTER = "all"


class IssueSettings(BaseModel):
"""Changes how issues are retrieved from the Github API"""

state_filter: ISSUE_STATE_FILTER = "all"
owner_filter: ISSUE_OWNER_FILTER = "all"


class RepositorySettings(BaseModel):
Expand All @@ -41,6 +52,7 @@ class Config(BaseModel):
appearance: AppearanceSettings = Field(default=AppearanceSettings(), alias="appearence")
repositories: RepositorySettings = RepositorySettings()
pull_requests: PullRequestSettings = PullRequestSettings()
issues: IssueSettings = IssueSettings()
cache: CacheSettings = CacheSettings()
api: ApiConfig = ApiConfig()

Expand Down
15 changes: 8 additions & 7 deletions lazy_github/lib/github/issues.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from functools import partial
from typing import Literal

from lazy_github.lib.github.client import GithubClient
from lazy_github.models.github import Issue, IssueComment, PartialPullRequest, Repository

IssueStateFilter = Literal["open"] | Literal["closed"] | Literal["all"]
IssueOwnerFilter = Literal["all"] | Literal["mine"]


async def _list(client: GithubClient, repo: Repository, state: IssueStateFilter) -> list[Issue]:
async def list_issues(
client: GithubClient, repo: Repository, state: IssueStateFilter, owner: IssueOwnerFilter
) -> list[Issue]:
query_params = {"state": state}
if owner == "mine":
user = await client.user()
query_params["creator"] = user.login

headers = client.headers_with_auth_accept(cache_duration=client.config.cache.list_issues_ttl)
response = await client.get(f"/repos/{repo.owner.login}/{repo.name}/issues", headers=headers, params=query_params)
response.raise_for_status()
Expand All @@ -21,11 +27,6 @@ async def _list(client: GithubClient, repo: Repository, state: IssueStateFilter)
return result


list_open_issues = partial(_list, state="open")
list_closed_issues = partial(_list, state="closed")
list_all_issues = partial(_list, state="all")


async def get_comments(client: GithubClient, issue: Issue) -> list[IssueComment]:
response = await client.get(issue.comments_url, headers=client.headers_with_auth_accept())
response.raise_for_status()
Expand Down
6 changes: 4 additions & 2 deletions lazy_github/lib/github/pull_requests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from lazy_github.lib.constants import DIFF_CONTENT_ACCEPT_TYPE
from lazy_github.lib.github.client import GithubClient
from lazy_github.lib.github.issues import list_all_issues
from lazy_github.lib.github.issues import list_issues
from lazy_github.models.github import (
FullPullRequest,
Issue,
Expand All @@ -13,7 +13,9 @@

async def list_for_repo(client: GithubClient, repo: Repository) -> list[PartialPullRequest]:
"""Lists the pull requests associated with the specified repo"""
issues = await list_all_issues(client, repo)
state_filter = client.config.pull_requests.state_filter
owner_filter = client.config.pull_requests.owner_filter
issues = await list_issues(client, repo, state_filter, owner_filter)
return [i for i in issues if isinstance(i, PartialPullRequest)]


Expand Down
6 changes: 4 additions & 2 deletions lazy_github/ui/screens/primary.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textual.widgets import Footer, TabbedContent

from lazy_github.lib.github.client import GithubClient
from lazy_github.lib.github.issues import list_all_issues
from lazy_github.lib.github.issues import list_issues
from lazy_github.lib.github.pull_requests import get_full_pull_request
from lazy_github.lib.messages import IssuesAndPullRequestsFetched, IssueSelected, PullRequestSelected, RepoSelected
from lazy_github.ui.widgets.actions import ActionsContainer
Expand Down Expand Up @@ -114,7 +114,9 @@ def actions(self) -> ActionsContainer:
async def on_repo_selected(self, message: RepoSelected) -> None:
# self.actions.post_message(message)
try:
issues_and_pull_requests = await list_all_issues(self.client, message.repo)
state_filter = self.client.config.issues.state_filter
owner_filter = self.client.config.issues.owner_filter
issues_and_pull_requests = await list_issues(self.client, message.repo, state_filter, owner_filter)
except HTTPStatusError as hse:
if hse.response.status_code == 404:
pass
Expand Down

0 comments on commit a8d28fe

Please sign in to comment.