Skip to content

Commit 7a92611

Browse files
committed
Allow urls for references, repos, and users
1 parent 12311a6 commit 7a92611

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Pydantic's HISTORY.md](https://github.com/pydantic/pydantic/blob/main/HISTORY.md), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [UNRELEASED]
8+
9+
### Changed
10+
11+
* Several commands now accept a GitHub URL as input (eg. `/gh issue issue:https://github.com/object-Object/discord-github-utils/issues/1`).
12+
713
## `0.2.1` - 2025-02-04
814

915
### Changed

bot/src/ghutils/utils/discord/references.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@
2020

2121
logger = logging.getLogger(__name__)
2222

23+
URL_PATTERN = re.compile(
24+
r"(?:https?://)?github.com/(?P<repo>[\w-]+/[\w-]+)/(?P<path>[\w-]+)/(?P<reference>[\w-]+)"
25+
)
26+
2327

2428
class ReferenceTransformer[T](Transformer, ABC):
2529
@property
2630
@abstractmethod
2731
def separator(self) -> str: ...
2832

33+
@property
34+
@abstractmethod
35+
def url_path(self) -> str: ...
36+
2937
@property
3038
@abstractmethod
3139
def reference_pattern(self) -> str: ...
@@ -106,7 +114,10 @@ async def get_repo_and_reference(
106114
interaction: Interaction,
107115
value: str,
108116
) -> tuple[RepositoryName, str]:
109-
if self.separator in value:
117+
if (match := URL_PATTERN.match(value)) and match["path"] == self.url_path:
118+
raw_repo = match["repo"]
119+
rest = match["reference"]
120+
elif self.separator in value:
110121
raw_repo, rest = value.split(self.separator, maxsplit=1)
111122
else:
112123
raw_repo = ""
@@ -137,6 +148,7 @@ def build_choice(
137148

138149
class IssueOrPRReferenceTransformer[T](ReferenceTransformer[T]):
139150
@property
151+
@abstractmethod
140152
def issue_type(self) -> Literal["issue", "pr"]: ...
141153

142154
@property
@@ -163,6 +175,10 @@ async def search_for_autocomplete(
163175

164176

165177
class IssueReferenceTransformer(IssueOrPRReferenceTransformer[Issue]):
178+
@property
179+
def url_path(self):
180+
return "issues"
181+
166182
@property
167183
def issue_type(self):
168184
return "issue"
@@ -183,6 +199,10 @@ async def resolve_reference(
183199

184200

185201
class PRReferenceTransformer(IssueOrPRReferenceTransformer[PullRequest]):
202+
@property
203+
def url_path(self):
204+
return "pull"
205+
186206
@property
187207
def issue_type(self):
188208
return "pr"
@@ -207,6 +227,10 @@ class CommitReferenceTransformer(ReferenceTransformer[Commit]):
207227
def separator(self):
208228
return "@"
209229

230+
@property
231+
def url_path(self):
232+
return "commit"
233+
210234
@property
211235
def reference_pattern(self):
212236
return r"[0-9a-f]{5,40}"

bot/src/ghutils/utils/discord/transformers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
import re
45

56
from discord import Interaction
67
from discord.app_commands import Transform, Transformer
@@ -18,8 +19,16 @@
1819
logger = logging.getLogger(__name__)
1920

2021

22+
REPO_URL_PATTERN = re.compile(r"(?:https?://)?github.com/(?P<value>[\w-]+/[\w-]+)")
23+
24+
USER_URL_PATTERN = re.compile(r"(?:https?://)?github.com/(?P<value>[\w-]+)")
25+
26+
2127
class RepositoryTransformer(Transformer):
2228
async def transform(self, interaction: Interaction, value: str) -> FullRepository:
29+
if match := REPO_URL_PATTERN.match(value):
30+
value = match["value"]
31+
2332
repo = RepositoryName.parse(value)
2433
async with GHUtilsBot.github_app_of(interaction) as (github, _):
2534
try:
@@ -41,6 +50,8 @@ async def autocomplete( # pyright: ignore[reportIncompatibleMethodOverride]
4150
) -> list[Choice[str]]:
4251
value = value.strip()
4352
if value:
53+
if match := REPO_URL_PATTERN.match(value):
54+
value = match["value"]
4455
query = f"{value} in:name fork:true"
4556
else:
4657
with GHUtilsBot.db_session_of(interaction) as session:
@@ -78,6 +89,9 @@ async def transform(
7889
interaction: Interaction,
7990
value: str,
8091
) -> PrivateUser | PublicUser:
92+
if match := USER_URL_PATTERN.match(value):
93+
value = match["value"]
94+
8195
async with GHUtilsBot.github_app_of(interaction) as (github, _):
8296
try:
8397
return await gh_request(github.rest.users.async_get_by_username(value))
@@ -103,6 +117,9 @@ async def autocomplete( # pyright: ignore[reportIncompatibleMethodOverride]
103117
user = await gh_request(github.rest.users.async_get_authenticated())
104118
return [Choice(name=user.login, value=user.login)]
105119

120+
if match := USER_URL_PATTERN.match(value):
121+
value = match["value"]
122+
106123
try:
107124
result = await gh_request(
108125
github.rest.search.async_users(

0 commit comments

Comments
 (0)