Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Refactor `get_logo_path` to make `logo_url` non-optional.
Niloth P31a06f2ed0ce
diff --git a/tools/screenshots/generate-integration-docs-screenshot b/tools/screenshots/generate-integration-docs-screenshotindex c7a94414476..b8a7a709d06 100755--- a/tools/screenshots/generate-integration-docs-screenshot+++ b/tools/screenshots/generate-integration-docs-screenshot@@ -86,12 +86,13 @@ def create_integration_bot(integration: Integration, bot_name: str | None = Nonenotify_created_bot(bot)bot_avatar_path = integration.get_bot_avatar_path()- if bot_avatar_path is not None:- bot_avatar_path = static_path(bot_avatar_path)- if os.path.isfile(bot_avatar_path):- with open(bot_avatar_path, "rb") as f:- upload_avatar_image(f, bot)- do_change_avatar_fields(bot, UserProfile.AVATAR_FROM_USER, acting_user=owner)+ bot_avatar_path = static_path(bot_avatar_path)+ if os.path.isfile(bot_avatar_path):+ with open(bot_avatar_path, "rb") as f:+ upload_avatar_image(f, bot)+ do_change_avatar_fields(bot, UserProfile.AVATAR_FROM_USER, acting_user=owner)+ else:+ raise ValueError(f"Bot avatar file {bot_avatar_path} does not exist.")return botdiff --git a/tools/setup/generate_integration_bots_avatars.py b/tools/setup/generate_integration_bots_avatars.pyindex 805269bb06f..e074e34018e 100755--- a/tools/setup/generate_integration_bots_avatars.py+++ b/tools/setup/generate_integration_bots_avatars.py@@ -35,13 +35,7 @@ def create_integration_bot_avatar(logo_path: str, bot_avatar_path: str) -> None:def generate_integration_bots_avatars(check_missing: bool = False) -> None:missing = set()for integration in INTEGRATIONS.values():- if not integration.logo_path:- continue-bot_avatar_path = integration.get_bot_avatar_path()- if bot_avatar_path is None:- continue-bot_avatar_path = os.path.join(ZULIP_PATH, "static", bot_avatar_path)if check_missing:if not os.path.isfile(bot_avatar_path):diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.pyindex 388ac181407..8afe134ffaa 100644--- a/zerver/lib/integrations.py+++ b/zerver/lib/integrations.py@@ -163,6 +163,7 @@ class Integration:webhook_screenshot_configs: list[WebhookScreenshotConfig] | None = None,client_name: str | None = None,logo: str | None = None,+ fallback_logo_path: str | None = None,secondary_line_text: str | None = None,display_name: str | None = None,doc: str | None = None,@@ -203,9 +204,8 @@ class Integration:)self.categories = [CATEGORIES[c] for c in categories]- self.logo_path = logo if logo is not None else self.get_logo_path()- # TODO: Enforce that all integrations have logo_url with an assertion.- self.logo_url = self.get_logo_url()+ self.logo_path = logo if logo is not None else self.get_logo_path(fallback_logo_path)+ self.logo_url = staticfiles_storage.url(self.logo_path)if display_name is None:display_name = name.title()@@ -218,28 +218,25 @@ class Integration:def is_enabled(self) -> bool:return True- def get_logo_path(self) -> str | None:- logo_file_path_svg = self.DEFAULT_LOGO_STATIC_PATH_SVG.format(name=self.name)- logo_file_path_png = self.DEFAULT_LOGO_STATIC_PATH_PNG.format(name=self.name)- if os.path.isfile(static_path(logo_file_path_svg)):- return logo_file_path_svg- elif os.path.isfile(static_path(logo_file_path_png)):- return logo_file_path_png+ def get_logo_path(self, fallback_logo_path: str | None = None) -> str:+ paths_to_check = [+ self.DEFAULT_LOGO_STATIC_PATH_SVG.format(name=self.name),+ self.DEFAULT_LOGO_STATIC_PATH_PNG.format(name=self.name),+ ]+ if fallback_logo_path is not None:+ paths_to_check.append(fallback_logo_path)- return None+ for potential_path in paths_to_check:+ if os.path.isfile(static_path(potential_path)):+ return potential_path- def get_bot_avatar_path(self) -> str | None:- if self.logo_path is not None:- name = os.path.splitext(os.path.basename(self.logo_path))[0]- return self.DEFAULT_BOT_AVATAR_PATH.format(name=name)-- return None-- def get_logo_url(self) -> str | None:- if self.logo_path is not None:- return staticfiles_storage.url(self.logo_path)+ raise AssertionError(+ f"Could not find a logo for integration {self.name}. Paths checked: {', '.join(paths_to_check)}"+ )- return None+ def get_bot_avatar_path(self) -> str:+ name = os.path.splitext(os.path.basename(self.logo_path))[0]+ return self.DEFAULT_BOT_AVATAR_PATH.format(name=name)def get_translated_categories(self) -> list[str]:return [str(category) for category in self.categories]@@ -268,18 +265,10 @@ class BotIntegration(Integration):categories=categories,fixtureless_screenshot_config_options=fixtureless_screenshot_config_options,secondary_line_text=secondary_line_text,+ logo=logo,+ fallback_logo_path=self.ZULIP_LOGO_STATIC_PATH_PNG,)- if logo is None:- self.logo_url = self.get_logo_url()- if self.logo_url is None:- # TODO: Add a test for this by initializing one in a test.- self.logo_url = staticfiles_storage.url(- self.ZULIP_LOGO_STATIC_PATH_PNG- ) # nocoverage- else:- self.logo_url = staticfiles_storage.url(logo)-if display_name is None:display_name = f"{name.title()} Bot" # nocoverageelse:@@ -463,10 +452,12 @@ class EmbeddedBotIntegration(Integration):"""DEFAULT_CLIENT_NAME = "Zulip{name}EmbeddedBot"+ ZULIP_LOGO_STATIC_PATH_PNG = "images/logo/zulip-icon-128x128.png"def __init__(self, name: str, *args: Any, **kwargs: Any) -> None:assert kwargs.get("client_name") is Nonekwargs["client_name"] = self.DEFAULT_CLIENT_NAME.format(name=name.title())+ kwargs["fallback_logo_path"] = self.ZULIP_LOGO_STATIC_PATH_PNGsuper().__init__(name, *args, **kwargs)diff --git a/zerver/tests/test_integrations.py b/zerver/tests/test_integrations.pyindex 272d6a48657..eb1dcbefef6 100644--- a/zerver/tests/test_integrations.py+++ b/zerver/tests/test_integrations.py@@ -2,6 +2,7 @@ import osfrom zerver.lib.integrations import (BOT_INTEGRATIONS,+ EMBEDDED_BOTS,EMBEDDED_INTEGRATIONS,HUBOT_INTEGRATIONS,INTEGRATIONS,@@ -41,15 +42,40 @@ class IntegrationsTestCase(ZulipTestCase):self.assertEqual(fixture_path, "zerver/webhooks/airbrake/fixtures/error_message.json")self.assertEqual(image_path, "static/images/integrations/ci/002.png")+ def test_get_logo_path(self) -> None:+ # Test with an integration that passed logo as an argument+ integration = INTEGRATIONS["slack_incoming"]+ with self.assertRaises(AssertionError):+ integration.get_logo_path()++ # Test with an integration that has only a PNG option+ integration = INTEGRATIONS["onyx"]+ self.assertEqual(integration.get_logo_path(), "images/integrations/logos/onyx.png")++ # Test the fallback logo with an embedded integration without a logo+ ZULIP_LOGO_STATIC_PATH_PNG = "images/logo/zulip-icon-128x128.png"+ integration = EMBEDDED_BOTS[0]+ with self.assertRaises(AssertionError):+ integration.get_logo_path()+ self.assertEqual(+ integration.get_logo_path(ZULIP_LOGO_STATIC_PATH_PNG), ZULIP_LOGO_STATIC_PATH_PNG+ )++ # Test with a bot integration that has a logo+ # They use different DEFAULT_* paths.+ integration = INTEGRATIONS["xkcd"]+ logo_path = integration.get_logo_path()+ self.assertEqual(logo_path, "generated/bots/xkcd/logo.png")+ self.assertTrue(logo_path.startswith("generated/bots/"))+def test_get_bot_avatar_path(self) -> None:integration = INTEGRATIONS["alertmanager"]self.assertEqual(integration.get_bot_avatar_path(), "images/integrations/bot_avatars/prometheus.png")- # New instance with logo parameter not set- integration = WebhookIntegration("alertmanager", ["misc"])- self.assertIsNone(integration.get_bot_avatar_path())+ with self.assertRaises(AssertionError):+ integration = Integration("alertmanager", ["misc"])def test_no_missing_doc_screenshot_config(self) -> None:integration_names = {integration.name for integration in INTEGRATIONS.values()}