From 4eda2c4d0708880286015cbb41f9276f495721d4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 13 2018 18:59:33 +0000 Subject: Style fixes for the mqtt support and adjust the unit-tests as well Fixes https://pagure.io/pagure/issue/3869 Merges https://pagure.io/pagure/pull-request/3921 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index 4c35103..d82dc82 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -19,10 +19,12 @@ import datetime import hashlib import json import logging +import os import re import smtplib import time import six +import ssl from email.header import Header from email.mime.text import MIMEText from six.moves.urllib_parse import urljoin @@ -98,37 +100,50 @@ def stomp_publish(topic, message): except Exception: _log.exception("Error sending stomp message") + def blinker_publish(topic, message): _log.info("Sending blinker signal to: pagure - topic: %s", topic) ready = blinker.signal("pagure") ready.send("pagure", topic=topic, message=message) + def mqtt_publish(topic, message): """ Try to publish a message on a MQTT message bus. """ if not pagure_config.get("MQTT_NOTIFICATIONS", True): return + + mqtt_host = pagure_config.get("MQTT_HOST") + mqtt_port = pagure_config.get("MQTT_PORT") + + mqtt_username = pagure_config.get("MQTT_USERNAME") + mqtt_pass = pagure_config.get("MQTT_PASSWORD") + + mqtt_ca_certs = pagure_config.get("MQTT_CA_CERTS") + mqtt_certfile = pagure_config.get("MQTT_CERTFILE") + mqtt_keyfile = pagure_config.get("MQTT_KEYFILE") + mqtt_cert_reqs = pagure_config.get("MQTT_CERT_REQS", ssl.CERT_REQUIRED) + mqtt_tls_version = pagure_config.get("MQTT_TLS_VERSION", ssl.PROTOCOL_TLS) + mqtt_ciphers = pagure_config.get("MQTT_CIPHERS") + # We catch Exception if we want :-p # pylint: disable=broad-except # Ignore message about mqtt import # pylint: disable=import-error try: import paho.mqtt.client as mqtt - import os - - mqtt_host = pagure_config.get("MQTT_HOST") - mqtt_port = pagure_config.get("MQTT_PORT") - mqtt_username = pagure_config.get("MQTT_USERNAME") - mqtt_pass = pagure_config.get("MQTT_PASSWORD") - mqtt_ca_certs = pagure_config.get("MQTT_CA_CERTS") - mqtt_certfile = pagure_config.get("MQTT_CERTFILE") - mqtt_keyfile = pagure_config.get("MQTT_KEYFILE") - mqtt_cert_reqs = pagure_config.get("MQTT_CERT_REQS", "ssl.CERT_REQUIRED") - mqtt_tls_version = pagure_config.get("MQTT_TLS_VERSION","ssl.PROTOCOL_TLS") - mqtt_ciphers = pagure_config.get("MQTT_CIPHERS") client = mqtt.Client(os.uname()[1]) - client.tls_set(ca_certs=mqtt_ca_certs, certfile=mqtt_certfile, keyfile=mqtt_keyfile, cert_reqs=mqtt_cert_reqs, tls_version=mqtt_tls_version, cliphers=mqtt_ciphers) - client.username_pw_set(mqtt_username, mqtt_pass) + client.tls_set( + ca_certs=mqtt_ca_certs, + certfile=mqtt_certfile, + keyfile=mqtt_keyfile, + cert_reqs=mqtt_cert_reqs, + tls_version=mqtt_tls_version, + ciphers=mqtt_ciphers, + ) + if mqtt_username and mqtt_pass: + client.username_pw_set(mqtt_username, mqtt_pass) + client.connect(mqtt_host, mqtt_port) client.publish(topic, json.dumps(message)) client.disconnect() @@ -156,6 +171,13 @@ def log(project, topic, msg, webhook=True): ): stomp_publish(topic, msg) + # Send mqtt notification (if mqtt is there and set-up) + if not project or ( + project.settings.get("mqtt_notifications", True) + and not project.private + ): + mqtt_publish(topic, msg) + # Send blink notification to any 3rd party plugins, if there are any blinker_publish(topic, msg) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 00c97d2..555e9ac 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -3859,6 +3859,7 @@ class PagureFlaskApiProjectOptionsTests(tests.Modeltests): "issue_tracker": True, "issue_tracker_read_only": False, "issues_default_to_private": False, + "mqtt_notifications": True, "notify_on_commit_flag": False, "notify_on_pull-request_flag": False, "open_metadata_access_to_all": False, @@ -3922,6 +3923,7 @@ class PagureFlaskApiProjectOptionsTests(tests.Modeltests): "issue_tracker": True, "issue_tracker_read_only": False, "issues_default_to_private": False, + "mqtt_notifications": True, "notify_on_commit_flag": False, "notify_on_pull-request_flag": False, "open_metadata_access_to_all": False, @@ -3977,6 +3979,7 @@ class PagureFlaskApiProjectOptionsTests(tests.Modeltests): "issue_tracker": True, "issue_tracker_read_only": False, "issues_default_to_private": False, + "mqtt_notifications": True, "notify_on_commit_flag": False, "notify_on_pull-request_flag": False, "open_metadata_access_to_all": False, diff --git a/tests/test_pagure_lib.py b/tests/test_pagure_lib.py index e9b7507..f791e4d 100644 --- a/tests/test_pagure_lib.py +++ b/tests/test_pagure_lib.py @@ -1894,6 +1894,7 @@ class PagureLibtests(tests.Modeltests): 'fedmsg_notifications': True, 'stomp_notifications': True, 'pull_request_access_only': False, + 'mqtt_notifications': True, }, user='pingou', ) @@ -1937,6 +1938,7 @@ class PagureLibtests(tests.Modeltests): 'fedmsg_notifications': True, 'stomp_notifications': True, 'pull_request_access_only': False, + 'mqtt_notifications': True, }, user='pingou', ) diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 3299ee0..711cf43 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -1700,7 +1700,7 @@ new file mode 100644 index 0000000..60f7480 --- /dev/null +++ b/456 -@@ -0,0 +1,144 @@ +@@ -0,0 +1,146 @@ +{ + "assignee": null, + "branch": "master", @@ -1752,6 +1752,7 @@ index 0000000..60f7480 + "issue_tracker": true, + "issue_tracker_read_only": false, + "issues_default_to_private": false, ++ "mqtt_notifications": true, + "notify_on_commit_flag": false, + "notify_on_pull-request_flag": false, + "open_metadata_access_to_all": false, @@ -1810,6 +1811,7 @@ index 0000000..60f7480 + "issue_tracker": true, + "issue_tracker_read_only": false, + "issues_default_to_private": false, ++ "mqtt_notifications": true, + "notify_on_commit_flag": false, + "notify_on_pull-request_flag": false, + "open_metadata_access_to_all": false,