webhooks: Allow event registration using webhook_view decorator.

In addition to event filtering, we add support for registering supported
events for a webhook integration using the webhook_view decorator.

The event types are stored in the view function directly as a function
attribute, and can be later accessed via the module path and the view
function name are given (which is already specified the integrations.py)

Note that the WebhookTestCase doesn't know the name of the view function
and the module of the webhook. WEBHOOK_DIR_NAME needs to be overridden
if we want exceptions to raised when one of our test functions triggered
a unspecified event, but this practice is not enforced.

all_event_type does not need to be given even if event filters are used
in the webhook. But if a list of event types is given, it will be possible
for us to include it in the documentation while ensuring that all the
tested events are included (but not vice versa at the current stage, as
we yet not required all the events included in the list to be tested)

This guarantees that we can always access the list of all the tested
events of a webhook. This feature will be later plumbed to marcos to
display all event types dynamically in doc.md.
This commit is contained in:
PIG208
2021-06-26 16:07:54 +08:00
committed by Tim Abbott
parent 13399833b0
commit 2f9c586af5
4 changed files with 96 additions and 8 deletions

View File

@@ -13,6 +13,7 @@ Author: josh_mandel
Build status: Passed :thumbs_up:
Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6c457d366a31), [build log](https://travis-ci.org/hl7-fhir/fhir-svn/builds/92495257)
""".strip()
VIEW_FUNCTION_NAME = "api_travis_webhook"
def test_travis_message(self) -> None:
"""
@@ -137,6 +138,25 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6
msg = self.get_last_message()
self.assertNotEqual(msg.topic_name(), self.TOPIC)
def test_travis_invalid_event(self) -> None:
payload = self.get_body("build")
payload = payload.replace("push", "invalid_event")
expected_error_messsage = """
Error: This test triggered a message using the event "invalid_event", which was not properly
registered via the @webhook_view(..., event_types=[...]). These registrations are important for Zulip
self-documenting the supported event types for this integration.
You can fix this by adding "invalid_event" to ALL_EVENT_TYPES for this webhook.
""".strip()
with self.assertLogs("django.request"):
with self.assertLogs("zerver.middleware.json_error_handler", level="ERROR") as m:
self.client_post(
self.url,
payload,
content_type="application/x-www-form-urlencoded",
)
self.assertIn(expected_error_messsage, m.output[0])
def get_body(self, fixture_name: str) -> str:
return urllib.parse.urlencode(
{"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}

View File

@@ -13,6 +13,10 @@ from zerver.models import UserProfile
GOOD_STATUSES = ["Passed", "Fixed"]
BAD_STATUSES = ["Failed", "Broken", "Still Failing", "Errored", "Canceled"]
PENDING_STATUSES = ["Pending"]
ALL_EVENT_TYPES = [
"push",
"pull_request",
]
MESSAGE_TEMPLATE = """\
Author: {}
@@ -20,7 +24,7 @@ Build status: {} {}
Details: [changes]({}), [build log]({})"""
@webhook_view("Travis")
@webhook_view("Travis", all_event_types=ALL_EVENT_TYPES)
@has_request_variables
def api_travis_webhook(
request: HttpRequest,