Skip to content

Commit

Permalink
create event (#10)
Browse files Browse the repository at this point in the history
* create event

* style: format code with Black and isort

This commit fixes the style issues introduced in 39883f8 according to the output
from Black and isort.

Details: #10

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
heitorpolidoro and deepsource-autofix[bot] committed Dec 17, 2023
1 parent 7fd956b commit b6a5940
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
35 changes: 35 additions & 0 deletions github_app/CreateEvent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from github.NamedUser import NamedUser
from github.Repository import Repository

from github_app.Event import Event
from github_app.LazyCompletableGithubObject import LazyCompletableGithubObject


class CreateEvent(Event):
"""This class represents a branch or tag creation event."""

name = "create"

def __init__(
self,
description,
master_branch,
pusher_type,
ref,
ref_type,
repository,
sender,
**kwargs,
):
super().__init__(**kwargs)
self.description: str = description
self.master_branch: str = master_branch
self.pusher_type: str = pusher_type
self.ref: str = ref
self.ref_type: str = ref_type
self.repository: Repository = LazyCompletableGithubObject.get_lazy_instance(
Repository, attributes=repository
)
self.sender: NamedUser = LazyCompletableGithubObject.get_lazy_instance(
NamedUser, attributes=sender
)
4 changes: 3 additions & 1 deletion github_app/Event.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, headers, installation):
def parse_event(cls, headers, body):
"""Returns an Event classe for the event in webhook"""
event = headers["X-Github-Event"]
action = body.pop("action")
action = body.pop("action", None)
event_class = cls.get_webhook_class(event, action)
return event_class(headers=headers, **body)

Expand All @@ -44,6 +44,8 @@ def get_webhook_class(cls, event, action):
raise ValueError(f"Multiple webhook classes for '{event}'")
if len(event_classes) == 1:
event_class = event_classes[0]
if action is None:
return event_class
action_classes = list(
filter(lambda x: x.action == action, event_class.__subclasses__())
)
Expand Down
4 changes: 3 additions & 1 deletion tests/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
def event_factory(event="event", action="action", add_to_body=None):
"""Factory to create events"""
add_to_body = add_to_body or []
body = {"action": action, "installation": {"id": 123}}
body = {"installation": {"id": 123}}
if action:
body["action"] = action
for item in add_to_body:
body.update({item: {}})
return Event.parse_event(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from github_app.CreateEvent import CreateEvent
from tests.factory import event_factory


def test_create():
event = event_factory(
"create",
None,
add_to_body=[
"description",
"master_branch",
"pusher_type",
"ref",
"ref_type",
"repository",
"sender",
],
)
assert isinstance(event, CreateEvent)

0 comments on commit b6a5940

Please sign in to comment.