From 054a8c67c7c349b148b2d9daeed6c6468fbb66f3 Mon Sep 17 00:00:00 2001 From: Riley Shaw <30989490+ShineyDev@users.noreply.github.com> Date: Sat, 14 Dec 2024 09:56:03 +0000 Subject: [PATCH] update Announcement per 2024-12-02 changes this was a very dumb breaking change, github --- github/content/announcement.py | 179 ++++----------------------------- github/core/http.py | 12 +-- 2 files changed, 20 insertions(+), 171 deletions(-) diff --git a/github/content/announcement.py b/github/content/announcement.py index 8a5a8ff8..56f0078e 100644 --- a/github/content/announcement.py +++ b/github/content/announcement.py @@ -2,15 +2,12 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing_extensions import Self, cast + from typing_extensions import Self - 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: @@ -18,44 +15,29 @@ 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( @@ -68,7 +50,7 @@ def can_viewer_dismiss( :type: :class:`bool` """ - return self._data["announcementUserDismissible"] + return self._data["isUserDismissible"] @property def created_at( @@ -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( @@ -94,7 +76,7 @@ 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 @@ -102,144 +84,17 @@ def expires_at( 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] = [ diff --git a/github/core/http.py b/github/core/http.py index 12cf817e..0b330ac2 100644 --- a/github/core/http.py +++ b/github/core/http.py @@ -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,