From 135412280537f3ce72369d03b98d8e3832a99cff Mon Sep 17 00:00:00 2001 From: Riley Shaw <30989490+ShineyDev@users.noreply.github.com> Date: Sun, 19 Jan 2025 13:40:57 +0000 Subject: [PATCH] implement Client.fetch_advisories --- github/core/client.py | 52 +++++++++++++++++++++++++++++++++++++++++-- github/core/http.py | 20 +++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/github/core/client.py b/github/core/client.py index 1c929670..8f2b49b4 100644 --- a/github/core/client.py +++ b/github/core/client.py @@ -7,11 +7,11 @@ from aiohttp import ClientSession from github.api import Metadata, RateLimit - from github.connection import Connection, VulnerabilityOrder + from github.connection import AdvisoryOrder, Connection, VulnerabilityOrder from github.content import CodeOfConduct, License from github.organization import Organization from github.repository import Repository, Topic - from github.security import Vulnerability + from github.security import Advisory, Vulnerability from github.user import AuthenticatedUser, User, UserStatus from github.utility.types import DateTime, T_json_object @@ -499,6 +499,54 @@ async def fetch_viewer( data = await self._http.fetch_query_viewer(**kwargs) return github.AuthenticatedUser._from_data(data, http=self._http) + def fetch_advisories( + self: Self, + /, + *, + cursor: str | None = MISSING, + limit: int = MISSING, + order_by: AdvisoryOrder = MISSING, + reverse: bool = MISSING, + **kwargs, # TODO + ) -> Connection[Advisory]: + """ + |aiter| + + Fetches security advisories. + + + Parameters + ---------- + cursor: :class:`str` + The cursor to start at. + limit: :class:`int` + The maximum number of elements to yield. + order_by: :class:`~github.AdvisoryOrder` + The field by which to order the elements. + reverse: :class:`bool` + Whether to yield the elements in reverse order. + + + Raises + ------ + + ~github.core.errors.ClientObjectMissingFieldError + The :attr:`id` attribute is missing. + + + :rtype: :class:`~github.Connection`[:class:`~github.Advisory`] + """ + + return github.Connection( + self._http.collect_query_advisories, + order_by.value if order_by is not MISSING else None, + data_map=lambda d: github.Advisory._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_vulnerabilities( self: Self, /, diff --git a/github/core/http.py b/github/core/http.py index 098777b0..0c3fb2d6 100644 --- a/github/core/http.py +++ b/github/core/http.py @@ -28,6 +28,7 @@ from github.repository.pull import PullData from github.repository.repository import RepositoryData from github.repository.topic import TopicData + from github.security.advisory import AdvisoryData from github.security.vulnerability import VulnerabilityData from github.user import User, UserStatus from github.user.user import UserData, ViewerData @@ -1252,6 +1253,25 @@ async def collect_pull_participants( return await self._collect(query, *path, pull_id=pull_id, **kwargs) + async def collect_query_advisories( + self: Self, + /, + order_by: str | None, + *, + fields: Iterable[str] = MISSING, + **kwargs, + ) -> ConnectionData[AdvisoryData]: + fields = github.utility.get_merged_graphql_fields(github.Advisory, fields) + query = "query($after:String,$before:String,$first:Int,$last:Int,$order_by:SecurityAdvisoryOrder){securityAdvisories(after:$after,before:$before,first:$first,last:$last,orderBy:$order_by){nodes{%s},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}" % ",".join(fields) + path = ("securityAdvisories",) + + if order_by is None: + order_by_data = None + else: + order_by_data = {"direction": "ASC", "field": order_by} + + return await self._collect(query, *path, order_by=order_by_data, **kwargs) + async def collect_query_vulnerabilities( self: Self, /,