Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks/travis: Expand event coverage.

sathwikshetty332fe2d905d449
diff --git a/zerver/webhooks/travis/tests.py b/zerver/webhooks/travis/tests.py
index de40f271e1f..a1ed9872111 100644
--- a/zerver/webhooks/travis/tests.py
+++ b/zerver/webhooks/travis/tests.py
@@ -12,7 +12,7 @@ class TravisHookTests(WebhookTestCase):
EXPECTED_NON_PULL_REQUEST_MESSAGE = (
":time_ticking: Build [#1](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277712204) "
- "**is in progress** for commit [Add Projects model.](https://github.com/sathwikshetty33/travis-test/commit/193da1b72346) by sathwikshetty33."
+ "**is in progress** for commit [Add Projects model.](https://github.com/sathwikshetty33/travis-test/commit/193da1b72346) by sathwikshetty33"
)
EXPECTED_PULL_REQUEST_MESSAGE = (
@@ -31,7 +31,7 @@ class TravisHookTests(WebhookTestCase):
self.check_webhook(
"non_pull_request",
self.NON_PR_TOPIC,
- self.EXPECTED_NON_PULL_REQUEST_MESSAGE,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
content_type="application/x-www-form-urlencoded",
)
@@ -63,7 +63,7 @@ class TravisHookTests(WebhookTestCase):
self.check_webhook(
"non_pull_request",
self.NON_PR_TOPIC,
- self.EXPECTED_NON_PULL_REQUEST_MESSAGE,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
content_type="application/x-www-form-urlencoded",
)
@@ -73,7 +73,7 @@ class TravisHookTests(WebhookTestCase):
self.check_webhook(
"non_pull_request",
self.NON_PR_TOPIC,
- self.EXPECTED_NON_PULL_REQUEST_MESSAGE,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
content_type="application/x-www-form-urlencoded",
)
@@ -112,7 +112,7 @@ class TravisHookTests(WebhookTestCase):
self.check_webhook(
"non_pull_request",
self.NON_PR_TOPIC,
- self.EXPECTED_NON_PULL_REQUEST_MESSAGE,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
content_type="application/x-www-form-urlencoded",
)
@@ -148,6 +148,29 @@ one or more new messages.
@override
def get_body(self, fixture_name: str) -> str:
+ if fixture_name in {"api", "cron"}:
+ payload = orjson.loads(
+ self.webhook_fixture_data("travis", "non_pull_request", file_type="json")
+ )
+ payload["type"] = fixture_name
+ return urlencode({"payload": orjson.dumps(payload).decode()})
+
return urlencode(
{"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}
)
+
+ def test_travis_api_event(self) -> None:
+ self.check_webhook(
+ "api",
+ self.NON_PR_TOPIC,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE} (triggered by api).",
+ content_type="application/x-www-form-urlencoded",
+ )
+
+ def test_travis_cron_event(self) -> None:
+ self.check_webhook(
+ "cron",
+ self.NON_PR_TOPIC,
+ f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE} (triggered by cron).",
+ content_type="application/x-www-form-urlencoded",
+ )
diff --git a/zerver/webhooks/travis/view.py b/zerver/webhooks/travis/view.py
index 57472525bbc..2f021b39d99 100644
--- a/zerver/webhooks/travis/view.py
+++ b/zerver/webhooks/travis/view.py
@@ -6,6 +6,7 @@ from django.http import HttpRequest, HttpResponse
from pydantic import BaseModel, Json
from zerver.decorator import webhook_view
+from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
from zerver.lib.webhooks.common import check_send_webhook_message
@@ -15,6 +16,8 @@ from zerver.models import UserProfile
ALL_EVENT_TYPES = [
"push",
"pull_request",
+ "api",
+ "cron",
]
STATUS_MAP = {
@@ -30,7 +33,7 @@ STATUS_MAP = {
MESSAGE_TEMPLATE = """
{emoji} Build [#{build_number}]({build_url}) **{status}** \
-for commit [{commit_message}]({compare_url}) by {author}.
+for commit [{commit_message}]({compare_url}) by {author}{trigger}.
""".strip()
@@ -52,12 +55,13 @@ class TravisPayload(BaseModel):
pull_request_title: str | None = None
-def get_message_body(payload: TravisPayload) -> str:
+def get_message_body(payload: TravisPayload, event: str) -> str:
# Extract only the first line of the commit message
# for multi-line commit messages.
commit_message = (
payload.message.strip().splitlines()[0] if payload.message else "(no commit message)"
)
+ trigger = f" (triggered by {event})" if event in ("api", "cron") else ""
status_message, emoji = STATUS_MAP[payload.status_message]
body = MESSAGE_TEMPLATE.format(
build_number=payload.number,
@@ -67,6 +71,7 @@ def get_message_body(payload: TravisPayload) -> str:
author=payload.author_name,
emoji=emoji,
compare_url=payload.compare_url,
+ trigger=trigger,
)
return body
@@ -99,8 +104,10 @@ def api_travis_webhook(
message: Annotated[Json[TravisPayload], ApiParamConfig("payload")],
) -> HttpResponse:
event = message.type
+ if event not in ALL_EVENT_TYPES:
+ raise UnsupportedWebhookEventTypeError(event)
- body = get_message_body(message)
+ body = get_message_body(message, event)
topic_name = get_topic(message, event)
check_send_webhook_message(request, user_profile, topic_name, body, event)