Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add thread screenshots for fixtureless integrations.
harsh_0303557b2d17ddf5
diff --git a/static/images/integrations/errbot/000.png b/static/images/integrations/errbot/000.pngdeleted file mode 100644index 05e7e88879e..00000000000Binary files a/static/images/integrations/errbot/000.png and /dev/null differdiff --git a/static/images/integrations/errbot/001.png b/static/images/integrations/errbot/001.pngnew file mode 100644index 00000000000..ec5711aef8bBinary files /dev/null and b/static/images/integrations/errbot/001.png differdiff --git a/static/images/integrations/github_detail/001.png b/static/images/integrations/github_detail/001.pngnew file mode 100644index 00000000000..b191d6bae7aBinary files /dev/null and b/static/images/integrations/github_detail/001.png differdiff --git a/static/images/integrations/hubot/001.png b/static/images/integrations/hubot/001.pngindex bde65f7547b..4b215c115fe 100644Binary files a/static/images/integrations/hubot/001.png and b/static/images/integrations/hubot/001.png differdiff --git a/static/images/integrations/irc/001.png b/static/images/integrations/irc/001.pngindex c6f3a5214cd..5db5b4895da 100644Binary files a/static/images/integrations/irc/001.png and b/static/images/integrations/irc/001.png differdiff --git a/static/images/integrations/matrix/001.png b/static/images/integrations/matrix/001.pngnew file mode 100644index 00000000000..61d092bd747Binary files /dev/null and b/static/images/integrations/matrix/001.png differdiff --git a/static/images/integrations/matrix/002.png b/static/images/integrations/matrix/002.pngnew file mode 100644index 00000000000..d9b30c04278Binary files /dev/null and b/static/images/integrations/matrix/002.png differdiff --git a/static/images/integrations/xkcd/001.png b/static/images/integrations/xkcd/001.pngnew file mode 100644index 00000000000..0a290469dffBinary files /dev/null and b/static/images/integrations/xkcd/001.png differdiff --git a/templates/zerver/integrations/errbot.md b/templates/zerver/integrations/errbot.mdindex ea957623f1a..928a0fd977b 100644--- a/templates/zerver/integrations/errbot.md+++ b/templates/zerver/integrations/errbot.md@@ -52,7 +52,7 @@ Run your favorite chatbot in Zulip!{!congrats.md!}-+### Related documentationdiff --git a/tools/screenshots/generate-integration-docs-screenshot b/tools/screenshots/generate-integration-docs-screenshotindex 34d1bef8752..8c85d0bbca0 100755--- a/tools/screenshots/generate-integration-docs-screenshot+++ b/tools/screenshots/generate-integration-docs-screenshot@@ -37,6 +37,7 @@ import requestsfrom scripts.lib.zulip_tools import BOLDRED, ENDCfrom tools.lib.test_script import prepare_puppeteer_runfrom zerver.actions.create_user import do_create_user, notify_created_bot+from zerver.actions.message_send import do_send_messages, internal_prep_stream_messagefrom zerver.actions.streams import bulk_add_subscriptionsfrom zerver.actions.user_settings import do_change_avatar_fieldsfrom zerver.lib.integrations import (@@ -53,12 +54,14 @@ from zerver.lib.integrations import (split_fixture_path,)from zerver.lib.storage import static_path-from zerver.lib.streams import create_stream_if_needed+from zerver.lib.streams import create_stream_if_needed, ensure_streamfrom zerver.lib.upload import upload_avatar_image+from zerver.lib.url_encoding import topic_narrow_urlfrom zerver.lib.webhooks.common import call_fixture_to_headers, parse_multipart_string-from zerver.models import Message, UserProfile-from zerver.models.realms import get_realm+from zerver.models import Message, Stream, UserProfile+from zerver.models.realms import Realm, get_realmfrom zerver.models.users import get_user_by_delivery_email+from zerver.webhooks.fixtureless_integrations import MessageThreaddef create_integration_bot(integration: Integration, bot_name: str | None = None) -> UserProfile:@@ -108,6 +111,26 @@ def create_integration_channel(channel_name: str, bot: UserProfile) -> None:bulk_add_subscriptions(realm, [channel], [bot, bot.bot_owner], acting_user=bot)+def create_user(+ realm: Realm, username: str, acting_user: UserProfile, channel: Stream+) -> UserProfile:+ try:+ user = UserProfile.objects.get(realm=realm, full_name=username)+ except UserProfile.DoesNotExist:+ sender_email = f"{username.replace(' ', '').lower()}@example.com"+ user = do_create_user(+ email=sender_email,+ password="123",+ realm=realm,+ full_name=username,+ acting_user=acting_user,+ )++ bulk_add_subscriptions(realm, [channel], [user], acting_user=acting_user)++ return user++def get_fixture_info(fixture_path: str) -> tuple[Any, bool, bool, str]:json_fixture = fixture_path.endswith(".json")multipart_fixture = fixture_path.endswith(".multipart")@@ -242,6 +265,69 @@ def capture_last_message_screenshot(bot: UserProfile, image_path: str) -> None:subprocess.check_call(["node", screenshot_script, message_id, image_path, realm.url])+def send_thread_messages(+ bot: UserProfile,+ channel_name: str,+ topic: str,+ thread_messages_data: list[MessageThread],+ integration: Integration,+) -> list[int]:+ realm = bot.realm+ channel = ensure_stream(realm, channel_name, acting_user=bot.bot_owner)+ assert bot.bot_owner is not None+ user = create_user(+ realm=realm, username="Bo Williams", acting_user=bot.bot_owner, channel=channel+ )++ Message.objects.filter(+ realm=realm,+ recipient=channel.recipient,+ subject__iexact=topic,+ ).delete()++ message_requests = []+ for messages in thread_messages_data:+ sender_name = messages["sender"]+ content = messages["message"]++ if sender_name in {bot.full_name, integration.display_name, integration.name.title()}:+ sender = bot+ else:+ sender = user++ message_request = internal_prep_stream_message(+ sender,+ channel,+ topic,+ content,+ )+ message_requests.append(message_request)++ sent_message_results = do_send_messages(message_requests)+ return [result.message_id for result in sent_message_results]+++def capture_thread_screenshot(+ bot: UserProfile, channel_name: str, topic: str, message_ids: list[int], image_path: str+) -> None:+ assert bot.bot_owner is not None+ stream = ensure_stream(bot.realm, channel_name, acting_user=bot.bot_owner)+ narrow_uri = topic_narrow_url(realm=bot.realm, stream=stream, topic_name=topic)+ narrow = f"{channel_name}/{topic}"+ screenshot_script = os.path.join(SCREENSHOTS_DIR, "thread-screenshot.ts")+ subprocess.check_call(+ [+ "node",+ screenshot_script,+ narrow_uri,+ narrow,+ str(min(message_ids)),+ image_path,+ bot.realm.url,+ ]+ )++def generate_screenshots_for_integration(integration: Integration) -> None:if integration.screenshot_configs is not None:for screenshot_config in integration.screenshot_configs:@@ -256,23 +342,30 @@ def generate_screenshot_from_config(channel_name = screenshot_config.channel or integration.namecreate_integration_channel(channel_name, bot)image_path = get_image_path(integration, screenshot_config)-- if isinstance(integration, IncomingWebhookIntegration):- assert isinstance(screenshot_config, WebhookScreenshotConfig)- fixture_path = get_fixture_path(integration, screenshot_config)- message_sent = send_bot_payload_message(bot, integration, fixture_path, screenshot_config)- else:- assert isinstance(screenshot_config, FixturelessScreenshotConfig)- send_bot_mock_message(- bot,- channel=channel_name,- topic=screenshot_config.topic,- message=screenshot_config.message,- )- message_sent = True-- if message_sent:- capture_last_message_screenshot(bot, image_path)+ match (integration, screenshot_config):+ case (IncomingWebhookIntegration(), WebhookScreenshotConfig()):+ fixture_path = get_fixture_path(integration, screenshot_config)+ if send_bot_payload_message(bot, integration, fixture_path, screenshot_config):+ capture_last_message_screenshot(bot, image_path)+ case (_, FixturelessScreenshotConfig(message=messages)) if isinstance(messages, list):+ message_ids = send_thread_messages(+ bot, channel_name, screenshot_config.topic, messages, integration+ )+ if message_ids:+ capture_thread_screenshot(+ bot, channel_name, screenshot_config.topic, message_ids, image_path+ )+ case (_, FixturelessScreenshotConfig(message=message)) if isinstance(message, str):+ send_bot_mock_message(+ bot,+ channel=channel_name,+ topic=screenshot_config.topic,+ message=message,+ )+ capture_last_message_screenshot(bot, image_path)+ case _:+ raise ValueError(f"Invalid configuration for integration {integration.name}")+ if os.path.exists(image_path):print(f"Screenshot captured to: {BOLDRED}{image_path}{ENDC}")diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.pyindex 92f401f68bb..a90c530f7a2 100644--- a/zerver/lib/integrations.py+++ b/zerver/lib/integrations.py@@ -86,6 +86,12 @@ FIXTURELESS_INTEGRATIONS_WITH_SCREENSHOTS: list[str] = ["rss","svn","trac",+ "errbot",+ "github_detail",+ "hubot",+ "irc",+ "matrix",+ "xkcd",]FIXTURELESS_SCREENSHOT_CONTENT: dict[str, list[fixtureless_integrations.ScreenshotContent]] = {key: [getattr(fixtureless_integrations, key.upper().replace("-", "_"))]@@ -120,7 +126,7 @@ class FixturelessScreenshotConfigOptions:@dataclassclass FixturelessScreenshotConfig:- message: str+ message: str | list[fixtureless_integrations.MessageThread]topic: strchannel: str | None = Noneimage_name: str = "001.png"@@ -1150,16 +1156,6 @@ INTEGRATIONS_MISSING_SCREENSHOT_CONFIG = (| {"intercom", "notion"}# Integrations that call external API endpoints.| {"slack"}- # Integrations that require screenshots of message threads - support is yet to be added- | {- "errbot",- "github_detail",- "hubot",- "irc",- # Also requires a screenshot on the Matrix side of the bridge- "matrix",- "xkcd",- }| hubot_integration_names)diff --git a/zerver/tests/test_docs.py b/zerver/tests/test_docs.pyindex ff57cbe1b87..9b2fe044499 100644--- a/zerver/tests/test_docs.py+++ b/zerver/tests/test_docs.py@@ -508,6 +508,11 @@ class DocPageTest(ZulipTestCase):"intercom/001.png","logos/hubot.png","hubot/001.png",+ # Images newly added to python-zulip-api docs+ "github_detail/001.png",+ "matrix/001.png",+ "matrix/002.png",+ "xkcd/001.png",}images_in_dir.update(exception_images)images_in_docs.update(exception_images)diff --git a/zerver/webhooks/fixtureless_integrations.py b/zerver/webhooks/fixtureless_integrations.pyindex 97c926537da..b36fe94baef 100644--- a/zerver/webhooks/fixtureless_integrations.py+++ b/zerver/webhooks/fixtureless_integrations.py@@ -58,9 +58,14 @@ DATE_ISO_8601 = _DT.date().isoformat()DATE_LONG = _DT.strftime("%A, %B %d, %Y")+class MessageThread(TypedDict):+ sender: str+ message: str++class ScreenshotContent(TypedDict):topic: str- content: str+ content: str | list[MessageThread]ASANA = ScreenshotContent(@@ -88,6 +93,22 @@ DISCOURSE = ScreenshotContent(> {COMMIT_BODY_A}""",)+ERRBOT = ScreenshotContent(+ topic="bot commands",+ content=[+ {"sender": f"{BO_NAME}", "message": "@**Errbot Bot** help"},+ {"sender": "Errbot", "message": "Available commands: `!help`, `!about`, `!config`"},+ {"sender": f"{BO_NAME}", "message": "!about"},+ {+ "sender": "Errbot",+ "message": """This is Errbot version 6.2.0+* Visit http://errbot.io/ for more information about errbot in general.+* Visit http://errbot.io/en/latest/#user-guide for help with configuration,administration and plugin development.+* Errbot is built through the hard work and dedication of everyone who contributes code, documentation and bug reports at https://github.com/errbotio.""",+ },+ ],+)+GIT = ScreenshotContent(topic=BRANCH_GIT,content=f"""`{DEPLOYMENT_HASH[:12]}` was deployed to `{BRANCH_GIT}` with:@@ -102,6 +123,25 @@ GITHUB_ACTIONS = ScreenshotContent(> Unable to connect.""",)+GITHUB_DETAIL = ScreenshotContent(+ topic="Issue and PR details",+ content=[+ {"sender": f"{BO_NAME}", "message": "@**GitHub Detail Bot** #1234"},+ {+ "sender": "GitHub Detail Bot",+ "message": """**[zulip/zulip#1234]() - Fix login bug affecting multiple users**+Created by **[octocat]()**+Status - **Open**+```quote+Users are experiencing issues when attempting to log in with valid credentials. The login button appears unresponsive and authentication fails silently. This affects both new and existing users across multiple browsers.++Expected: User should be logged in successfully+Actual: Login fails without error message+```""",+ },+ ],+)+GOOGLE_CALENDAR = ScreenshotContent(topic="Team reminders",content=f"""The [Development Sync]() event is scheduled from 2 PM - 3 PM on {DATE_LONG} at Conference Room B.@@ -110,6 +150,28 @@ GOOGLE_CALENDAR = ScreenshotContent([Join call]().""",)+HUBOT = ScreenshotContent(+ topic="general",+ content=[+ {"sender": f"{BO_NAME}", "message": "@**Hubot** pug me"},+ {+ "sender": "Hubot",+ "message": "https://29.media.tumblr.com/tumblr_ll3xcmS6Bh1qb08qmo1_500.jpg",+ },+ ],+)++IRC = ScreenshotContent(+ topic="IRC",+ content=[+ {"sender": f"{BO_NAME}", "message": "Message from Zulip!"},+ {+ "sender": "IRC Bot",+ "message": "irc_user: Hi!",+ },+ ],+)+JENKINS = ScreenshotContent(topic=PROJECT_NAME,content=f"**Build:** [#{REVISION_NUMBER}](): FAILURE :cross_mark:",@@ -122,6 +184,17 @@ Google’s recent unveiling of what it calls a “new class of agentic experienchttps://www.technologyreview.com/{DATE_ISO_8601.replace("-", "/")}/1119545/dont-let-hype-about-ai-agents-get-ahead-of-reality/""",)+MATRIX = ScreenshotContent(+ topic="Matrix",+ content=[+ {"sender": f"{BO_NAME}", "message": "Message from Zulip!"},+ {+ "sender": "Matrix Bot",+ "message": f"**{BO_NAME}** [@johnsmith:matrix.org]: Hi from Matrix!",+ },+ ],+)+MERCURIAL = ScreenshotContent(topic=BRANCH_MERCURIAL,content=f"""**{BO_NAME}** pushed [2 commits]() to **{BRANCH_MERCURIAL}** (`{REVISION_NUMBER}:{DEPLOYMENT_HASH[:12]}`):@@ -184,3 +257,18 @@ TRAC = ScreenshotContent(status: **new** => **closed**, resolution: => **fixed**""",)++XKCD = ScreenshotContent(+ topic="xkcd",+ content=[+ {"sender": f"{BO_NAME}", "message": "@**xkcd Bot** help"},+ {+ "sender": "xkcd Bot",+ "message": """xkcd bot supports these commands:+* `@**xkcd** help` to show this help message.+* `@**xkcd** latest` to fetch the latest comic strip from xkcd.+* `@**xkcd** random` to fetch a random comic strip from xkcd.+* `@**xkcd** <comic id>` to fetch a comic strip based on `<comic id>` e.g `@**xkcd** 1234`.""",+ },+ ],+)