Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/travis: Improve topic naming.
sathwikshetty33bc8b73ee59e7
diff --git a/static/images/integrations/travis/001.png b/static/images/integrations/travis/001.pngindex 099492461c1..9daa7cee26d 100644Binary files a/static/images/integrations/travis/001.png and b/static/images/integrations/travis/001.png differdiff --git a/zerver/webhooks/travis/tests.py b/zerver/webhooks/travis/tests.pyindex f2592d6741c..de40f271e1f 100644--- a/zerver/webhooks/travis/tests.py+++ b/zerver/webhooks/travis/tests.py@@ -1,12 +1,15 @@from urllib.parse import urlencode+import orjsonfrom typing_extensions import overridefrom zerver.lib.test_classes import WebhookTestCaseclass TravisHookTests(WebhookTestCase):- TOPIC_NAME = "builds"+ NON_PR_TOPIC = "travis-test / main"+ PR_TOPIC = "travis-test / PR #2 feat: Add Name.txt."+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."@@ -27,7 +30,7 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("non_pull_request",- self.TOPIC_NAME,+ self.NON_PR_TOPIC,self.EXPECTED_NON_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)@@ -37,7 +40,7 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("pull_request",- self.TOPIC_NAME,+ self.PR_TOPIC,self.EXPECTED_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)@@ -59,7 +62,7 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("non_pull_request",- self.TOPIC_NAME,+ self.NON_PR_TOPIC,self.EXPECTED_NON_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)@@ -69,7 +72,7 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("non_pull_request",- self.TOPIC_NAME,+ self.NON_PR_TOPIC,self.EXPECTED_NON_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)@@ -91,7 +94,7 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("pull_request",- self.TOPIC_NAME,+ self.PR_TOPIC,self.EXPECTED_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)@@ -101,14 +104,14 @@ class TravisHookTests(WebhookTestCase):self.check_webhook("pull_request",- self.TOPIC_NAME,+ self.PR_TOPIC,self.EXPECTED_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)self.check_webhook("non_pull_request",- self.TOPIC_NAME,+ self.NON_PR_TOPIC,self.EXPECTED_NON_PULL_REQUEST_MESSAGE,content_type="application/x-www-form-urlencoded",)diff --git a/zerver/webhooks/travis/view.py b/zerver/webhooks/travis/view.pyindex 7348e25ecc2..57472525bbc 100644--- a/zerver/webhooks/travis/view.py+++ b/zerver/webhooks/travis/view.py@@ -9,6 +9,7 @@ from zerver.decorator import webhook_viewfrom zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import ApiParamConfig, typed_endpointfrom zerver.lib.webhooks.common import check_send_webhook_message+from zerver.lib.webhooks.git import TOPIC_WITH_BRANCH_TEMPLATE, TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATEfrom zerver.models import UserProfileALL_EVENT_TYPES = [@@ -33,6 +34,10 @@ for commit [{commit_message}]({compare_url}) by {author}.""".strip()+class Repository(BaseModel):+ name: str++class TravisPayload(BaseModel):author_name: strstatus_message: str@@ -41,6 +46,10 @@ class TravisPayload(BaseModel):type: strmessage: str | None = Nonenumber: str+ branch: str+ repository: Repository+ pull_request_number: int | None = None+ pull_request_title: str | None = Nonedef get_message_body(payload: TravisPayload) -> str:@@ -49,7 +58,6 @@ def get_message_body(payload: TravisPayload) -> str:commit_message = (payload.message.strip().splitlines()[0] if payload.message else "(no commit message)")-status_message, emoji = STATUS_MAP[payload.status_message]body = MESSAGE_TEMPLATE.format(build_number=payload.number,@@ -64,6 +72,24 @@ def get_message_body(payload: TravisPayload) -> str:return body+def get_topic(payload: TravisPayload, event: str) -> str:++ if event == "pull_request":+ topic = TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(+ repo=payload.repository.name,+ type="PR",+ id=payload.pull_request_number,+ title=payload.pull_request_title,+ )+ else:+ topic = TOPIC_WITH_BRANCH_TEMPLATE.format(+ repo=payload.repository.name,+ branch=payload.branch,+ )++ return topic++@webhook_view("Travis", all_event_types=ALL_EVENT_TYPES)@typed_endpointdef api_travis_webhook(@@ -73,8 +99,9 @@ def api_travis_webhook(message: Annotated[Json[TravisPayload], ApiParamConfig("payload")],) -> HttpResponse:event = message.type+body = get_message_body(message)- topic_name = "builds"+ topic_name = get_topic(message, event)check_send_webhook_message(request, user_profile, topic_name, body, event)return json_success(request)