Skip to content

Commit

Permalink
feature: webhook deliveries
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgreg31 committed May 26, 2023
1 parent b94a83c commit acb8a62
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 6 deletions.
8 changes: 7 additions & 1 deletion github/GithubObject.pyi
Expand Up @@ -39,6 +39,10 @@ class GithubObject:
def _makeIntAttribute(
value: Optional[Union[int, str]]
) -> Union[_ValuedAttribute, _BadAttribute]: ...
@staticmethod
def _makeFloatAttribute(
value: Optional[Union[float, str]]
) -> Union[_ValuedAttribute, _BadAttribute]: ...
def _makeListOfClassesAttribute(
self, klass: Any, value: Any
) -> Union[_ValuedAttribute, _BadAttribute]: ...
Expand Down Expand Up @@ -119,8 +123,10 @@ class _BadAttribute:

class _NotSetType:
def __repr__(self) -> str: ...
value = None

class _ValuedAttribute:
def __init__(self, value: Any) -> None: ...
def __init__(self, value: Any) -> None:
self.value = value

NotSet: _NotSetType
310 changes: 310 additions & 0 deletions github/HookDelivery.py
@@ -0,0 +1,310 @@
############################ Copyrights and license ############################
# #
# Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2012 Zearin <zearin@gonk.net> #
# Copyright 2013 AKFish <akfish@gmail.com> #
# Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2016 Jannis Gebauer <ja.geb@me.com> #
# Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> #
# Copyright 2018 Wan Liuyang <tsfdye@gmail.com> #
# Copyright 2018 sfdye <tsfdye@gmail.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
from typing import Any, Dict, Optional, Union

import github.GithubObject
from github.GithubObject import _BadAttribute, _NotSetType, _ValuedAttribute


class HookDeliverySummary(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a Summary of HookDeliveries
"""

def __repr__(self) -> str:
return self.get__repr__({"id": self._id.value})

@property
def id(self) -> Optional[int]:
"""
:type: integer
"""
return self._id.value

@property
def guid(self) -> Optional[str]:
"""
:type: string
"""
return self._guid.value

@property
def delivered_at(self) -> Optional[str]:
"""
:type: string
"""
return self._delivered_at.value

@property
def redelivery(self) -> Optional[bool]:
"""
:type: boolean
"""
return self._redelivery.value

@property
def duration(self) -> Optional[float]:
"""
:type: float
"""
return self._duration.value

@property
def status(self) -> Optional[str]:
"""
:type: string
"""
return self._status.value

@property
def status_code(self) -> Optional[int]:
"""
:type: integer
"""
return self._status_code.value

@property
def event(self) -> Optional[str]:
"""
:type: string
"""
return self._event.value

@property
def action(self) -> Optional[str]:
"""
:type: string
"""
return self._action.value

@property
def installation_id(self) -> Optional[int]:
"""
:type: integer
"""
return self._installation_id.value

@property
def repository_id(self) -> Optional[int]:
"""
:type: integer
"""
return self._repository_id.value

@property
def url(self) -> Optional[str]:
"""
:type: string
"""
return self._url.value

def _initAttributes(self) -> None:
self._id: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._guid: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._delivered_at: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._redelivery: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._duration: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._status: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._status_code: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._event: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._action: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._installation_id: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._repository_id: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._url: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet

def _useAttributes(self, attributes: Dict[str, Any]):
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "guid" in attributes: # pragma no branch
self._guid = self._makeStringAttribute(attributes["guid"])
if "delivered_at" in attributes: # pragma no branch
self._delivered_at = self._makeStringAttribute(attributes["delivered_at"])
if "redelivery" in attributes: # pragma no branch
self._redelivery = self._makeBoolAttribute(attributes["redelivery"])
if "duration" in attributes: # pragma no branch
self._duration = self._makeFloatAttribute(attributes["duration"])
if "status" in attributes: # pragma no branch
self._status = self._makeStringAttribute(attributes["status"])
if "status_code" in attributes: # pragma no branch
self._status_code = self._makeIntAttribute(attributes["status_code"])
if "event" in attributes: # pragma no branch
self._event = self._makeStringAttribute(attributes["event"])
if "action" in attributes: # pragma no branch
self._action = self._makeStringAttribute(attributes["action"])
if "installation_id" in attributes: # pragma no branch
self._installation_id = self._makeIntAttribute(
attributes["installation_id"]
)
if "repository_id" in attributes: # pragma no branch
self._repository_id = self._makeIntAttribute(attributes["repository_id"])
if "url" in attributes: # pragma no branch
self._url = self._makeStringAttribute(attributes["url"])


class HookDeliveryRequest(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a HookDeliveryRequest
"""

def __repr__(self) -> str:
return self.get__repr__({"payload": self._payload.value})

@property
def headers(self) -> Optional[dict]:
"""
:type: dict
"""
return self._request_headers.value

@property
def payload(self) -> Optional[dict]:
"""
:type: dict
"""
return self._payload.value

def _initAttributes(self) -> None:
self._request_headers: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._payload: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet

def _useAttributes(self, attributes: Dict[str, Any]) -> None:
if "headers" in attributes: # pragma no branch
self._request_headers = self._makeDictAttribute(attributes["headers"])
if "payload" in attributes: # pragma no branch
self._payload = self._makeDictAttribute(attributes["payload"])


class HookDeliveryResponse(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a HookDeliveryResponse
"""

def __repr__(self) -> str:
return self.get__repr__({"payload": self._payload.value})

@property
def headers(self) -> Optional[dict]:
"""
:type: dict
"""
return self._response_headers.value

@property
def payload(self) -> Optional[str]:
"""
:type: str
"""
return self._payload.value

def _initAttributes(self) -> None:
self._response_headers: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._payload: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet

def _useAttributes(self, attributes: Dict[str, Any]) -> None:
if "headers" in attributes: # pragma no branch
self._response_headers = self._makeDictAttribute(attributes["headers"])
if "payload" in attributes: # pragma no branch
self._payload = self._makeStringAttribute(attributes["payload"])


class HookDelivery(HookDeliverySummary):
"""
This class represents a HookDelivery
"""

def __repr__(self) -> str:
return self.get__repr__({"id": self._id.value})

@property
def request(self) -> Optional[HookDeliveryRequest]:
"""
:type: :class:`HookDeliveryRequest`
"""
return self._request.value

@property
def response(self) -> Optional[HookDeliveryResponse]:
"""
:type: :class:`HookDeliveryResponse`
"""
return self._response.value

def _initAttributes(self) -> None:
super()._initAttributes()
self._request: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet
self._response: Union[
_ValuedAttribute, _BadAttribute, _NotSetType
] = github.GithubObject.NotSet

def _useAttributes(self, attributes: Dict[str, Any]) -> None:
super()._useAttributes(attributes)
if "request" in attributes: # pragma no branch
self._request = self._makeClassAttribute(
HookDeliveryRequest, attributes["request"]
)
if "response" in attributes: # pragma no branch
self._response = self._makeClassAttribute(
HookDeliveryResponse, attributes["response"]
)
# self._response = self._makeDictAttribute(attributes["response"])
36 changes: 36 additions & 0 deletions github/MainClass.py
Expand Up @@ -49,6 +49,7 @@

import datetime
import pickle
from typing import List

import urllib3

Expand All @@ -66,10 +67,12 @@
Consts,
GithubApp,
GitignoreTemplate,
HookDelivery,
HookDescription,
RateLimit,
Repository,
)
from .HookDelivery import HookDeliverySummary
from .Requester import Requester


Expand Down Expand Up @@ -689,6 +692,39 @@ def get_hooks(self):
for attributes in data
]

def get_hook_delivery(self, hook_id: int, delivery_id: int) -> HookDelivery:
"""
:calls: `GET /hooks/{hook_id}/deliveries/{delivery_id} <https://docs.github.com/en/rest/reference/repos#webhooks>`_
:param hook_id: integer
:param delivery_id: integer
:rtype: :class:`github.HookDelivery.HookDelivery`
"""
assert isinstance(hook_id, int), hook_id
assert isinstance(delivery_id, int), delivery_id
headers, attributes = self.__requester.requestJsonAndCheck(
"GET", f"/hooks/{hook_id}/deliveries/{delivery_id}"
)
return HookDelivery.HookDelivery(
self.__requester, headers, attributes, completed=True
)

def get_hook_deliveries(self, hook_id: int) -> List[HookDeliverySummary]:
"""
:calls: `GET /hooks/{hook_id}/deliveries <https://docs.github.com/en/rest/reference/repos#webhooks>`_
:param hook_id: integer
:rtype: list of :class:`github.HookDelivery.HookDeliverySummary`
"""
assert isinstance(hook_id, int), hook_id
headers, data = self.__requester.requestJsonAndCheck(
"GET", f"/hooks/{hook_id}/deliveries"
)
return [
HookDelivery.HookDeliverySummary(
self.__requester, headers, attributes, completed=True
)
for attributes in data
]

def get_gitignore_templates(self):
"""
:calls: `GET /gitignore/templates <https://docs.github.com/en/rest/reference/gitignore>`_
Expand Down

0 comments on commit acb8a62

Please sign in to comment.