|
| 1 | +from dataclasses import dataclass |
| 2 | +from datetime import UTC, datetime, timedelta |
| 3 | +from re import Match |
| 4 | +from typing import Any, override |
| 5 | + |
| 6 | +from discord import Color, Embed, Interaction, Message |
| 7 | +from discord.ui import Button, DynamicItem, Item |
| 8 | +from githubkit import GitHub |
| 9 | +from githubkit.rest import Commit, Issue |
| 10 | +from pydantic import TypeAdapter |
| 11 | +from pydantic.dataclasses import dataclass as pydantic_dataclass |
| 12 | + |
| 13 | +from ghutils.core.bot import GHUtilsBot |
| 14 | +from ghutils.core.types import LoginState |
| 15 | +from ghutils.ui.embeds.commits import create_commit_embed |
| 16 | +from ghutils.ui.embeds.issues import ( |
| 17 | + create_issue_embed, |
| 18 | + create_issue_embeds, |
| 19 | +) |
| 20 | +from ghutils.utils.discord.mentions import relative_timestamp |
| 21 | +from ghutils.utils.discord.references import ( |
| 22 | + CommitReference, |
| 23 | + IssueReference, |
| 24 | + PRReference, |
| 25 | +) |
| 26 | +from ghutils.utils.github import RepositoryName, gh_request |
| 27 | + |
| 28 | + |
| 29 | +@pydantic_dataclass |
| 30 | +class RefreshIssueButton( |
| 31 | + DynamicItem[Button[Any]], |
| 32 | + template=r"RefreshIssue:(?P<repo_id>[0-9]+):(?P<issue>[0-9]+)", |
| 33 | +): |
| 34 | + repo_id: int |
| 35 | + issue: int |
| 36 | + |
| 37 | + def __post_init__(self): |
| 38 | + super().__init__( |
| 39 | + Button( |
| 40 | + emoji="🔄", |
| 41 | + custom_id=f"RefreshIssue:{self.repo_id}:{self.issue}", |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + @classmethod |
| 46 | + @override |
| 47 | + async def from_custom_id( |
| 48 | + cls, |
| 49 | + interaction: Interaction, |
| 50 | + item: Item[Any], |
| 51 | + match: Match[str], |
| 52 | + ): |
| 53 | + return TypeAdapter(cls).validate_python(match.groupdict()) |
| 54 | + |
| 55 | + @classmethod |
| 56 | + async def from_reference( |
| 57 | + cls, |
| 58 | + github: GitHub[Any], |
| 59 | + reference: IssueReference | PRReference, |
| 60 | + ): |
| 61 | + repo_name, issue = reference |
| 62 | + repo = await gh_request( |
| 63 | + github.rest.repos.async_get(owner=repo_name.owner, repo=repo_name.repo) |
| 64 | + ) |
| 65 | + return cls( |
| 66 | + repo_id=repo.id, |
| 67 | + issue=issue.number, |
| 68 | + ) |
| 69 | + |
| 70 | + @override |
| 71 | + async def callback(self, interaction: Interaction): |
| 72 | + async with GHUtilsBot.github_app_of(interaction) as (github, state): |
| 73 | + if not await _check_ratelimit(interaction, state): |
| 74 | + return |
| 75 | + |
| 76 | + # NOTE: this is an undocumented endpoint, but it seems like it's probably stable (https://stackoverflow.com/a/75527854) |
| 77 | + # we use this because user and repository names may be too long to fit in a custom id |
| 78 | + issue = await gh_request( |
| 79 | + github.arequest( # pyright: ignore[reportUnknownMemberType] |
| 80 | + "GET", |
| 81 | + f"/repositories/{self.repo_id}/issues/{self.issue}", |
| 82 | + response_model=Issue, |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + repo = RepositoryName.from_url(issue.html_url) |
| 87 | + |
| 88 | + await interaction.response.edit_message( |
| 89 | + embed=create_issue_embed(repo, issue), |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@dataclass |
| 94 | +class RefreshIssuesButton( |
| 95 | + DynamicItem[Button[Any]], |
| 96 | + template=r"RefreshIssues:(?P<message_id>[0-9]+)", |
| 97 | +): |
| 98 | + message: Message |
| 99 | + |
| 100 | + def __post_init__(self): |
| 101 | + super().__init__( |
| 102 | + Button( |
| 103 | + emoji="🔄", |
| 104 | + custom_id=f"RefreshIssues:{self.message.id}", |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + @classmethod |
| 109 | + @override |
| 110 | + async def from_custom_id( |
| 111 | + cls, |
| 112 | + interaction: Interaction, |
| 113 | + item: Item[Any], |
| 114 | + match: Match[str], |
| 115 | + ): |
| 116 | + assert interaction.message is not None |
| 117 | + message_id = int(match["message_id"]) |
| 118 | + return cls( |
| 119 | + message=await interaction.message.channel.fetch_message(message_id), |
| 120 | + ) |
| 121 | + |
| 122 | + @override |
| 123 | + async def callback(self, interaction: Interaction): |
| 124 | + async with GHUtilsBot.github_app_of(interaction) as (github, state): |
| 125 | + if not await _check_ratelimit(interaction, state): |
| 126 | + return |
| 127 | + |
| 128 | + # disable the button while we're working to give a loading indication |
| 129 | + self.item.disabled = True |
| 130 | + await interaction.response.edit_message(view=self.view) |
| 131 | + |
| 132 | + self.item.disabled = False |
| 133 | + try: |
| 134 | + contents = await create_issue_embeds(github, interaction, self.message) |
| 135 | + await contents.edit_original_response(interaction, view=self.view) |
| 136 | + except Exception: |
| 137 | + await interaction.response.edit_message(view=self.view) |
| 138 | + raise |
| 139 | + |
| 140 | + |
| 141 | +@pydantic_dataclass |
| 142 | +class RefreshCommitButton( |
| 143 | + DynamicItem[Button[Any]], |
| 144 | + template=r"RefreshCommit:(?P<repo_id>[0-9]+):(?P<sha>[^:]+)", |
| 145 | +): |
| 146 | + repo_id: int |
| 147 | + sha: str |
| 148 | + |
| 149 | + def __post_init__(self): |
| 150 | + super().__init__( |
| 151 | + Button( |
| 152 | + emoji="🔄", |
| 153 | + custom_id=f"RefreshCommit:{self.repo_id}:{self.sha}", |
| 154 | + ) |
| 155 | + ) |
| 156 | + |
| 157 | + @classmethod |
| 158 | + @override |
| 159 | + async def from_custom_id( |
| 160 | + cls, |
| 161 | + interaction: Interaction, |
| 162 | + item: Item[Any], |
| 163 | + match: Match[str], |
| 164 | + ): |
| 165 | + return TypeAdapter(cls).validate_python(match.groupdict()) |
| 166 | + |
| 167 | + @classmethod |
| 168 | + async def from_reference( |
| 169 | + cls, |
| 170 | + github: GitHub[Any], |
| 171 | + reference: CommitReference, |
| 172 | + ): |
| 173 | + repo_name, commit = reference |
| 174 | + repo = await gh_request( |
| 175 | + github.rest.repos.async_get(owner=repo_name.owner, repo=repo_name.repo) |
| 176 | + ) |
| 177 | + return cls( |
| 178 | + repo_id=repo.id, |
| 179 | + sha=commit.sha, |
| 180 | + ) |
| 181 | + |
| 182 | + @override |
| 183 | + async def callback(self, interaction: Interaction): |
| 184 | + async with GHUtilsBot.github_app_of(interaction) as (github, state): |
| 185 | + if not await _check_ratelimit(interaction, state): |
| 186 | + return |
| 187 | + |
| 188 | + commit = await gh_request( |
| 189 | + github.arequest( # pyright: ignore[reportUnknownMemberType] |
| 190 | + "GET", |
| 191 | + f"/repositories/{self.repo_id}/commits/{self.sha}", |
| 192 | + response_model=Commit, |
| 193 | + ) |
| 194 | + ) |
| 195 | + |
| 196 | + repo = RepositoryName.from_url(commit.html_url) |
| 197 | + |
| 198 | + await interaction.response.edit_message( |
| 199 | + embed=await create_commit_embed(github, repo, commit), |
| 200 | + ) |
| 201 | + |
| 202 | + |
| 203 | +async def _check_ratelimit(interaction: Interaction, state: LoginState) -> bool: |
| 204 | + now = datetime.now(UTC) |
| 205 | + if ( |
| 206 | + state.logged_out() |
| 207 | + and interaction.message |
| 208 | + and (edited_at := interaction.message.edited_at) |
| 209 | + and (retry_time := edited_at + timedelta(seconds=60)) |
| 210 | + and retry_time > now |
| 211 | + ): |
| 212 | + await interaction.response.send_message( |
| 213 | + embed=Embed( |
| 214 | + title="Slow down!", |
| 215 | + description="This button can only be used once per minute by unauthenticated users." |
| 216 | + + f" Use `/gh login` to authenticate, or try again {relative_timestamp(retry_time)}.", |
| 217 | + color=Color.red(), |
| 218 | + ), |
| 219 | + ephemeral=True, |
| 220 | + # delete the message after the timeout, but wait at least 10 seconds to allow reading it fully |
| 221 | + delete_after=max((retry_time - now).total_seconds(), 10), |
| 222 | + ) |
| 223 | + return False |
| 224 | + return True |
0 commit comments