From c3d99f66138bc9cf8225e3697910777baf3951fb Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Mon, 19 Jun 2017 12:39:13 +0100 Subject: [PATCH] integrations/perforce: Skip messages for missing streams. For a large perforce server with many users, this allows projects to opt-in to zulip notifications by creating the stream for the branch to be monitored. Commits to paths for which a stream mapping yields a result, but no stream exists in Zulip, are simply dropped silently. This behaviour is opt-in, by setting the configuration key ZULIP_IGNORE_MISSING_STREAM = True in the zulip_perforce_config.py file. --- integrations/perforce/zulip_change-commit.py | 12 ++++++++++++ integrations/perforce/zulip_perforce_config.py | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/integrations/perforce/zulip_change-commit.py b/integrations/perforce/zulip_change-commit.py index ed1caf5..71c211d 100755 --- a/integrations/perforce/zulip_change-commit.py +++ b/integrations/perforce/zulip_change-commit.py @@ -72,10 +72,22 @@ except ValueError: metadata = git_p4.p4_describe(changelist) # type: Dict[str, str] destination = config.commit_notice_destination(changeroot, changelist) # type: Optional[Dict[str, str]] + if destination is None: # Don't forward the notice anywhere sys.exit(0) +ignore_missing_stream = None +if hasattr(config, "ZULIP_IGNORE_MISSING_STREAM"): + ignore_missing_stream = config.ZULIP_IGNORE_MISSING_STREAM + +if ignore_missing_stream: + # Check if the destination stream exists yet + stream_state = client.get_stream_id(destination["stream"]) + if stream_state["result"] == "error": + # Silently discard the message + sys.exit(0) + change = metadata["change"] p4web = None if hasattr(config, "P4_WEB"): diff --git a/integrations/perforce/zulip_perforce_config.py b/integrations/perforce/zulip_perforce_config.py index 2b23920..6827cc9 100644 --- a/integrations/perforce/zulip_perforce_config.py +++ b/integrations/perforce/zulip_perforce_config.py @@ -26,6 +26,11 @@ ZULIP_USER = "p4-bot@example.com" ZULIP_API_KEY = "0123456789abcdef0123456789abcdef" ZULIP_SITE = "https://zulip.example.com" +# Set this to True to silently drop messages if the destination stream +# does not exist. This prevents the warnings from Zulip's Notification Bot +# when commits are made on a branch for which no stream has been created. +ZULIP_IGNORE_MISSING_STREAM = False + # Set this to point at a p4web installation to get changelist IDs as links # P4_WEB = "https://p4web.example.com" P4_WEB = None