webhooks/semaphore: Improve message formatting and punctuation.

This commit is contained in:
Eeshan Garg 2019-04-16 23:27:31 -02:30 committed by Tim Abbott
parent 9d79becc0e
commit 674fcfcce1
4 changed files with 55 additions and 13 deletions

View File

@ -14,7 +14,7 @@
"url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf", "url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf",
"author_name":"Don Quixote", "author_name":"Don Quixote",
"author_email":"don@lamancha.com", "author_email":"don@lamancha.com",
"message":"Create user account for Rocinante :horse:.", "message":"Create user account for Rocinante",
"timestamp":"1605-01-16T00:01:41Z" "timestamp":"1605-01-16T00:01:41Z"
} }
} }

View File

@ -19,7 +19,7 @@
"url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf", "url":"https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf",
"author_name":"Don Quixote", "author_name":"Don Quixote",
"author_email":"don@lamancha.com", "author_email":"don@lamancha.com",
"message":"Create user account for Rocinante :horse:.", "message":"Create user account for Rocinante",
"timestamp":"2016-06-16T18:29:08Z" "timestamp":"2016-06-16T18:29:08Z"
} }
} }

View File

@ -12,15 +12,22 @@ class SemaphoreHookTests(WebhookTestCase):
def test_semaphore_build(self) -> None: def test_semaphore_build(self) -> None:
expected_topic = u"knighthood/master" # repo/branch expected_topic = u"knighthood/master" # repo/branch
expected_message = u"""[build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314): passed expected_message = """
!avatar(don@lamancha.com) [`a490b8d`](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf): Create user account for Rocinante :horse:.""" [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, self.send_and_test_stream_message('build', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_semaphore_deploy(self) -> None: def test_semaphore_deploy(self) -> None:
expected_topic = u"knighthood/master" 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 expected_message = """
!avatar(don@lamancha.com) [`a490b8d`](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf): Create user account for Rocinante :horse:.""" [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, self.send_and_test_stream_message('deploy', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")

View File

@ -10,6 +10,21 @@ from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile 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') @api_key_only_webhook_view('Semaphore')
@has_request_variables @has_request_variables
def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile, 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": if event == "build":
build_url = payload["build_url"] build_url = payload["build_url"]
build_number = payload["build_number"] 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": elif event == "deploy":
build_url = payload["build_html_url"] 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_url = payload["html_url"]
deploy_number = payload["number"] deploy_number = payload["number"]
server_name = payload["server_name"] server_name = payload["server_name"]
content = u"[deploy %s](%s) of [build %s](%s) on server %s: %s\n" % \ content = DEPLOY_TEMPLATE.format(
(deploy_number, deploy_url, build_number, build_url, server_name, result) 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 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], subject = TOPIC_TEMPLATE.format(
commit_url, message) project=project_name,
subject = u"%s/%s" % (project_name, branch_name) branch=branch_name
)
check_send_webhook_message(request, user_profile, subject, content) check_send_webhook_message(request, user_profile, subject, content)
return json_success() return json_success()