Skip to content

Commit 966a91f

Browse files
committed
implement Client.fetch_repository_owner
1 parent 2113621 commit 966a91f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

github/core/client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,48 @@ async def fetch_repository(
364364

365365
return github.Repository._from_data(data, http=self._http)
366366

367+
async def fetch_repository_owner(
368+
self: Self,
369+
login: str,
370+
/,
371+
**kwargs, # TODO
372+
) -> Organization | User:
373+
"""
374+
|coro|
375+
376+
Fetches a repository owner by its login.
377+
378+
379+
Parameters
380+
----------
381+
382+
login: :class:`str`
383+
The login of the repository owner.
384+
385+
386+
Raises
387+
------
388+
389+
~github.core.errors.ClientResponseGraphQLNotFoundError
390+
A repository owner with the provided login does not exist.
391+
392+
393+
:rtype: :class:`~github.Organization` | :class:`~github.User`
394+
"""
395+
396+
data = await self._http.fetch_query_repository_owner(login, **kwargs)
397+
398+
# TODO[type-from-data]
399+
400+
graphql_type = data["__typename"]
401+
402+
if graphql_type == "Organization":
403+
return github.Organization._from_data(data, http=self._http)
404+
elif graphql_type == "User":
405+
return github.User._from_data(data, http=self._http)
406+
else:
407+
raise RuntimeError(f"invalid type {graphql_type} for Query.repositoryOwner")
408+
367409
async def fetch_topic(
368410
self: Self,
369411
name: str,

github/core/http.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,30 @@ async def fetch_query_repository(
543543

544544
return data
545545

546+
async def fetch_query_repository_owner(
547+
self: Self,
548+
/,
549+
login: str,
550+
*,
551+
fields: Iterable[str] = MISSING,
552+
) -> OrganizationData | UserData:
553+
organization_fields = github.utility.get_merged_graphql_fields(github.Organization, fields)
554+
user_fields = github.utility.get_merged_graphql_fields(github.User, fields)
555+
query = "query($login:String!){repositoryOwner(login:$login){...on Organization{%s}...on User{%s}}}" % (",".join(organization_fields), ",".join(user_fields))
556+
path = ("repositoryOwner",)
557+
558+
data = await self._fetch(query, *path, login=login)
559+
560+
if TYPE_CHECKING:
561+
data = cast(OrganizationData | UserData, data)
562+
563+
if data["__typename"] == "Organization":
564+
data = self._patch_organizationdata(data)
565+
elif data["__typename"] == "User":
566+
data = self._patch_userdata(data)
567+
568+
return data
569+
546570
if TYPE_CHECKING:
547571

548572
@overload

0 commit comments

Comments
 (0)