Skip to content

Commit

Permalink
implement Repository.fetch_references (Repository.refs) field
Browse files Browse the repository at this point in the history
  • Loading branch information
ShineyDev committed Feb 2, 2025
1 parent 9adf63f commit e713475
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
21 changes: 21 additions & 0 deletions github/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,27 @@ async def collect_repository_pulls(

return await self._collect(query, *path, repository_id=repository_id, order_by=order_by_data, **kwargs)

async def collect_repository_references(
self,
/,
repository_id: str,
reference_prefix: str,
order_by: str | None,
*,
fields: Iterable[str] = MISSING,
**kwargs,
) -> ConnectionData[ReferenceData]:
fields = github.utility.get_merged_graphql_fields(github.Reference, fields)
query = "query($after:String,$before:String,$first:Int,$last:Int,$order_by:RefOrder,$reference_prefix:String!,$repository_id:ID!){node(id:$repository_id){...on Repository{refs(after:$after,before:$before,first:$first,last:$last,orderBy:$order_by,refPrefix:$reference_prefix){nodes{%s},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}}}" % ",".join(fields)
path = ("node", "refs")

if order_by is None:
order_by_data = None
else:
order_by_data = {"direction": "ASC", "field": order_by}

return await self._collect(query, *path, repository_id=repository_id, reference_prefix=reference_prefix, order_by=order_by_data, **kwargs)

async def collect_repository_topics(
self,
/,
Expand Down
113 changes: 110 additions & 3 deletions github/repository/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Literal, cast
from typing import Literal, overload, cast

from github.connection import Connection, DiscussionOrder, IssueOrder, LabelOrder, MilestoneOrder, PullOrder, RepositoryOrder
from github.connection import Connection, DiscussionOrder, IssueOrder, LabelOrder, MilestoneOrder, PullOrder, ReferenceOrder, RepositoryOrder
from github.content import CodeOfConduct, License
from github.core.http import HTTPClient
from github.git import Reference
from github.git import Reference, ReferenceType
from github.organization import Organization
from github.repository import Discussion, Issue, Label, Milestone, Pull, Topic
from github.repository.discussion import DiscussionData
Expand Down Expand Up @@ -2079,6 +2079,113 @@ def fetch_pulls(
**kwargs,
)

if TYPE_CHECKING:

@overload # NOTE: ()
def fetch_references(
self,
/,
*,
cursor: str | None = MISSING,
limit: int = MISSING,
order_by: ReferenceOrder = MISSING,
reverse: bool = MISSING,
**kwargs, # TODO
) -> Connection[Reference]:
...

@overload # NOTE: (prefix="refs/heads/")
def fetch_references(
self,
/,
*,
cursor: str | None = MISSING,
limit: int = MISSING,
order_by: ReferenceOrder = MISSING,
prefix: str,
reverse: bool = MISSING,
**kwargs, # TODO
) -> Connection[Reference]:
...

@overload # NOTE: (type=ReferenceType.head)
def fetch_references(
self,
/,
*,
cursor: str | None = MISSING,
limit: int = MISSING,
order_by: ReferenceOrder = MISSING,
reverse: bool = MISSING,
type: ReferenceType,
**kwargs, # TODO
) -> Connection[Reference]:
...

def fetch_references(
self,
/,
*,
cursor: str | None = MISSING,
limit: int = MISSING,
order_by: ReferenceOrder = MISSING,
prefix: str = MISSING,
reverse: bool = MISSING,
type: ReferenceType = MISSING,
**kwargs, # TODO
) -> Connection[Reference]:
"""
|aiter|
Fetches references in the repository.
Parameters
----------
cursor: :class:`str`
The cursor to start at.
limit: :class:`int`
The maximum number of elements to yield.
order_by: :class:`~github.ReferenceOrder`
The field by which to order the elements.
prefix: :class:`str`
The Git reference prefix, eg. "refs/heads/".
reverse: :class:`bool`
Whether to yield the elements in reverse order.
type: :class:`~github.ReferenceType`
The Git reference type, eg. ReferenceType.head.
Raises
------
~github.core.errors.ClientObjectMissingFieldError
The :attr:`id` attribute is missing.
:rtype: :class:`~github.Connection`[:class:`~github.Reference`]
"""

if type is not MISSING:
if prefix is not MISSING:
raise RuntimeError

prefix = type.value
elif prefix is MISSING:
prefix = "refs/"

return github.Connection(
self._http.collect_repository_references,
self.id,
prefix,
order_by.value if order_by is not MISSING else None,
data_map=lambda d: github.Reference._from_data(d, http=self._http),
cursor=cursor if cursor is not MISSING else None,
limit=limit if limit is not MISSING else None,
reverse=reverse if reverse is not MISSING else False,
**kwargs,
)

def fetch_topics(
self,
/,
Expand Down

0 comments on commit e713475

Please sign in to comment.