From fd5acb85e916f010e063c09ce7a08dc91acd1eda Mon Sep 17 00:00:00 2001 From: Robert Coup Date: Fri, 27 Oct 2017 09:55:50 +0100 Subject: [PATCH] Suppress duplicate messages for multi-format package uploads. --- devpi_slack/main.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/devpi_slack/main.py b/devpi_slack/main.py index 4f77919..f8c241d 100644 --- a/devpi_slack/main.py +++ b/devpi_slack/main.py @@ -9,6 +9,11 @@ from devpi_slack import __version__ +# simple duplicate message suppression for multiple formats of the same package +# the hook is triggered once per format upload +last_message = None + + def devpiserver_indexconfig_defaults(): return {"slack_icon": None, "slack_hook": None, "slack_user": None} @@ -22,17 +27,23 @@ def devpiserver_on_upload_sync(log, application_url, stage, project, version): if not slack_hook: return + message = "Uploaded {}=={} to {}".format( + project, + version, + application_url + ) + global last_message + if message == last_message: + log.debug("skipping duplicate Slack notification: %s", message) + return + session = new_requests_session(agent=("devpi-slack", __version__)) try: r = session.post( slack_hook, data={ 'payload': json.dumps({ - "text": "Uploaded {}=={} to {}".format( - project, - version, - application_url - ), + "text": message, "icon_url": slack_icon, "username": slack_user, }) @@ -43,6 +54,7 @@ def devpiserver_on_upload_sync(log, application_url, stage, project, version): if 200 <= r.status_code < 300: log.info("successfully sent Slack notification: %s", slack_hook) + last_message = message else: log.error("%s: failed to send Slack notification: %s", r.status_code, slack_hook)