Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress duplicate messages for multi-format package uploads. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions devpi_slack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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,
})
Expand All @@ -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)
Expand Down