From e41cc6a80f2bc5c1b62bb1d66e5ae0c172ef5074 Mon Sep 17 00:00:00 2001 From: Jaimyn Mayer Date: Sun, 11 Aug 2024 13:50:56 +1000 Subject: [PATCH] added report issue to discord feature --- memberportal/api_member_tools/views.py | 45 ++++++++++++++++--- .../membermatters/constance_config.py | 12 +++++ memberportal/membermatters/settings.py | 5 +++ memberportal/services/discord.py | 36 +++++++++++++++ 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/memberportal/api_member_tools/views.py b/memberportal/api_member_tools/views.py index 9889894a..fe78b792 100644 --- a/memberportal/api_member_tools/views.py +++ b/memberportal/api_member_tools/views.py @@ -3,13 +3,17 @@ from api_meeting.models import Meeting from constance import config from services.emails import send_email_to_admin +from services import discord + from random import shuffle import requests from django.utils import timezone - from rest_framework import status, permissions from rest_framework.response import Response from rest_framework.views import APIView +import logging + +logger = logging.getLogger("api_member_tools") class SwipesList(APIView): @@ -109,6 +113,8 @@ def post(self, request): body = request.data title = body["title"] description = request.user.profile.get_full_name() + ": " + body["description"] + vikunja_task_url = None + trello_card_url = None if not (title and description): return Response(status=status.HTTP_400_BAD_REQUEST) @@ -199,12 +205,14 @@ def post(self, request): ) if (vikunja_label_id is not None) and ( - task_response.status_code == 200 + task_response.status_code == 201 ): + task_id = "unknown" try: - task_id = task_response.json["id"] + task_id = task_response.json()["id"] + vikunja_task_url = f"{config.VIKUNJA_API_URL}/tasks/{task_id}" label_body = { - "label_id": vikunja_label_id, + "label_id": int(vikunja_label_id), "created": "1970-01-01T00:00:00.000Z", } @@ -216,15 +224,29 @@ def post(self, request): "Authorization": "Bearer " + config.VIKUNJA_API_TOKEN }, ) + + if label_response.status_code != 201: + logger.warning( + f"Failed to add label to Vikunja task {task_id}: %s", + label_response.json(), + ) + except Exception: + logger.exception( + f"Failed to add label to Vikunja task {task_id}." + ) pass - if task_response.status_code != 200: + if task_response.status_code != 201: + logger.error( + "Failed to create Vikunja task: %s", task_response.json() + ) failed = True except Exception: # uh oh, but don't stop processing other ones failed = True + logger.exception("Failed to create reported issue Vikunja task.") if config.REPORT_ISSUE_ENABLE_TRELLO: try: @@ -248,9 +270,12 @@ def post(self, request): if response.status_code != 200: failed = True + trello_card_url = response.json()["shortUrl"] + except Exception: # uh oh, but don't stop processing other ones failed = True + logger.exception("Failed to create reported issue Trello card.") # email report if config.REPORT_ISSUE_ENABLE_EMAIL: @@ -271,6 +296,16 @@ def post(self, request): except Exception: # uh oh, but don't stop processing other ones failed = True + logger.exception("Failed to send reported issue email.") + + # discord report + if config.REPORT_ISSUE_ENABLE_DISCORD: + username = request.user.profile.get_full_name() + description = body["description"] + + discord.post_reported_issue_to_discord( + username, title, description, vikunja_task_url, trello_card_url + ) if failed: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/memberportal/membermatters/constance_config.py b/memberportal/membermatters/constance_config.py index 86c86f5d..d0d450b2 100644 --- a/memberportal/membermatters/constance_config.py +++ b/memberportal/membermatters/constance_config.py @@ -126,6 +126,11 @@ True, "Enable the submit issue to email integration.", ), + # Discord config + "REPORT_ISSUE_ENABLE_DISCORD": ( + False, + "Enable the submit issue to Discord integration.", + ), # Vikunja config "REPORT_ISSUE_ENABLE_VIKUNJA": ( False, @@ -232,6 +237,10 @@ "https://discordapp.com/api/webhooks/", "Discord URL to send webhook notifications to for vending/memberbucks purchases.", ), + "DISCORD_REPORT_ISSUE_WEBHOOK": ( + "https://discordapp.com/api/webhooks/", + "Discord URL to send webhook notifications to when reporting issues.", + ), "ENABLE_DISCOURSE_SSO_PROTOCOL": ( False, "Enable support for the discourse SSO protocol.", @@ -436,6 +445,7 @@ "SMS_ENABLE", "TWILIO_ACCOUNT_SID", "TWILIO_AUTH_TOKEN", + "TWILIO_AUTH_TOKEN", "SMS_DEFAULT_COUNTRY_CODE", "SMS_SENDER_ID", "SMS_MESSAGES", @@ -456,6 +466,7 @@ "Report Issue Services", ( "REPORT_ISSUE_ENABLE_EMAIL", + "REPORT_ISSUE_ENABLE_DISCORD", "REPORT_ISSUE_ENABLE_VIKUNJA", "REPORT_ISSUE_ENABLE_TRELLO", ), @@ -544,6 +555,7 @@ "DISCORD_DOOR_WEBHOOK", "DISCORD_INTERLOCK_WEBHOOK", "DISCORD_MEMBERBUCKS_PURCHASE_WEBHOOK", + "DISCORD_REPORT_ISSUE_WEBHOOK", ), ), ] diff --git a/memberportal/membermatters/settings.py b/memberportal/membermatters/settings.py index f7660f99..0791f8d2 100644 --- a/memberportal/membermatters/settings.py +++ b/memberportal/membermatters/settings.py @@ -278,6 +278,11 @@ "level": os.environ.get("MM_LOG_LEVEL_SPACEDIRECTORY", "INFO"), "propagate": False, }, + "api_member_tools": { + "handlers": ["console", "file"], + "level": os.environ.get("MM_LOG_LEVEL_MEMBER_TOOLS", "INFO"), + "propagate": False, + }, "metrics": { "handlers": ["console", "file"], "level": os.environ.get("MM_LOG_LEVEL_METRICS", "INFO"), diff --git a/memberportal/services/discord.py b/memberportal/services/discord.py index ce878dc6..ebb5b1f3 100644 --- a/memberportal/services/discord.py +++ b/memberportal/services/discord.py @@ -178,3 +178,39 @@ def post_purchase_to_discord(description): return True return True + + +def post_reported_issue_to_discord( + fullname, title, description, vikunja_task_url=None, trello_card_url=None +): + if config.ENABLE_DISCORD_INTEGRATION and config.DISCORD_REPORT_ISSUE_WEBHOOK: + logger.debug("Posting reported issue to Discord!") + + url = config.DISCORD_REPORT_ISSUE_WEBHOOK + + if vikunja_task_url or trello_card_url: + description += ( + f"\n\n[View in Vikunja]({vikunja_task_url})" if vikunja_task_url else "" + ) + description += ( + f"\n\n[View in Trello]({trello_card_url})" if trello_card_url else "" + ) + + json_message = { + "content": f"{fullname} just reported a new issue!", + "embeds": [], + } + + json_message["embeds"].append( + { + "title": title, + "description": description, + "color": 5025616, + } + ) + try: + requests.post(url, json=json_message, timeout=settings.REQUEST_TIMEOUT) + except requests.exceptions.ReadTimeout: + return True + + return True