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: Improve message template.

sathwikshetty33dd3ee35dc0af
diff --git a/tools/test-backend b/tools/test-backend
index 819d8936dad..41d882f3fe1 100755
--- a/tools/test-backend
+++ b/tools/test-backend
@@ -123,7 +123,6 @@ not_yet_fully_covered = [
# Webhook integrations with incomplete coverage
"zerver/webhooks/greenhouse/view.py",
"zerver/webhooks/teamcity/view.py",
- "zerver/webhooks/travis/view.py",
"zerver/webhooks/zapier/view.py",
# This is hard to get test coverage for, and low value to do so
"zerver/views/sentry.py",
diff --git a/zerver/webhooks/travis/tests.py b/zerver/webhooks/travis/tests.py
index 75af2ab9bd8..de7718c4f88 100644
--- a/zerver/webhooks/travis/tests.py
+++ b/zerver/webhooks/travis/tests.py
@@ -7,17 +7,15 @@ from zerver.lib.test_classes import WebhookTestCase
class TravisHookTests(WebhookTestCase):
TOPIC_NAME = "builds"
- EXPECTED_NON_PULL_REQUEST_MESSAGE = """
-Author: sathwikshetty33
-Build status: Pending :counterclockwise:
-Details: [changes](https://github.com/sathwikshetty33/travis-test/commit/193da1b72346), [build log](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277712204)
-""".strip()
-
- EXPECTED_PULL_REQUEST_MESSAGE = """
-Author: Sathwik Shetty
-Build status: Failed :thumbs_down:
-Details: [changes](https://github.com/sathwikshetty33/travis-test/pull/2), [build log](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277927122)
-""".strip()
+ EXPECTED_NON_PULL_REQUEST_MESSAGE = (
+ ":counterclockwise: 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."
+ )
+
+ EXPECTED_PULL_REQUEST_MESSAGE = (
+ ":thumbs_down: Build [#42](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277927122) "
+ "**failed** for commit [feat: Created Name.txt.](https://github.com/sathwikshetty33/travis-test/pull/2) by Sathwik Shetty."
+ )
def test_travis_message(self) -> None:
"""
diff --git a/zerver/webhooks/travis/view.py b/zerver/webhooks/travis/view.py
index df7b5a0c2ba..d6975122927 100644
--- a/zerver/webhooks/travis/view.py
+++ b/zerver/webhooks/travis/view.py
@@ -11,18 +11,26 @@ from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
from zerver.lib.webhooks.common import check_send_webhook_message
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: {}
-Build status: {} {}
-Details: [changes]({}), [build log]({})"""
+STATUS_MAP = {
+ "Passed": ("passed", ":thumbs_up:"),
+ "Fixed": ("was fixed", ":thumbs_up:"),
+ "Failed": ("failed", ":thumbs_down:"),
+ "Broken": ("is broken", ":thumbs_down:"),
+ "Still Failing": ("is still failing", ":thumbs_down:"),
+ "Canceled": ("was canceled", ":thumbs_down:"),
+ "Errored": ("errored", ":thumbs_down:"),
+ "Pending": ("is in progress", ":counterclockwise:"),
+}
+
+MESSAGE_TEMPLATE = """
+{emoji} Build [#{build_number}]({build_url}) **{status}** \
+for commit [{commit_message}]({compare_url}) by {author}.
+""".strip()
class TravisPayload(BaseModel):
@@ -31,6 +39,29 @@ class TravisPayload(BaseModel):
compare_url: str
build_url: str
type: str
+ message: str | None = None
+ number: str
+
+
+def get_message_body(payload: TravisPayload) -> 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)"
+ )
+
+ status_message, emoji = STATUS_MAP[payload.status_message]
+ body = MESSAGE_TEMPLATE.format(
+ build_number=payload.number,
+ build_url=payload.build_url,
+ status=status_message,
+ commit_message=commit_message,
+ author=payload.author_name,
+ emoji=emoji,
+ compare_url=payload.compare_url,
+ )
+
+ return body
@webhook_view("Travis", all_event_types=ALL_EVENT_TYPES)
@@ -42,24 +73,7 @@ def api_travis_webhook(
message: Annotated[Json[TravisPayload], ApiParamConfig("payload")],
) -> HttpResponse:
event = message.type
- message_status = message.status_message
-
- if message_status in GOOD_STATUSES:
- emoji = ":thumbs_up:"
- elif message_status in BAD_STATUSES:
- emoji = ":thumbs_down:"
- elif message_status in PENDING_STATUSES:
- emoji = ":counterclockwise:"
- else:
- emoji = f"(No emoji specified for status '{message_status}'.)"
-
- body = MESSAGE_TEMPLATE.format(
- message.author_name,
- message_status,
- emoji,
- message.compare_url,
- message.build_url,
- )
+ body = get_message_body(message)
topic_name = "builds"
check_send_webhook_message(request, user_profile, topic_name, body, event)