From 1bad1fbec3ce2b5e68b2083de2966478f58c1b97 Mon Sep 17 00:00:00 2001
From: Quentin Duchemin <quentinduchemin@tuta.io>
Date: Wed, 2 Sep 2020 23:17:10 +0200
Subject: [PATCH] [WekanHooks] Remove unmaintainable and stupid text parsing.
 Keep original message.

---
 pica-wekan/filter-hooks/CHANGELOG.md |  5 ++++
 pica-wekan/filter-hooks/README.md    | 12 ++--------
 pica-wekan/filter-hooks/main.py      | 36 +++++-----------------------
 3 files changed, 13 insertions(+), 40 deletions(-)

diff --git a/pica-wekan/filter-hooks/CHANGELOG.md b/pica-wekan/filter-hooks/CHANGELOG.md
index 9a4bf03e..96b0dc3c 100644
--- a/pica-wekan/filter-hooks/CHANGELOG.md
+++ b/pica-wekan/filter-hooks/CHANGELOG.md
@@ -1,3 +1,8 @@
+## v0.2
+
+Remove unmaintainable and stupid text parsing.
+Keep original message.
+
 ## v0.1
 
 Initial release.
diff --git a/pica-wekan/filter-hooks/README.md b/pica-wekan/filter-hooks/README.md
index 3ca4eae4..d16f1cf9 100644
--- a/pica-wekan/filter-hooks/README.md
+++ b/pica-wekan/filter-hooks/README.md
@@ -5,7 +5,7 @@ Grâce aux hooks, on peut configurer Mattermost pour recevoir un message sur un
 
 Le souci est que Wekan [déclenche les hooks sortants pour chaque activité](https://github.com/wekan/wekan/wiki/Webhook-data), même les plus insignifiantes, ce qui spamme le canal.
 
-L'idée est donc de passer par un intermédiaire pour filter les hooks selon le type d'activité, et ne transmettre que les activités intéressantes. C'est ce que fait ce petit outil. Le code est très basique voire bancal et pourra être amélioré.
+L'idée est donc de passer par un intermédiaire pour filter les hooks selon le type d'activité, et ne transmettre que les activités intéressantes. C'est ce que fait ce petit outil. Le code est très basique et pourra être amélioré.
 
 ### Lancement
 
@@ -26,15 +26,7 @@ Dans Wekan, l'URL du hook sortant sera alors (à la place de `URL_HOOK`) :
 https://URL_FILTER/forward_hooks?url=URL_HOOK
 ```
 
-Quand il reçoit une requête, l'outil examine le hook et voit si le type d'activité correspond à une liste pré-définie.
-Si oui, il construit un objet JSON avec une description en français de la forme :
-```json
-{
-  "text": "Machin a créé la carte truc dans TODO"
-}
-```
-
-Puis l'envoye à `URL_HOOK`. La plupart des outils type Mattermost sont compatibles avec ce format (c'est celui utilisé par Wekan, d'ailleurs). Sur Mattermost, un message sera posté avec `text` pour contenu.
+Quand il reçoit une requête, l'outil examine le hook et voit si le type d'activité correspond à une liste pré-définie et l'envoie à `URL_HOOK` le cas échéant.
 
 ### Type d'activité pris en compte
 
diff --git a/pica-wekan/filter-hooks/main.py b/pica-wekan/filter-hooks/main.py
index 9cbee81a..f75927af 100644
--- a/pica-wekan/filter-hooks/main.py
+++ b/pica-wekan/filter-hooks/main.py
@@ -2,7 +2,6 @@
 # -*- coding: utf-8 -*-
 
 import json
-import os
 import logging
 
 import requests
@@ -23,11 +22,11 @@ app = Flask(__name__)
 
 # List of hooks to forward to the real hook URL
 # See https://github.com/wekan/wekan/wiki/Webhook-data
-allowed_hooks = {
-    'act-createCard': 'créé',
-    'act-moveCard': 'déplacé',
-    'act-addComment': 'commenté'
-}
+allowed_hooks = [
+    'act-createCard'
+    'act-moveCard'
+    'act-addComment'
+]
 
 @app.route('/forward_hooks', methods=['POST'])
 def filter_hook():
@@ -35,31 +34,8 @@ def filter_hook():
     event = hook_data['description']
     # Check if we must forward hook
     if event in allowed_hooks:
-        # Retrieve card URL
-        card_url_pos = hook_data['text'].index(os.environ['KANBAN_URL'])
-        card_url = hook_data['text'][card_url_pos:]
-
-        # Text like '<user> a <verbe> la carte <nom>' with a link
-        text = f'{hook_data["user"]} a [{allowed_hooks[event]} la carte **{hook_data["card"]}**]({card_url})'
-
-        # If card moved, add origin and destination lists
-        if event == 'act-moveCard':
-            # Yes, I'm ashamed of this ugly parsing, thanks for asking.
-            from_list = hook_data['text'].split('from list')[1].split('"')[1]
-            to_list = hook_data['text'].split('to list')[1].split('"')[1]
-            text += f' de {from_list} vers {to_list}.'
-
-        # If there is a new comment, add it as a citation
-        elif event == 'act-addComment':
-            text += f'\n>{hook_data["comment"]}'
-
-        # If new card, add the list it belongs to
-        elif event == 'act-createCard':
-            to_list = hook_data['text'].split('to list')[1].split('"')[1]
-            text += f' dans {to_list}'
-
         # Forward the hook
-        r = requests.post(request.args['url'], json={'text': text})
+        r = requests.post(request.args['url'], json=hook_data)
         if r.status_code != 200:
             logging.error(f'Unable to forward hook : {r}')
         else:
-- 
GitLab