Implement receive mode webhooks in CLI

This commit is contained in:
Micah Lee 2021-04-11 13:33:58 -07:00
parent f4d71182d6
commit 29970d38ff
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
3 changed files with 54 additions and 4 deletions

View file

@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import tempfile
import json
import requests
from datetime import datetime
from flask import Request, request, render_template, make_response, flash, redirect
from werkzeug.utils import secure_filename
@ -101,6 +102,14 @@ class ReceiveModeWeb:
)
print(f"\nReceived: {local_path}")
# Send webhook if configured
if (
self.web.settings.get("receive", "webhook_url")
and not request.upload_error
and len(files) > 0
):
self.send_websocket_notification(f"{len(files)} files uploaded")
if request.upload_error:
self.common.log(
"ReceiveModeWeb",
@ -172,6 +181,22 @@ class ReceiveModeWeb:
return self.web.error403()
return upload(ajax=True)
def send_websocket_notification(self, data):
self.common.log("ReceiveModeWeb", "send_websocket_notification", data)
try:
requests.post(
self.web.settings.get("receive", "webhook_url"),
data=data,
timeout=5,
proxies=self.web.proxies,
)
except Exception as e:
self.common.log(
"ReceiveModeWeb",
"send_websocket_notification",
f"sending failed: {e}",
)
class ReceiveModeWSGIMiddleware(object):
"""