From c14e2fb7e3585fa5a08e0162ad0ba26fdc9a57b7 Mon Sep 17 00:00:00 2001 From: Mathis Date: Sun, 12 Jul 2026 18:41:25 +0200 Subject: [PATCH] =?UTF-8?q?V1.4=20de=20l'am=C3=A9lioration=20du=20suivi=20?= =?UTF-8?q?des=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ commandes/bug_report.py | 30 +++++++++++++------ commandes/gitlab_integration.py | 22 ++++++++++++-- commandes/gitlab_issues.json | 47 ------------------------------ scripts/migrate_gitlab_tracking.py | 2 +- scripts/recover_gitlab_comments.py | 5 ++-- 6 files changed, 47 insertions(+), 62 deletions(-) delete mode 100644 commandes/gitlab_issues.json diff --git a/.gitignore b/.gitignore index 649fd36..4d06eae 100755 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ config.db *.db-journal *.db-wal *.db-shm +*gitlab_issues.json +*forgejo_issues.json + # Dossiers de stockage data/ diff --git a/commandes/bug_report.py b/commandes/bug_report.py index 9eacde9..bb0ab43 100644 --- a/commandes/bug_report.py +++ b/commandes/bug_report.py @@ -13,14 +13,10 @@ from utils.premium import check_premium_tier kuby_logger = logging.getLogger("KubyBot") BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) -TRACKING_FILE = os.path.join(PROJECT_ROOT, "commandes", "gitlab_issues.json") +TRACKING_FILE = os.path.join(PROJECT_ROOT, "data", "gitlab_issues.json") LEGACY_REPORTS_FILE = os.path.join(PROJECT_ROOT, "data", "gitlab_reports.json") +LEGACY_TRACKING_FILE = os.path.join(PROJECT_ROOT, "commandes", "gitlab_issues.json") -# S'assure que le fichier de tracking est toujours écrit à l'emplacement attendu même si le service systemd démarre depuis un autre répertoire. -if not os.path.exists(TRACKING_FILE): - legacy_candidate = os.path.join(BASE_DIR, "commandes", "gitlab_issues.json") - if os.path.exists(legacy_candidate): - TRACKING_FILE = legacy_candidate def classify_webhook_event(payload: Dict[str, Any]) -> Optional[str]: @@ -268,14 +264,27 @@ def extract_requester_id_from_payload(payload: Dict[str, Any]) -> Optional[int]: def load_tracking(): """Charge le tracking depuis gitlab_issues.json (avec migration automatique)""" tracking = {} + + # 1. Tenter de charger depuis la nouvelle localisation dans data/ if os.path.exists(TRACKING_FILE): try: with open(TRACKING_FILE, "r", encoding="utf-8") as f: tracking = json.load(f) except Exception as e: - kuby_logger.error(f"Erreur chargement gitlab_issues.json: {e}") + kuby_logger.error(f"Erreur chargement gitlab_issues.json dans data/: {e}") + + # 2. Si non trouvé, tenter de charger depuis l'ancienne localisation dans commandes/ (migration) + if not tracking and os.path.exists(LEGACY_TRACKING_FILE): + try: + with open(LEGACY_TRACKING_FILE, "r", encoding="utf-8") as f: + tracking = json.load(f) + kuby_logger.info(f"Migration: Chargé l'ancien fichier de tracking depuis {LEGACY_TRACKING_FILE}") + # Sauvegarder immédiatement dans le nouvel emplacement + save_tracking(tracking) + except Exception as e: + kuby_logger.error(f"Erreur chargement ancien gitlab_issues.json dans commandes/: {e}") - # Migration automatique depuis l'ancien fichier + # Migration automatique depuis l'historique gitlab_reports.json if os.path.exists(LEGACY_REPORTS_FILE): try: with open(LEGACY_REPORTS_FILE, "r", encoding="utf-8") as f: @@ -296,13 +305,16 @@ def load_tracking(): } migrated_count += 1 - kuby_logger.info(f"Migration automatique: {migrated_count} issue(s) fusionnée(s) depuis l'historique") + if migrated_count > 0: + kuby_logger.info(f"Migration automatique: {migrated_count} issue(s) fusionnée(s) depuis l'historique") + save_tracking(tracking) except Exception as e: kuby_logger.error(f"Erreur migration gitlab_reports.json: {e}") return tracking + def save_tracking(data: Dict[str, Any]) -> None: """Sauvegarde dans gitlab_issues.json""" os.makedirs(os.path.dirname(TRACKING_FILE), exist_ok=True) diff --git a/commandes/gitlab_integration.py b/commandes/gitlab_integration.py index f964a5a..ca7657f 100644 --- a/commandes/gitlab_integration.py +++ b/commandes/gitlab_integration.py @@ -18,7 +18,9 @@ if not FORGEJO_TOKEN or not FORGEJO_OWNER or not FORGEJO_REPO: raise RuntimeError("Forgejo variables (FORGEJO_TOKEN, FORGEJO_OWNER, FORGEJO_REPO) must be set.") # Fichiers de persistance mis à jour pour Forgejo -ISSUE_TRACK_FILE = os.path.join(os.path.dirname(__file__), "forgejo_issues.json") +PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) +ISSUE_TRACK_FILE = os.path.join(PROJECT_ROOT, "data", "forgejo_issues.json") +LEGACY_ISSUE_TRACK_FILE = os.path.join(PROJECT_ROOT, "commandes", "forgejo_issues.json") def load_issue_tracking() -> Dict[str, Any]: tracking = {} @@ -27,12 +29,26 @@ def load_issue_tracking() -> Dict[str, Any]: with open(ISSUE_TRACK_FILE, "r", encoding="utf-8") as f: tracking = json.load(f) except Exception as e: - print(f"⚠️ Erreur chargement forgejo_issues.json: {e}") + print(f"⚠️ Erreur chargement forgejo_issues.json dans data/: {e}") + + if not tracking and os.path.exists(LEGACY_ISSUE_TRACK_FILE): + try: + with open(LEGACY_ISSUE_TRACK_FILE, "r", encoding="utf-8") as f: + tracking = json.load(f) + print(f"Migration: Chargé forgejo_issues.json depuis {LEGACY_ISSUE_TRACK_FILE}") + save_issue_tracking(tracking) + except Exception as e: + print(f"⚠️ Erreur chargement ancien forgejo_issues.json: {e}") + return tracking def save_issue_tracking(data: Dict[str, Any]) -> None: - with open(ISSUE_TRACK_FILE, "w", encoding="utf-8") as f: + os.makedirs(os.path.dirname(ISSUE_TRACK_FILE), exist_ok=True) + temp_file = f"{ISSUE_TRACK_FILE}.tmp" + with open(temp_file, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) + os.replace(temp_file, ISSUE_TRACK_FILE) + class ForgejoIntegration(commands.Cog): """Cog fournissant des commandes slash pour créer des tickets sur Forgejo et notifier les utilisateurs.""" diff --git a/commandes/gitlab_issues.json b/commandes/gitlab_issues.json deleted file mode 100644 index 4003089..0000000 --- a/commandes/gitlab_issues.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "4": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "5": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "15": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "153": { - "requester_id": 534003230435442690, - "last_note_id": 0, - "type": "legacy_report" - }, - "160": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "161": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "162": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "164": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "legacy_report" - }, - "None": { - "requester_id": 971446412690722826, - "last_note_id": 0, - "type": "manual-report" - } -} \ No newline at end of file diff --git a/scripts/migrate_gitlab_tracking.py b/scripts/migrate_gitlab_tracking.py index 0fdac66..c2cab7d 100644 --- a/scripts/migrate_gitlab_tracking.py +++ b/scripts/migrate_gitlab_tracking.py @@ -15,7 +15,7 @@ import sys # Chemins des fichiers BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -TRACKING_FILE = os.path.join(BASE_DIR, "commandes", "gitlab_issues.json") +TRACKING_FILE = os.path.join(BASE_DIR, "data", "gitlab_issues.json") LEGACY_REPORTS_FILE = os.path.join(BASE_DIR, "data", "gitlab_reports.json") diff --git a/scripts/recover_gitlab_comments.py b/scripts/recover_gitlab_comments.py index 3812f74..136484a 100644 --- a/scripts/recover_gitlab_comments.py +++ b/scripts/recover_gitlab_comments.py @@ -26,7 +26,7 @@ from typing import Dict, List, Any, Optional # Chemin des fichiers de tracking BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -GITLAB_ISSUES_FILE = os.path.join(BASE_DIR, "commandes", "gitlab_issues.json") +GITLAB_ISSUES_FILE = os.path.join(BASE_DIR, "data", "gitlab_issues.json") GITLAB_REPORTS_FILE = os.path.join(BASE_DIR, "data", "gitlab_reports.json") # Configuration GitLab @@ -231,7 +231,8 @@ async def recover_comments(): print("✅ RÉCUPÉRATION TERMINÉE") print("=" * 60) print("\nProchaines étapes:") - print("1. Vérifiez le fichier commandes/gitlab_issues.json") + print("1. Vérifiez le fichier data/gitlab_issues.json") + print("2. Redémarrez le bot pour qu'il utilise les nouvelles données") print("3. Les nouveaux commentaires seront désormais suivis correctement") -- 2.49.1