2023-12-05 21:14:17 +01:00
|
|
|
from urllib.parse import urlencode
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
|
|
|
|
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 TravisHookTests(WebhookTestCase):
|
2024-05-04 22:02:50 +02:00
|
|
|
CHANNEL_NAME = "travis"
|
2020-04-09 21:51:58 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/travis?stream={stream}&api_key={api_key}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "travis"
|
2024-01-17 15:53:30 +01:00
|
|
|
TOPIC_NAME = "builds"
|
2021-06-07 21:01:06 +02:00
|
|
|
EXPECTED_MESSAGE = """
|
|
|
|
Author: josh_mandel
|
|
|
|
Build status: Passed :thumbs_up:
|
|
|
|
Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6c457d366a31), [build log](https://travis-ci.org/hl7-fhir/fhir-svn/builds/92495257)
|
|
|
|
""".strip()
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_travis_message(self) -> None:
|
2016-09-20 22:51:11 +02:00
|
|
|
"""
|
|
|
|
Build notifications are generated by Travis after build completes.
|
|
|
|
|
|
|
|
The subject describes the repo and Stash "project". The
|
|
|
|
content describes the commits pushed.
|
|
|
|
"""
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
"build",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-06-07 21:01:06 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
2021-02-12 08:19:30 +01:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2017-03-06 07:12:40 +01:00
|
|
|
)
|
|
|
|
|
2024-07-18 23:44:59 +02:00
|
|
|
def test_travis_only_pull_request_event(self) -> None:
|
|
|
|
self.url = f'{self.build_webhook_url()}&only_events=["pull_request"]'
|
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
self.check_webhook(
|
2024-07-18 23:44:59 +02:00
|
|
|
"pull_request",
|
|
|
|
self.TOPIC_NAME,
|
|
|
|
self.EXPECTED_MESSAGE,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
2017-03-06 07:12:40 +01:00
|
|
|
)
|
|
|
|
|
2024-07-18 23:44:59 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
expect_noop=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_travis_exclude_pull_request_event(self) -> None:
|
|
|
|
self.url = f'{self.build_webhook_url()}&exclude_events=["pull_request"]'
|
2017-03-06 07:12:40 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"pull_request",
|
2024-07-18 23:44:59 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
expect_noop=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-06-07 21:01:06 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2016-09-20 22:51:11 +02:00
|
|
|
)
|
|
|
|
|
2021-05-17 20:26:48 +02:00
|
|
|
def test_travis_only_push_event(self) -> None:
|
|
|
|
self.url = f'{self.build_webhook_url()}&only_events=["push"]'
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-05-17 20:26:48 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"pull_request",
|
2021-05-17 20:26:48 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2021-06-30 09:46:14 +02:00
|
|
|
expect_noop=True,
|
2021-05-17 20:26:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_travis_exclude_push_event(self) -> None:
|
|
|
|
self.url = f'{self.build_webhook_url()}&exclude_events=["push"]'
|
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
2021-05-17 20:26:48 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2021-06-30 09:46:14 +02:00
|
|
|
expect_noop=True,
|
2021-05-17 20:26:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"pull_request",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-05-17 20:26:48 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_travis_include_glob_events(self) -> None:
|
2024-07-18 23:44:59 +02:00
|
|
|
self.url = f'{self.build_webhook_url()}&include_events=["*"]'
|
2021-05-17 20:26:48 +02:00
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"pull_request",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-05-17 20:26:48 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
2024-01-17 15:53:30 +01:00
|
|
|
self.TOPIC_NAME,
|
2021-05-17 20:26:48 +02:00
|
|
|
self.EXPECTED_MESSAGE,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_travis_exclude_glob_events(self) -> None:
|
2024-07-18 23:44:59 +02:00
|
|
|
self.url = f'{self.build_webhook_url()}&exclude_events=["*"]'
|
2021-05-17 20:26:48 +02:00
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"pull_request",
|
2021-05-17 20:26:48 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2021-06-30 09:46:14 +02:00
|
|
|
expect_noop=True,
|
2021-05-17 20:26:48 +02:00
|
|
|
)
|
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"build",
|
2021-05-17 20:26:48 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2021-06-30 09:46:14 +02:00
|
|
|
expect_noop=True,
|
2021-05-17 20:26:48 +02:00
|
|
|
)
|
|
|
|
|
2021-06-30 09:46:14 +02:00
|
|
|
def test_travis_noop(self) -> None:
|
|
|
|
expected_error_message = """
|
|
|
|
While no message is expected given expect_noop=True,
|
|
|
|
your test code triggered an endpoint that did write
|
|
|
|
one or more new messages.
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
with self.assertRaises(Exception) as exc:
|
|
|
|
self.check_webhook(
|
|
|
|
"build", content_type="application/x-www-form-urlencoded", expect_noop=True
|
|
|
|
)
|
|
|
|
self.assertEqual(str(exc.exception), expected_error_message)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_body(self, fixture_name: str) -> str:
|
2023-12-05 21:14:17 +01:00
|
|
|
return urlencode(
|
2021-02-12 08:20:45 +01:00
|
|
|
{"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|