Skip to content

Commit

Permalink
Add and hook up edit issue modal screen
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Sep 9, 2024
1 parent c6f17a1 commit 049fddb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
23 changes: 20 additions & 3 deletions lazy_github/lib/github/issues.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from typing import TypedDict, Unpack

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


class UpdateIssuePayload(TypedDict):
title: str | None
body: str | None


async def list_issues(
Expand Down Expand Up @@ -30,9 +37,19 @@ async def get_comments(client: GithubClient, issue: Issue) -> list[IssueComment]
return [IssueComment(**i) for i in response.json()]


async def create_comment(client: GithubClient, repo: Repository, issue: Issue, comment_body: str) -> IssueComment:
url = f"/repos/{repo.owner.login}/{repo.name}/issues/{issue.number}/comments"
async def create_comment(client: GithubClient, issue: Issue, comment_body: str) -> IssueComment:
url = f"/repos/{issue.repo.owner.login}/{issue.repo.name}/issues/{issue.number}/comments"
body = {"body": comment_body}
response = await client.post(url, json=body, headers=client.headers_with_auth_accept())
response.raise_for_status()
return IssueComment(**response.json())


async def update_issue(
client: GithubClient, issue_to_update: Issue, **updated_fields: Unpack[UpdateIssuePayload]
) -> Issue:
repo = issue_to_update.repo
url = f"/repos/{repo.owner.login}/{repo.name}/issues/{issue_to_update.number}"
response = await client.patch(url, json=updated_fields, headers=client.headers_with_auth_accept())
response.raise_for_status()
return Issue(**response.json(), repo=repo)
78 changes: 78 additions & 0 deletions lazy_github/ui/screens/edit_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from textual import on
from textual.app import ComposeResult
from textual.containers import Container, Horizontal
from textual.screen import ModalScreen
from textual.widgets import Button, Input, Label, Rule, TextArea

from lazy_github.lib.github import issues
from lazy_github.lib.github.client import GithubClient
from lazy_github.models.github import Issue


class EditIssueContainer(Container):
DEFAULT_CSS = """
#button_holder {
align: center middle;
}
#updated_issue_body {
height: 15;
}
"""

def __init__(self, client: GithubClient, issue: Issue, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.client = client
self.issue = issue

def compose(self) -> ComposeResult:
yield Label("[bold]Title[/bold]")
yield Input(self.issue.title, id="updated_issue_title")

yield Rule()

yield Label("[bold]Description[/bold]")
yield TextArea.code_editor(self.issue.body or "", id="updated_issue_body", soft_wrap=True)

with Horizontal(id="button_holder"):
yield Button("Save", id="save_updated_issue", variant="success")
yield Button("Cancel", id="cancel_updated_issue", variant="error")

@on(Button.Pressed, "#cancel_updated_issue")
def cancel_updated_issue(self, _: Button) -> None:
self.app.pop_screen()

@on(Button.Pressed, "#save_updated_issue")
async def submit_updated_issue(self, save_button: Button) -> None:
save_button.label = "Saving..."
save_button.disabled = True
updated_title = self.query_one("#updated_issue_title", Input).value
updated_body = self.query_one("#updated_issue_body", TextArea).text
self.notify(f"Updating issue #{self.issue.number}...", timeout=1)
await issues.update_issue(self.client, self.issue, title=updated_title, body=updated_body)
self.notify(f"Successfully updated issue #{self.issue.number}")
self.app.pop_screen()


class EditIssueModal(ModalScreen):
DEFAULT_CSS = """
EditIssueModal {
align: center middle;
}
EditIssueContainer {
align: center middle;
height: 30;
width: 100;
border: thick $background 80%;
background: $surface;
}
"""

def __init__(self, client: GithubClient, issue: Issue, *args, **kwargs):
super().__init__(*args, **kwargs)
self.issue = issue
self.client = client

def compose(self) -> ComposeResult:
yield EditIssueContainer(self.client, self.issue)
2 changes: 1 addition & 1 deletion lazy_github/ui/screens/new_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def post_comment(self, _: Button.Pressed) -> None:
if isinstance(self.reply_to, ReviewComment):
await pull_requests.reply_to_review_comment(self.client, self.repo, self.issue, self.reply_to, body)
else:
await issues.create_comment(self.client, self.repo, self.issue, body)
await issues.create_comment(self.client, self.issue, body)
except HTTPStatusError as hse:
# TODO: We should handle the error case better here
log_event(f"Error while posting comment for issue #{self.issue.number}: {hse}")
Expand Down
4 changes: 2 additions & 2 deletions lazy_github/ui/screens/primary.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def compose(self) -> ComposeResult:
pulls.display = self.client.config.appearance.show_pull_requests
yield pulls

issues = IssuesContainer(id="issues")
issues = IssuesContainer(self.client, id="issues")
issues.display = self.client.config.appearance.show_issues
yield issues

Expand Down Expand Up @@ -202,7 +202,7 @@ async def on_pull_request_selected(self, message: PullRequestSelected) -> None:
async def on_issue_selected(self, message: IssueSelected) -> None:
tabbed_content = self.query_one("#selection_detail_tabs", TabbedContent)
await tabbed_content.clear_panes()
await tabbed_content.add_pane(IssueOverviewTabPane(message.issue))
await tabbed_content.add_pane(IssueOverviewTabPane(self.client, message.issue))
await tabbed_content.add_pane(IssueConversationTabPane(self.client, message.issue))
tabbed_content.children[0].focus()
self.details.border_title = f"[5] Issue #{message.issue.number} Details"
Expand Down
19 changes: 18 additions & 1 deletion lazy_github/ui/widgets/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@
from lazy_github.lib.messages import IssuesAndPullRequestsFetched, IssueSelected
from lazy_github.lib.string_utils import link
from lazy_github.models.github import Issue, IssueState
from lazy_github.ui.screens.edit_issue import EditIssueModal
from lazy_github.ui.widgets.command_log import log_event
from lazy_github.ui.widgets.common import LazyGithubContainer, LazyGithubDataTable, SearchableLazyGithubDataTable
from lazy_github.ui.widgets.conversations import IssueCommentContainer


class IssuesContainer(LazyGithubContainer):
BINDINGS = [("E", "edit_issue", "Edit Issue")]

issues: Dict[int, Issue] = {}
status_column_index = -1
number_column_index = -1
title_column_index = -1

def __init__(self, client: GithubClient, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.client = client

def compose(self) -> ComposeResult:
self.border_title = "[3] Issues"
yield SearchableLazyGithubDataTable(
Expand Down Expand Up @@ -67,6 +74,10 @@ async def get_selected_issue(self) -> Issue:
number = self.table.get_cell_at(pr_number_coord)
return self.issues[number]

async def action_edit_issue(self) -> None:
issue = await self.get_selected_issue()
self.app.push_screen(EditIssueModal(self.client, issue))

@on(LazyGithubDataTable.RowSelected, "#issues_table")
async def issue_selected(self) -> None:
issue = await self.get_selected_issue()
Expand All @@ -81,8 +92,11 @@ class IssueOverviewTabPane(TabPane):
}
"""

def __init__(self, issue: Issue) -> None:
BINDINGS = [("E", "edit_issue", "Edit Issue")]

def __init__(self, client: GithubClient, issue: Issue) -> None:
super().__init__("Overview", id="issue_overview_pane")
self.client = client
self.issue = issue

def compose(self) -> ComposeResult:
Expand All @@ -100,6 +114,9 @@ def compose(self) -> ComposeResult:
yield Rule()
yield Markdown(self.issue.body)

def action_edit_issue(self) -> None:
self.app.push_screen(EditIssueModal(self.client, self.issue))


class IssueConversationTabPane(TabPane):
def __init__(self, client: GithubClient, issue: Issue) -> None:
Expand Down

0 comments on commit 049fddb

Please sign in to comment.