|
| 1 | +import re |
| 2 | + |
| 3 | +from textual import on |
| 4 | +from textual.app import ComposeResult |
| 5 | +from textual.containers import Container |
| 6 | +from textual.content import Content |
| 7 | +from textual.screen import ModalScreen |
| 8 | +from textual.validation import ValidationResult, Validator |
| 9 | +from textual.widgets import Button, Input, Label, Markdown, Rule |
| 10 | + |
| 11 | +from lazy_github.lib.bindings import LazyGithubBindings |
| 12 | +from lazy_github.lib.github.backends.protocol import GithubApiRequestFailed |
| 13 | +from lazy_github.lib.github.issues import get_issue_by_number |
| 14 | +from lazy_github.lib.github.pull_requests import get_full_pull_request |
| 15 | +from lazy_github.lib.github.repositories import get_repository_by_name |
| 16 | +from lazy_github.models.github import FullPullRequest, Issue, Repository |
| 17 | +from lazy_github.ui.widgets.common import LazyGithubFooter, ModalDialogButtons |
| 18 | + |
| 19 | +REPO_PATTERN_STRING = r"(https://)?[^\/]+\/(?P<owner>[^\/]+)\/(?P<repo>[^\/]+)" |
| 20 | +REPO_PATTERN = re.compile(REPO_PATTERN_STRING) |
| 21 | +ISSUE_PATTERN = re.compile(REPO_PATTERN_STRING + r"\/issues\/(?P<issue>\d+).*") |
| 22 | +PR_PATTERN = re.compile(REPO_PATTERN_STRING + r"\/pull\/(?P<pull_request>\d+).*") |
| 23 | + |
| 24 | +PastedLinkSubject = Repository | FullPullRequest | Issue |
| 25 | + |
| 26 | + |
| 27 | +class GithubLinkValidator(Validator): |
| 28 | + def validate(self, value: str) -> ValidationResult: |
| 29 | + if REPO_PATTERN.match(value) or ISSUE_PATTERN.match(value) or PR_PATTERN.match(value): |
| 30 | + return self.success() |
| 31 | + else: |
| 32 | + return self.failure() |
| 33 | + |
| 34 | + |
| 35 | +class PasteLinkContainer(Container): |
| 36 | + DEFAULT_CSS = """ |
| 37 | + PasteLinkContainer { |
| 38 | + align: center middle; |
| 39 | + } |
| 40 | + """ |
| 41 | + |
| 42 | + def compose(self) -> ComposeResult: |
| 43 | + yield Markdown("# Paste a web GitHub link:") |
| 44 | + yield Label(Content.from_markup("[bold]Link:[/bold]")) |
| 45 | + yield Input(id="link_to_paste", placeholder="https://github.com/...", validators=GithubLinkValidator()) |
| 46 | + yield Rule() |
| 47 | + yield ModalDialogButtons(submit_text="Open") |
| 48 | + |
| 49 | + |
| 50 | +class PasteLinkModal(ModalScreen[PastedLinkSubject]): |
| 51 | + DEFAULT_CSS = """ |
| 52 | + PasteLinkModal { |
| 53 | + align: center middle; |
| 54 | + content-align: center middle; |
| 55 | + } |
| 56 | +
|
| 57 | + PasteLinkContainer { |
| 58 | + width: 80; |
| 59 | + max-height: 20; |
| 60 | + border: thick $background 80%; |
| 61 | + background: $surface-lighten-3; |
| 62 | + } |
| 63 | + """ |
| 64 | + |
| 65 | + BINDINGS = [LazyGithubBindings.SUBMIT_DIALOG, LazyGithubBindings.CLOSE_DIALOG] |
| 66 | + |
| 67 | + def compose(self) -> ComposeResult: |
| 68 | + yield PasteLinkContainer() |
| 69 | + yield LazyGithubFooter() |
| 70 | + |
| 71 | + async def _parse_link_and_action(self, link: str) -> None: |
| 72 | + match = PR_PATTERN.match(link) or ISSUE_PATTERN.match(link) or REPO_PATTERN.match(link) |
| 73 | + if match is None: |
| 74 | + return |
| 75 | + self.query_one("#submit", Button).disabled = True |
| 76 | + self.query_one("#cancel", Button).disabled = True |
| 77 | + |
| 78 | + groups = match.groupdict() |
| 79 | + |
| 80 | + repo = await get_repository_by_name(f"{groups['owner']}/{groups['repo']}") |
| 81 | + if not repo: |
| 82 | + self.notify("Could not find repository!", title="Unknown repo", severity="error") |
| 83 | + return |
| 84 | + |
| 85 | + if issue_number := groups.get("issue"): |
| 86 | + try: |
| 87 | + issue = await get_issue_by_number(repo, int(issue_number)) |
| 88 | + self.dismiss(issue) |
| 89 | + except GithubApiRequestFailed: |
| 90 | + self.notify("Could not find issue!", title="Unknown issue", severity="error") |
| 91 | + elif pr_number := groups.get("pull_request"): |
| 92 | + try: |
| 93 | + pr = await get_full_pull_request(repo, int(pr_number)) |
| 94 | + self.dismiss(pr) |
| 95 | + except GithubApiRequestFailed: |
| 96 | + self.notify("Could not find pull request!", title="Unknown PR", severity="error") |
| 97 | + else: |
| 98 | + self.notify("Unknown link") |
| 99 | + self.query_one("#submit", Button).disabled = False |
| 100 | + self.query_one("#cancel", Button).disabled = False |
| 101 | + |
| 102 | + @on(Button.Pressed, "#submit") |
| 103 | + async def action_submit(self) -> None: |
| 104 | + link = self.query_one("#link_to_paste", Input) |
| 105 | + await self._parse_link_and_action(link.value) |
| 106 | + |
| 107 | + @on(Button.Pressed, "#cancel") |
| 108 | + async def action_close(self) -> None: |
| 109 | + self.dismiss(None) |
0 commit comments