2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import patch
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-09-20 22:51:11 +02:00
|
|
|
class SemaphoreHookTests(WebhookTestCase):
|
2021-02-12 08:20:45 +01:00
|
|
|
STREAM_NAME = "semaphore"
|
2016-09-20 22:51:11 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/semaphore?stream={stream}&api_key={api_key}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "semaphore"
|
2016-09-20 22:51:11 +02:00
|
|
|
|
|
|
|
# Messages are generated by Semaphore on git push. The subject lines below
|
|
|
|
# contain information on the repo and branch, and the message has links and
|
|
|
|
# details about the build, deploy, server, author, and commit
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
# Tests for Semaphore Classic
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_semaphore_build(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "knighthood/master" # repo/branch
|
2019-04-17 03:57:31 +02:00
|
|
|
expected_message = """
|
|
|
|
[Build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) passed:
|
2022-11-07 21:24:35 +01:00
|
|
|
* **Commit**: [a490b8d508e: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf)
|
2019-04-17 03:57:31 +02:00
|
|
|
* **Author**: don@lamancha.com
|
|
|
|
""".strip()
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_semaphore_deploy(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "knighthood/master"
|
2019-04-17 03:57:31 +02:00
|
|
|
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:
|
2022-11-07 21:24:35 +01:00
|
|
|
* **Commit**: [a490b8d508e: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf)
|
2019-04-17 03:57:31 +02:00
|
|
|
* **Author**: don@lamancha.com
|
|
|
|
* **Server**: lamancha-271
|
|
|
|
""".strip()
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"deploy",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Tests for Semaphore 2.0
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore2_push(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/rw/webhook_impl" # repo/branch
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/acabe58e-4bcc-4d39-be06-e98d71917703) pipeline **stopped**:
|
2022-11-07 21:24:35 +01:00
|
|
|
* **Commit**: [(2d9f5fcec1c)](https://github.com/renderedtext/notifications/commit/2d9f5fcec1ca7c68fa7bd44dd58ec4ff65814563) Implement webhooks for SemaphoreCI
|
2020-03-14 13:12:03 +01:00
|
|
|
* **Branch**: rw/webhook_impl
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"push", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore2_push_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/rw/webhook_impl" # repo/branch
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/acabe58e-4bcc-4d39-be06-e98d71917703) pipeline **stopped**:
|
2022-11-07 21:24:35 +01:00
|
|
|
* **Commit**: (2d9f5fcec1c) Implement webhooks for SemaphoreCI
|
2020-03-14 13:12:03 +01:00
|
|
|
* **Branch**: rw/webhook_impl
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
2021-02-12 08:20:45 +01:00
|
|
|
with patch("zerver.webhooks.semaphore.view.is_github_repo", return_value=False):
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"push", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore_pull_request(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/test-notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/84383f37-d025-4811-b719-61c6acc92a1e) pipeline **failed**:
|
2021-05-10 07:02:14 +02:00
|
|
|
* **Pull request**: [Testing PR notifications](https://github.com/renderedtext/notifications/pull/3)
|
2020-03-14 13:12:03 +01:00
|
|
|
* **Branch**: test-notifications
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"pull_request", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore_pull_request_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/test-notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/84383f37-d025-4811-b719-61c6acc92a1e) pipeline **failed**:
|
2021-05-10 07:02:14 +02:00
|
|
|
* **Pull request**: Testing PR notifications (#3)
|
2020-03-14 13:12:03 +01:00
|
|
|
* **Branch**: test-notifications
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
2021-02-12 08:20:45 +01:00
|
|
|
with patch("zerver.webhooks.semaphore.view.is_github_repo", return_value=False):
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"pull_request", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore_tag(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped**:
|
|
|
|
* **Tag**: [v1.0.1](https://github.com/renderedtext/notifications/tree/v1.0.1)
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook("tag", expected_topic, expected_message, content_type="application/json")
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def test_semaphore_tag_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped**:
|
|
|
|
* **Tag**: v1.0.1
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
2021-02-12 08:20:45 +01:00
|
|
|
with patch("zerver.webhooks.semaphore.view.is_github_repo", return_value=False):
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"tag", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
2022-04-29 13:17:50 +02:00
|
|
|
def test_semaphore_unknown(self) -> None:
|
|
|
|
expected_topic = "knighthood/master"
|
|
|
|
expected_message = "unknown: passed"
|
|
|
|
self.check_webhook(
|
|
|
|
"unknown",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
def test_semaphore_unknown_event(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped** for unknown event
|
|
|
|
""".strip()
|
2021-02-12 08:19:30 +01:00
|
|
|
with patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zerver.webhooks.semaphore.tests.SemaphoreHookTests.get_body", self.get_unknown_event
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"tag", expected_topic, expected_message, content_type="application/json"
|
|
|
|
)
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def get_unknown_event(self, fixture_name: str) -> str:
|
|
|
|
"""Return modified payload with revision.reference_type changed"""
|
2021-02-12 08:19:30 +01:00
|
|
|
fixture_data = orjson.loads(
|
|
|
|
self.webhook_fixture_data("semaphore", fixture_name, file_type="json")
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_data["revision"]["reference_type"] = "unknown"
|
2020-03-14 13:12:03 +01:00
|
|
|
return fixture_data
|