From a8d28fed5d8e0fec9dbfbb0351627df5019e42c5 Mon Sep 17 00:00:00 2001 From: gizmo385 Date: Mon, 2 Sep 2024 02:06:31 -0600 Subject: [PATCH] Add issue/pr user state filters to settings --- lazy_github/lib/config.py | 16 ++++++++++++++-- lazy_github/lib/github/issues.py | 15 ++++++++------- lazy_github/lib/github/pull_requests.py | 6 ++++-- lazy_github/ui/screens/primary.py | 6 ++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lazy_github/lib/config.py b/lazy_github/lib/config.py index 4a17788..79342eb 100644 --- a/lazy_github/lib/config.py +++ b/lazy_github/lib/config.py @@ -10,7 +10,8 @@ _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): @@ -18,7 +19,17 @@ class ApiConfig(BaseModel): 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): @@ -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() diff --git a/lazy_github/lib/github/issues.py b/lazy_github/lib/github/issues.py index 5238a4e..3e45e79 100644 --- a/lazy_github/lib/github/issues.py +++ b/lazy_github/lib/github/issues.py @@ -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() @@ -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() diff --git a/lazy_github/lib/github/pull_requests.py b/lazy_github/lib/github/pull_requests.py index af40e19..dd90920 100644 --- a/lazy_github/lib/github/pull_requests.py +++ b/lazy_github/lib/github/pull_requests.py @@ -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, @@ -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)] diff --git a/lazy_github/ui/screens/primary.py b/lazy_github/ui/screens/primary.py index 49c485c..3e8f15e 100644 --- a/lazy_github/ui/screens/primary.py +++ b/lazy_github/ui/screens/primary.py @@ -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 @@ -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