Skip to content

Commit

Permalink
update Announcement per 2024-12-02 changes
Browse files Browse the repository at this point in the history
this was a very dumb breaking change, github
  • Loading branch information
ShineyDev committed Dec 14, 2024
1 parent 71506e3 commit 054a8c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 171 deletions.
179 changes: 17 additions & 162 deletions github/content/announcement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,42 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing_extensions import Self, cast
from typing_extensions import Self

Check warning on line 5 in github/content/announcement.py

View workflow job for this annotation

GitHub Actions / Check

Import "typing_extensions" could not be resolved from source

from github.core.http import HTTPClient
from github.utility.types import T_json_key, T_json_value, DateTime
from github.utility.types import DateTime

import github
from github.core.errors import ClientObjectMissingFieldError
from github.interfaces import Type
from github.utility import MISSING


if TYPE_CHECKING:
from github.interfaces.type import TypeData


class AnnouncementData(TypeData):
announcement: str
announcementCreatedAt: str
announcementExpiresAt: str | None
announcementUserDismissible: bool
id: str
createdAt: str
expiresAt: str | None
isUserDismissible: bool
message: str


class Announcement(Type):
"""
Represents an announcement banner on an Enterprise or Organization.
.. container:: operations
.. describe:: x == y
.. describe:: x != y
Compares two objects by their :attr:`ID <.id>`.
.. describe:: hash(x)
Returns the hash of the object's :attr:`ID <.id>`.
"""

__slots__ = ()

_data: AnnouncementData
_http: HTTPClient

_graphql_fields: dict[str, str] = {
"text": "announcement",
"created_at": "announcementCreatedAt",
"expires_at": "announcementExpiresAt",
"can_viewer_dismiss": "announcementUserDismissible",
"id": "id",
"created_at": "createdAt",
"expires_at": "expiresAt",
"can_viewer_dismiss": "isUserDismissible",
"message": "message",
}

_graphql_type = "Organization" # NOTE: this is a lie.
_graphql_type = "AnnouncementBanner"

@property
def can_viewer_dismiss(
Expand All @@ -68,7 +50,7 @@ def can_viewer_dismiss(
:type: :class:`bool`
"""

return self._data["announcementUserDismissible"]
return self._data["isUserDismissible"]

@property
def created_at(
Expand All @@ -81,7 +63,7 @@ def created_at(
:type: :class:`~datetime.datetime`
"""

return github.utility.iso_to_datetime(self._data["announcementCreatedAt"])
return github.utility.iso_to_datetime(self._data["createdAt"])

@property
def expires_at(
Expand All @@ -94,152 +76,25 @@ def expires_at(
:type: Optional[:class:`~datetime.datetime`]
"""

expires_at = self._data["announcementExpiresAt"]
expires_at = self._data["expiresAt"]

if expires_at is None:
return None

return github.utility.iso_to_datetime(expires_at)

@property
def text(
def message(
self: Self,
/,
) -> str:
"""
The text of the announcement.
The message of the announcement.
:type: :class:`str`
"""

return self._data["announcement"]

async def _fetch_field(
self: Self,
field: T_json_key,
/,
*,
save: bool = MISSING,
) -> T_json_value:
save = save if save is not MISSING else True

try:
id = self._data["id"]
except KeyError:
raise ClientObjectMissingFieldError from None

data = await self._http.fetch_query_node(self.__class__, id, fields=(field,))

value = data[field]

if save:
self._data[field] = value

return value

async def fetch_can_viewer_dismiss(
self: Self,
/,
) -> str:
"""
|coro|
Fetches whether the authenticated user can dismiss the
announcement.
Raises
------
~github.core.errors.ClientObjectMissingFieldError
The :attr:`id` attribute is missing.
:rtype: :class:`bool`
"""

return await self._fetch_field("announcementUserDismissable") # type: ignore

async def fetch_created_at(
self: Self,
/,
) -> DateTime:
"""
|coro|
Fetches the date and time at which the announcement was
created.
Raises
------
~github.core.errors.ClientObjectMissingFieldError
The :attr:`id` attribute is missing.
:rtype: :class:`~datetime.datetime`
"""

created_at = await self._fetch_field("announcementCreatedAt")

if TYPE_CHECKING:
created_at = cast(str, created_at)

return github.utility.iso_to_datetime(created_at)

async def fetch_expires_at(
self: Self,
/,
) -> DateTime | None:
"""
|coro|
Fetches the date and time at which the announcement will
expire.
Raises
------
~github.core.errors.ClientObjectMissingFieldError
The :attr:`id` attribute is missing.
:rtype: Optional[:class:`~datetime.datetime`]
"""

expires_at = await self._fetch_field("announcementExpiresAt")

if expires_at is None:
return None

if TYPE_CHECKING:
expires_at = cast(str, expires_at)

return github.utility.iso_to_datetime(expires_at)

async def fetch_text(
self: Self,
/,
) -> DateTime | None:
"""
|coro|
Fetches the text of the announcement.
Raises
------
~github.core.errors.ClientObjectMissingFieldError
The :attr:`id` attribute is missing.
:rtype: :class:`str`
"""

return await self._fetch_field("announcement") # type: ignore
return self._data["message"]


__all__: list[str] = [
Expand Down
12 changes: 3 additions & 9 deletions github/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,12 @@ async def fetch_announcementowner_announcement(
fields: Iterable[str] = MISSING,
) -> AnnouncementData | None:
fields = github.utility.get_merged_graphql_fields(github.Announcement, fields)
query = "query($announcementowner_id: ID!){node(id:$announcementowner_id){...on AnnouncementBanner{%s}}}" % ",".join(fields)
path = ("node",)
query = "query($announcementowner_id: ID!){node(id:$announcementowner_id){...on Enterprise{announcementBanner{%(f)s}}...on Organization{announcementBanner{%(f)s}}}}" % {"f": ",".join(fields)}
path = ("node", "announcementBanner")

data = await self._fetch(query, *path, announcementowner_id=announcementowner_id)

if TYPE_CHECKING:
data = cast(AnnouncementData, data)

if data["announcementCreatedAt"] is None:
return None

return data
return data # type: ignore

async def fetch_query_all_codes_of_conduct(
self: Self,
Expand Down

0 comments on commit 054a8c6

Please sign in to comment.