diff --git a/zerver/webhooks/semaphore/fixtures/build.json b/zerver/webhooks/semaphore/fixtures/build.json index d2939c855e..e22d51bec1 100644 --- a/zerver/webhooks/semaphore/fixtures/build.json +++ b/zerver/webhooks/semaphore/fixtures/build.json @@ -14,7 +14,7 @@ "url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf", "author_name":"Don Quixote", "author_email":"don@lamancha.com", - "message":"Create user account for Rocinante :horse:.", + "message":"Create user account for Rocinante", "timestamp":"1605-01-16T00:01:41Z" } } diff --git a/zerver/webhooks/semaphore/fixtures/deploy.json b/zerver/webhooks/semaphore/fixtures/deploy.json index 6122da4e0b..f85cab1771 100644 --- a/zerver/webhooks/semaphore/fixtures/deploy.json +++ b/zerver/webhooks/semaphore/fixtures/deploy.json @@ -19,7 +19,7 @@ "url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf", "author_name":"Don Quixote", "author_email":"don@lamancha.com", - "message":"Create user account for Rocinante :horse:.", + "message":"Create user account for Rocinante", "timestamp":"2016-06-16T18:29:08Z" } } diff --git a/zerver/webhooks/semaphore/tests.py b/zerver/webhooks/semaphore/tests.py index f52eb00dfc..d692d1fd57 100644 --- a/zerver/webhooks/semaphore/tests.py +++ b/zerver/webhooks/semaphore/tests.py @@ -12,15 +12,22 @@ class SemaphoreHookTests(WebhookTestCase): def test_semaphore_build(self) -> None: expected_topic = u"knighthood/master" # repo/branch - expected_message = u"""[build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314): passed -!avatar(don@lamancha.com) [`a490b8d`](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf): Create user account for Rocinante :horse:.""" + expected_message = """ +[Build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) passed: +* **Commit**: [a490b8d: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf) +* **Author**: don@lamancha.com +""".strip() self.send_and_test_stream_message('build', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_semaphore_deploy(self) -> None: expected_topic = u"knighthood/master" - expected_message = u"""[deploy 17](https://semaphoreci.com/donquixote/knighthood/servers/lamancha-271/deploys/17) of [build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) on server lamancha-271: passed -!avatar(don@lamancha.com) [`a490b8d`](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf): Create user account for Rocinante :horse:.""" + expected_message = """ +[Deploy 17](https://semaphoreci.com/donquixote/knighthood/servers/lamancha-271/deploys/17) of [build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) passed: +* **Commit**: [a490b8d: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf) +* **Author**: don@lamancha.com +* **Server**: lamancha-271 +""".strip() self.send_and_test_stream_message('deploy', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") diff --git a/zerver/webhooks/semaphore/view.py b/zerver/webhooks/semaphore/view.py index d20d6b6671..21ebf1b03f 100644 --- a/zerver/webhooks/semaphore/view.py +++ b/zerver/webhooks/semaphore/view.py @@ -10,6 +10,21 @@ from zerver.lib.response import json_success from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile +BUILD_TEMPLATE = """ +[Build {build_number}]({build_url}) {status}: +* **Commit**: [{commit_hash}: {commit_message}]({commit_url}) +* **Author**: {email} +""".strip() + +DEPLOY_TEMPLATE = """ +[Deploy {deploy_number}]({deploy_url}) of [build {build_number}]({build_url}) {status}: +* **Commit**: [{commit_hash}: {commit_message}]({commit_url}) +* **Author**: {email} +* **Server**: {server_name} +""".strip() + +TOPIC_TEMPLATE = "{project}/{branch}" + @api_key_only_webhook_view('Semaphore') @has_request_variables def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile, @@ -29,7 +44,15 @@ def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile, if event == "build": build_url = payload["build_url"] build_number = payload["build_number"] - content = u"[build %s](%s): %s\n" % (build_number, build_url, result) + content = BUILD_TEMPLATE.format( + build_number=build_number, + build_url=build_url, + status=result, + commit_hash=commit_id[:7], + commit_message=message, + commit_url=commit_url, + email=author_email + ) elif event == "deploy": build_url = payload["build_html_url"] @@ -37,15 +60,27 @@ def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile, deploy_url = payload["html_url"] deploy_number = payload["number"] server_name = payload["server_name"] - content = u"[deploy %s](%s) of [build %s](%s) on server %s: %s\n" % \ - (deploy_number, deploy_url, build_number, build_url, server_name, result) + content = DEPLOY_TEMPLATE.format( + deploy_number=deploy_number, + deploy_url=deploy_url, + build_number=build_number, + build_url=build_url, + status=result, + commit_hash=commit_id[:7], + commit_message=message, + commit_url=commit_url, + email=author_email, + server_name=server_name + ) else: # should never get here - content = u"%s: %s\n" % (event, result) + content = "{event}: {result}".format( + event=event, result=result) - content += "!avatar(%s) [`%s`](%s): %s" % (author_email, commit_id[:7], - commit_url, message) - subject = u"%s/%s" % (project_name, branch_name) + subject = TOPIC_TEMPLATE.format( + project=project_name, + branch=branch_name + ) check_send_webhook_message(request, user_profile, subject, content) return json_success()