Skip to content

Commit

Permalink
✅ add webhook parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu committed Dec 28, 2023
1 parent 855b419 commit ef1d314
Show file tree
Hide file tree
Showing 15 changed files with 964 additions and 34 deletions.
3 changes: 2 additions & 1 deletion codegen/templates/latest/webhooks.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if TYPE_CHECKING:
from {{ output_module }}.{{ latest_version_module }}.webhooks._types import WebhookEvent as WebhookEvent
from {{ output_module }}.{{ latest_version_module }}.webhooks._types import webhook_action_types as webhook_action_types
from {{ output_module }}.{{ latest_version_module }}.webhooks._types import webhook_event_types as webhook_event_types
from {{ output_module }}.{{ latest_version_module }}.webhooks._namespace import EventNameType as EventNameType
from {{ output_module }}.{{ latest_version_module }}.webhooks._namespace import WebhookNamespace as WebhookNamespace
from {{ output_module }}.{{ latest_version_module }}.webhooks._namespace import VALID_EVENT_NAMES as VALID_EVENT_NAMES
else:
Expand All @@ -28,6 +29,6 @@ else:
"webhook_event_types"
),
"{{ output_module }}.{{ latest_version_module }}.webhooks._namespace": (
"VALID_EVENT_NAMES", "WebhookNamespace"
"EventNameType", "VALID_EVENT_NAMES", "WebhookNamespace"
)
}
3 changes: 2 additions & 1 deletion codegen/templates/webhooks/__init__.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if TYPE_CHECKING:
from ._types import WebhookEvent as WebhookEvent
from ._types import webhook_action_types as webhook_action_types
from ._types import webhook_event_types as webhook_event_types
from ._namespace import EventNameType as EventNameType
from ._namespace import WebhookNamespace as WebhookNamespace
from ._namespace import VALID_EVENT_NAMES as VALID_EVENT_NAMES
else:
Expand All @@ -28,6 +29,6 @@ else:
"webhook_event_types"
),
"._namespace": (
"VALID_EVENT_NAMES", "WebhookNamespace"
"EventNameType", "VALID_EVENT_NAMES", "WebhookNamespace"
)
}
10 changes: 10 additions & 0 deletions codegen/templates/webhooks/_namespace.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class WebhookNamespace:
...
{% endfor %}

@overload
@staticmethod
def parse(name: EventNameType, payload: Union[str, bytes]) -> "WebhookEvent":
...

@staticmethod
def parse(name: EventNameType, payload: Union[str, bytes]) -> "WebhookEvent":
"""Parse the webhook payload with event name.
Expand Down Expand Up @@ -95,6 +100,11 @@ class WebhookNamespace:
...
{% endfor %}

@overload
@staticmethod
def parse_obj(name: EventNameType, payload: Dict[str, Any]) -> "WebhookEvent":
...

@staticmethod
def parse_obj(name: EventNameType, payload: Dict[str, Any]) -> "WebhookEvent":
"""Parse the webhook payload dict with event name.
Expand Down
1 change: 1 addition & 0 deletions githubkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .core import GitHubCore as GitHubCore
from .response import Response as Response
from .paginator import Paginator as Paginator
from .compat import GitHubModel as GitHubModel
from .auth import AppAuthStrategy as AppAuthStrategy
from .auth import BaseAuthStrategy as BaseAuthStrategy
from .auth import TokenAuthStrategy as TokenAuthStrategy
Expand Down
4 changes: 4 additions & 0 deletions githubkit/versions/latest/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
from githubkit.versions.v2022_11_28.webhooks import (
watch_action_types as watch_action_types,
)
from githubkit.versions.v2022_11_28.webhooks._namespace import (
EventNameType as EventNameType,
)
from githubkit.versions.v2022_11_28.webhooks import (
CustomPropertyEvent as CustomPropertyEvent,
)
Expand Down Expand Up @@ -695,6 +698,7 @@
"webhook_event_types",
),
"githubkit.versions.v2022_11_28.webhooks._namespace": (
"EventNameType",
"VALID_EVENT_NAMES",
"WebhookNamespace",
),
Expand Down
10 changes: 5 additions & 5 deletions githubkit/versions/v2022_11_28/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14071,7 +14071,7 @@ class WebhookBranchProtectionRuleEditedPropRule(GitHubModel):
class WebhookCheckRunCompleted(GitHubModel):
"""Check Run Completed Event"""

action: Missing[Literal["completed"]] = Field(default=UNSET)
action: Literal["completed"] = Field()
check_run: CheckRunWithSimpleCheckSuite = Field(
title="CheckRun",
description="A check performed on the code of a given code change",
Expand Down Expand Up @@ -14110,7 +14110,7 @@ class WebhookCheckRunCompletedFormEncoded(GitHubModel):
class WebhookCheckRunCreated(GitHubModel):
"""Check Run Created Event"""

action: Missing[Literal["created"]] = Field(default=UNSET)
action: Literal["created"] = Field()
check_run: CheckRunWithSimpleCheckSuite = Field(
title="CheckRun",
description="A check performed on the code of a given code change",
Expand Down Expand Up @@ -14203,7 +14203,7 @@ class WebhookCheckRunRequestedActionFormEncoded(GitHubModel):
class WebhookCheckRunRerequested(GitHubModel):
"""Check Run Re-Requested Event"""

action: Missing[Literal["rerequested"]] = Field(default=UNSET)
action: Literal["rerequested"] = Field()
check_run: CheckRunWithSimpleCheckSuite = Field(
title="CheckRun",
description="A check performed on the code of a given code change",
Expand Down Expand Up @@ -17544,7 +17544,7 @@ class WebhookDeploymentCreatedPropWorkflowRun(GitHubModel):
class WebhookDeploymentProtectionRuleRequested(GitHubModel):
"""deployment protection rule requested event"""

action: Missing[Literal["requested"]] = Field(default=UNSET)
action: Literal["requested"] = Field()
environment: Missing[str] = Field(
default=UNSET,
description="The name of the environment that has the deployment protection rule.",
Expand Down Expand Up @@ -74451,7 +74451,7 @@ class WebhookSecretScanningAlertCreated(GitHubModel):
class WebhookSecretScanningAlertLocationCreated(GitHubModel):
"""Secret Scanning Alert Location Created Event"""

action: Missing[Literal["created"]] = Field(default=UNSET)
action: Literal["created"] = Field()
alert: SecretScanningAlertWebhook = Field()
installation: Missing[SimpleInstallation] = Field(
default=UNSET,
Expand Down
10 changes: 5 additions & 5 deletions githubkit/versions/v2022_11_28/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10979,7 +10979,7 @@ class WebhookBranchProtectionRuleEditedPropRuleType(TypedDict):
class WebhookCheckRunCompletedType(TypedDict):
"""Check Run Completed Event"""

action: NotRequired[Literal["completed"]]
action: Literal["completed"]
check_run: CheckRunWithSimpleCheckSuiteType
installation: NotRequired[SimpleInstallationType]
organization: NotRequired[OrganizationSimpleWebhooksType]
Expand All @@ -10999,7 +10999,7 @@ class WebhookCheckRunCompletedFormEncodedType(TypedDict):
class WebhookCheckRunCreatedType(TypedDict):
"""Check Run Created Event"""

action: NotRequired[Literal["created"]]
action: Literal["created"]
check_run: CheckRunWithSimpleCheckSuiteType
installation: NotRequired[SimpleInstallationType]
organization: NotRequired[OrganizationSimpleWebhooksType]
Expand Down Expand Up @@ -11049,7 +11049,7 @@ class WebhookCheckRunRequestedActionFormEncodedType(TypedDict):
class WebhookCheckRunRerequestedType(TypedDict):
"""Check Run Re-Requested Event"""

action: NotRequired[Literal["rerequested"]]
action: Literal["rerequested"]
check_run: CheckRunWithSimpleCheckSuiteType
installation: NotRequired[SimpleInstallationType]
organization: NotRequired[OrganizationSimpleWebhooksType]
Expand Down Expand Up @@ -13493,7 +13493,7 @@ class WebhookDeploymentCreatedPropWorkflowRunType(TypedDict):
class WebhookDeploymentProtectionRuleRequestedType(TypedDict):
"""deployment protection rule requested event"""

action: NotRequired[Literal["requested"]]
action: Literal["requested"]
environment: NotRequired[str]
event: NotRequired[str]
deployment_callback_url: NotRequired[str]
Expand Down Expand Up @@ -61166,7 +61166,7 @@ class WebhookSecretScanningAlertCreatedType(TypedDict):
class WebhookSecretScanningAlertLocationCreatedType(TypedDict):
"""Secret Scanning Alert Location Created Event"""

action: NotRequired[Literal["created"]]
action: Literal["created"]
alert: SecretScanningAlertWebhookType
installation: NotRequired[SimpleInstallationType]
location: SecretScanningLocationType
Expand Down
3 changes: 2 additions & 1 deletion githubkit/versions/v2022_11_28/webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .team_add import TeamAddEvent as TeamAddEvent
from .check_run import CheckRunEvent as CheckRunEvent
from .org_block import OrgBlockEvent as OrgBlockEvent
from ._namespace import EventNameType as EventNameType
from .milestone import MilestoneEvent as MilestoneEvent
from .deploy_key import DeployKeyEvent as DeployKeyEvent
from .fork import fork_action_types as fork_action_types
Expand Down Expand Up @@ -401,5 +402,5 @@
".workflow_job": ("WorkflowJobEvent", "workflow_job_action_types"),
".workflow_run": ("WorkflowRunEvent", "workflow_run_action_types"),
"._types": ("WebhookEvent", "webhook_action_types", "webhook_event_types"),
"._namespace": ("VALID_EVENT_NAMES", "WebhookNamespace"),
"._namespace": ("EventNameType", "VALID_EVENT_NAMES", "WebhookNamespace"),
}
10 changes: 10 additions & 0 deletions githubkit/versions/v2022_11_28/webhooks/_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ def parse(
) -> "WorkflowRunEvent":
...

@overload
@staticmethod
def parse(name: EventNameType, payload: Union[str, bytes]) -> "WebhookEvent":
...

@staticmethod
def parse(name: EventNameType, payload: Union[str, bytes]) -> "WebhookEvent":
"""Parse the webhook payload with event name.
Expand Down Expand Up @@ -1212,6 +1217,11 @@ def parse_obj(
) -> "WorkflowRunEvent":
...

@overload
@staticmethod
def parse_obj(name: EventNameType, payload: Dict[str, Any]) -> "WebhookEvent":
...

@staticmethod
def parse_obj(name: EventNameType, payload: Dict[str, Any]) -> "WebhookEvent":
"""Parse the webhook payload dict with event name.
Expand Down
20 changes: 1 addition & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pre-commit = "^3.0.0"
openapi-pydantic = "^0.4.0"

[tool.poetry.group.test.dependencies]
anyio = "*"
pytest = "^7.4.3"
pytest-cov = "^4.1.0"
pytest-xdist = "^3.5.0"
pytest-asyncio = "^0.23.2"

[tool.poetry.extras]
jwt = ["PyJWT"]
Expand All @@ -45,7 +45,6 @@ auth = ["PyJWT", "anyio"]
all = ["PyJWT", "anyio"]

[tool.pytest.ini_options]
asyncio_mode = "strict"
addopts = "--cov=githubkit --cov-append --cov-report=term-missing"
filterwarnings = ["error", "ignore::DeprecationWarning"]

Expand Down Expand Up @@ -276,6 +275,38 @@ source = "https://raw.githubusercontent.com/github/rest-api-description/main/des
# ] }

# Webhook schema overrides
# webhook check run action is required
"/components/schemas/webhook-check-run-completed" = { required = [
"action",
"check_run",
"repository",
"sender",
] }
"/components/schemas/webhook-check-run-created" = { required = [
"action",
"check_run",
"repository",
"sender",
] }
"/components/schemas/webhook-check-run-rerequested" = { required = [
"action",
"check_run",
"repository",
"sender",
] }
# webhook deployment protection rule action is required
"/components/schemas/webhook-deployment-protection-rule-requested" = { required = [
"action",
] }
# webhook secret scanning alert location action is required
"/components/schemas/webhook-secret-scanning-alert-location-created" = { required = [
"action",
"location",
"alert",
"repository",
"sender",
] }

# webhook fork topics can be null
"/components/schemas/webhook-fork/properties/forkee/allOf/1/properties/topics/items" = { type = [
"string",
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

from githubkit import GitHub, ActionAuthStrategy


@pytest.fixture
def g() -> GitHub:
return GitHub(ActionAuthStrategy())
546 changes: 546 additions & 0 deletions tests/test_webhooks/assets/pull_request_opened.json

Large diffs are not rendered by default.

Loading

0 comments on commit ef1d314

Please sign in to comment.