-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1609 lines (1357 loc) · 58.5 KB
/
Copy pathmain.py
File metadata and controls
1609 lines (1357 loc) · 58.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
GitLab Issues Analyzer - Main Entry Point
This is the main entry point for the GitLab Issues Analyzer application.
It supports both webhook and polling modes for detecting new GitLab issues.
"""
import argparse
import logging
import re
import signal
import sys
from datetime import datetime
from threading import Event, Thread
from typing import Any, Dict, List, Optional, Union
import pytz
from flask import Flask, jsonify, render_template, request
from src.analysis_cache import AnalysisCache
from src.analyzer import IssueAnalyzer
from src.config import load_config, validate_config
from src.email_sender import EmailSender
from src.exceptions import AnalysisError, ConfigurationError, EmailError, GitLabAPIError
from src.gitlab_client import GitLabClient
from src.monitor import IssueMonitor
from src.reporter import generate_email_report, LABEL_COLORS
# Global flag for graceful shutdown
shutdown_event = Event()
app = Flask(__name__)
# Disable Werkzeug's default request logging to use timezone-aware logging
werkzeug_logger = logging.getLogger("werkzeug")
werkzeug_logger.setLevel(logging.WARNING)
# Global components
gitlab_client: Optional[GitLabClient] = None
analyzer: Optional[IssueAnalyzer] = None
email_sender: Optional[EmailSender] = None
monitor: Optional[IssueMonitor] = None
analysis_cache: Optional[AnalysisCache] = None
config: Optional[dict] = None
dry_run: bool = False
logger: Optional[logging.Logger] = None
def setup_logging(log_level: str = "INFO", timezone: str = "Asia/Ho_Chi_Minh") -> logging.Logger:
"""
Set up logging configuration with timezone support.
Args:
log_level: Logging level (DEBUG, INFO, WARNING, ERROR)
timezone: Timezone string (e.g., 'Asia/Ho_Chi_Minh')
Returns:
Configured logger
"""
class TimezoneFormatter(logging.Formatter):
def __init__(self, tz_str):
super().__init__(
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt=None
)
try:
self.tz = pytz.timezone(tz_str)
except pytz.exceptions.UnknownTimeZoneError:
self.tz = pytz.UTC
def formatTime(self, record, datefmt=None):
dt = datetime.fromtimestamp(record.created, tz=self.tz)
if datefmt:
return dt.strftime(datefmt)
return dt.strftime("%Y-%m-%d %H:%M:%S")
try:
pytz.timezone(timezone)
except pytz.exceptions.UnknownTimeZoneError:
basic_logger = logging.getLogger(__name__)
basic_logger.warning(f"Unknown timezone: {timezone}, falling back to UTC")
timezone = "UTC"
formatter = TimezoneFormatter(timezone)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.setLevel(getattr(logging, log_level.upper(), logging.INFO))
root_logger.handlers = [handler]
# Configure Werkzeug logger to use timezone-aware timestamps
class WerkzeugTimezoneFormatter(logging.Formatter):
"""Formatter for Werkzeug access logs with timezone-aware timestamps."""
def __init__(self, tz_str):
try:
self.tz = pytz.timezone(tz_str)
except pytz.exceptions.UnknownTimeZoneError:
self.tz = pytz.UTC
# Use Werkzeug's default format but with timezone-aware timestamps
super().__init__(
fmt='%(message)s',
datefmt=None
)
def format(self, record):
# Format the timestamp in timezone
dt = datetime.fromtimestamp(record.created, tz=self.tz)
timestamp = dt.strftime("%d/%b/%Y %H:%M:%S")
# Werkzeug access logs format: "IP - - [timestamp] "method path" status"
# We need to preserve the original message but replace the timestamp
original_msg = record.getMessage()
# Try to find and replace timestamp in Werkzeug format [DD/MMM/YYYY HH:MM:SS]
# Pattern matches Werkzeug timestamp format: [02/Jan/2026 04:38:13]
pattern = r'\[(\d{2}/\w{3}/\d{4} \d{2}:\d{2}:\d{2})\]'
if re.search(pattern, original_msg):
# Replace with timezone-aware timestamp
formatted_msg = re.sub(pattern, f'[{timestamp}]', original_msg)
record.msg = formatted_msg
record.args = ()
return super().format(record)
werkzeug_logger = logging.getLogger("werkzeug")
werkzeug_logger.setLevel(getattr(logging, log_level.upper(), logging.INFO))
# Remove existing handlers to avoid duplicate logs
werkzeug_logger.handlers = []
# Add our timezone-aware handler with custom formatter
werkzeug_handler = logging.StreamHandler()
werkzeug_formatter = WerkzeugTimezoneFormatter(timezone)
werkzeug_handler.setFormatter(werkzeug_formatter)
werkzeug_logger.addHandler(werkzeug_handler)
# Prevent propagation to root logger to avoid duplicate logs
werkzeug_logger.propagate = False
return logging.getLogger(__name__)
def initialize_components(cfg: dict) -> None:
"""
Initialize all application components.
Args:
cfg: Configuration dictionary
Raises:
ConfigurationError: If configuration is invalid
"""
global gitlab_client, analyzer, email_sender, monitor, analysis_cache
# Initialize GitLab client
gitlab_config = cfg["gitlab"]
gitlab_client = GitLabClient(
url=gitlab_config["url"],
token=gitlab_config["token"],
project_id=gitlab_config.get(
"project_id"
), # Optional - None uses global endpoint
timeout=30,
max_retries=cfg["app"].get("max_retries", 3),
retry_backoff=cfg["app"].get("retry_backoff", 2.0),
)
# Initialize analyzer
ai_config = cfg["ai"]
analyzer = IssueAnalyzer(
provider=ai_config["provider"],
api_key=ai_config["api_key"],
model=ai_config["model"],
base_url=ai_config.get("base_url"),
temperature=ai_config.get("temperature", 0.7),
max_tokens=ai_config.get("max_tokens", 2000),
timeout=120,
max_retries=cfg["app"].get("max_retries", 3),
retry_backoff=cfg["app"].get("retry_backoff", 2.0),
enable_reasoning=ai_config.get("enable_reasoning", False),
)
# Initialize email sender
smtp_config = cfg["smtp"]
email_sender = EmailSender(
host=smtp_config["host"],
port=smtp_config["port"],
username=smtp_config["username"],
password=smtp_config["password"],
from_email=smtp_config["from_email"],
use_tls=smtp_config.get("use_tls", True),
use_ssl=smtp_config.get("use_ssl", False),
timeout=30,
max_retries=cfg["app"].get("max_retries", 3),
retry_backoff=cfg["app"].get("retry_backoff", 2.0),
)
# Initialize monitor
filter_labels = cfg.get("gitlab", {}).get("issue_filter", {}).get("labels")
monitor = IssueMonitor(
gitlab_client=gitlab_client,
filter_labels=filter_labels,
processed_issues_file=cfg["app"].get("processed_issues_file"),
)
# Initialize analysis cache
cache_file = cfg["app"].get("analysis_cache_file", "data/analysis_cache.json")
analysis_cache = AnalysisCache(cache_file=cache_file)
def process_issue(
issue_iid: int,
project_id: Optional[int] = None,
skip_cache: bool = False,
to_email: Optional[Union[str, List[str]]] = None,
) -> bool:
"""
Process a single issue: fetch, analyze, and send email.
Args:
issue_iid: Internal Issue ID
project_id: Optional project ID from issue data (required when using global endpoint)
skip_cache: If True, skip cached analysis and re-analyze the issue
to_email: Optional custom recipient email(s). If None, uses default from config.
Returns:
True if processed successfully, False otherwise
"""
try:
logger.info(f"👉 Processing issue #{issue_iid}")
if dry_run:
return True
# Fetch basic issue data first to get issue_id for cache check
# Use get_issue (lighter) instead of fetch_comprehensive_issue_data
logger.info(f"👉 Fetching basic data for issue #{issue_iid}")
basic_issue_data = gitlab_client.get_issue(issue_iid, project_id=project_id)
issue_id = basic_issue_data.get("id")
logger.info(f"✅ Fetched basic data for issue #{issue_iid} successfully")
# Check cache first (unless skip_cache is True) - avoid comprehensive fetch if cached
cached_data = None
if not skip_cache and issue_id and analysis_cache:
cached_data = analysis_cache.get_issue_with_report(issue_id, issue_iid)
if cached_data:
logger.info(
f"👉 Using cached analysis for issue #{issue_iid} (ID: {issue_id})"
)
analysis = cached_data.get("analysis", {})
report = cached_data.get("email_report", {})
issue_data = {**basic_issue_data, **cached_data.get("issue_data", {})}
else:
if skip_cache:
logger.info(
f"👉 Skipping cache and re-analyzing issue #{issue_iid} (ID: {issue_id})"
)
# Fetch comprehensive issue data (comments, attachments, etc.) for analysis
logger.info(f"👉 Fetching comprehensive data for issue #{issue_iid}")
comprehensive_data = gitlab_client.fetch_comprehensive_issue_data(
issue_iid, project_id=project_id
)
# Merge with basic data
issue_data = {**basic_issue_data, **comprehensive_data}
logger.info(f"✅ Fetched comprehensive data for issue #{issue_iid} successfully")
# Analyze issue
logger.info(f"👉 Analyzing issue #{issue_iid}...")
gitlab_url = config.get("gitlab", {}).get("url") if config else None
try:
analysis = analyzer.analyze_issue(issue_data, gitlab_url=gitlab_url)
except AnalysisError as e:
error_message = str(e)
if issue_id and analysis_cache:
analysis_cache.set(
issue_id,
{},
issue_iid,
issue_data,
email_report=None,
error=error_message,
)
raise
subject_prefix = config["smtp"].get("subject_prefix", "[GitLab Issue Analysis]")
report = generate_email_report(issue_data, analysis, subject_prefix)
# Cache analysis and email report together (only once, after both are ready)
if issue_id and analysis_cache:
analysis_cache.set(
issue_id, analysis, issue_iid, issue_data, email_report=report, error=None
)
email_recipients = to_email if to_email is not None else config["smtp"]["to_email"]
if isinstance(email_recipients, str):
email_recipients = [email_recipients]
try:
logger.info(f"👉 Sending email for issue #{issue_iid} to {email_recipients}")
email_sender.send_email(
to=email_recipients,
subject=report["subject"],
body=report["text"],
html_body=report["html"],
)
logger.info(f"✅ Email sent successfully for issue #{issue_iid}")
except EmailError as e:
logger.warning(
f"Email failed for issue #{issue_iid}: {e}. Issue will still be marked as processed."
)
# Mark as processed (even if email failed)
if issue_id:
monitor.mark_as_processed(issue_id)
logger.info(f"🎯 Processed the issue #{issue_iid} successful")
return True
except GitLabAPIError as e:
logger.error(f"❌ GitLab API error processing issue #{issue_iid}: {e}")
return False
except AnalysisError as e:
logger.error(f"❌ Analysis error processing issue #{issue_iid}: {e}")
return False
except Exception as e:
logger.error(
f"❌ Unexpected error processing issue #{issue_iid}: {e}", exc_info=True
)
return False
def process_issue_from_data(
issue: Dict[str, Any], project_id: Optional[int] = None, skip_cache: bool = False
) -> bool:
"""
Process an issue from issue data: fetch details, analyze, and send email.
This function uses the issue data from polling/webhook to avoid re-fetching
the basic issue information. The issue data already contains project_id from
the global API endpoint response (which returns issues from multiple projects).
Args:
issue: Issue dictionary from API response (must include 'iid' and 'project_id')
project_id: Optional project ID (extracted from issue if not provided)
skip_cache: If True, skip cached analysis and re-analyze the issue
Returns:
True if processed successfully, False otherwise
"""
issue_iid = issue.get("iid")
if not issue_iid:
logger.error("❌ Issue data missing 'iid' field")
return False
# Extract project_id from issue data if not provided
# This is required because the global endpoint returns issues from multiple projects
if not project_id:
project_id = issue.get("project_id")
if not project_id:
logger.error(
f"❌ Cannot process issue #{issue_iid}: missing project_id in issue data"
)
return False
try:
issue_id = issue.get("id")
if not skip_cache and issue_id and analysis_cache:
cached_data = analysis_cache.get_issue_with_report(issue_id, issue_iid)
if cached_data:
return True
logger.info(f"👉 Processing issue #{issue_iid} from project {project_id}")
if dry_run:
return True
if skip_cache:
logger.info(
f"👉 Skipping cache and re-analyzing issue #{issue_iid} (ID: {issue_id})"
)
logger.info(f"👉 Fetching comprehensive data for issue #{issue_iid}")
comprehensive_data = gitlab_client.fetch_comprehensive_issue_data(
issue_iid, project_id=project_id
)
issue_data = {**issue, **comprehensive_data}
logger.info(f"✅ Fetched comprehensive data for issue #{issue_iid} successfully")
logger.info(f"👉 Analyzing issue #{issue_iid}...")
gitlab_url = config.get("gitlab", {}).get("url") if config else None
try:
analysis = analyzer.analyze_issue(issue_data, gitlab_url=gitlab_url)
except AnalysisError as e:
error_message = str(e)
if issue_id and analysis_cache:
analysis_cache.set(
issue_id,
{},
issue_iid,
issue_data,
email_report=None,
error=error_message,
)
raise
subject_prefix = config["smtp"].get("subject_prefix", "[GitLab Issue Analysis]")
report = generate_email_report(issue_data, analysis, subject_prefix)
if issue_id and analysis_cache:
analysis_cache.set(
issue_id, analysis, issue_iid, issue_data, email_report=report, error=None
)
to_email = config["smtp"]["to_email"]
if isinstance(to_email, str):
to_email = [to_email]
try:
logger.info(f"👉 Sending email for issue #{issue_iid} to {to_email}")
email_sender.send_email(
to=to_email,
subject=report["subject"],
body=report["text"],
html_body=report["html"],
)
logger.info(f"✅ Email sent successfully for issue #{issue_iid}")
except EmailError as e:
logger.warning(
f"Email failed for issue #{issue_iid}: {e}. Issue will still be marked as processed."
)
if issue_id:
monitor.mark_as_processed(issue_id)
logger.info(f"🎯 Processed the issue #{issue_iid} successful")
return True
except GitLabAPIError as e:
logger.error(f"❌ GitLab API error processing issue #{issue_iid}: {e}")
return False
except AnalysisError as e:
logger.error(f"❌ Analysis error processing issue #{issue_iid}: {e}")
return False
except Exception as e:
logger.error(
f"❌ Unexpected error processing issue #{issue_iid}: {e}", exc_info=True
)
return False
def run_flask_server(host: str = "0.0.0.0", port: int = 8000) -> None:
"""
Run Flask server in a separate thread.
Args:
host: Host to bind to
port: Port to bind to
"""
logger.info(f"👉 Starting Flask server (host: {host}, port: {port})")
try:
app.run(host=host, port=port, debug=False, use_reloader=False)
logger.info(f"✅ Flask server started successfully at http://{host}:{port}/")
except Exception as e:
logger.error(f"❌ Error in Flask server: {e}", exc_info=True)
def run_polling_mode(poll_interval: int) -> None:
"""
Run application in polling mode.
Args:
poll_interval: Polling interval in seconds
"""
# Start Flask server in background thread for dashboard access during polling
flask_host = config.get("app", {}).get("webhook_host", "0.0.0.0")
flask_port = config.get("app", {}).get("webhook_port", 8000)
flask_thread = Thread(
target=run_flask_server,
args=(flask_host, flask_port),
daemon=True,
name="FlaskServer",
)
flask_thread.start()
logger.info(f"✅ Website available at http://{flask_host}:{flask_port}/")
while not shutdown_event.is_set():
try:
gitlab_config = config.get("gitlab", {})
issue_filter = gitlab_config.get("issue_filter", {})
scope = issue_filter.get("scope")
labels = issue_filter.get("labels")
# Get start time from ENV or default to current time
issue_start_time = config.get("app", {}).get("issue_start_time")
if issue_start_time:
# Normalize to ISO 8601 format for GitLab API
normalized_time = issue_start_time.strip().replace(" ", "T", 1)
if not (normalized_time.endswith("Z") or ("+" in normalized_time[-6:] or "-" in normalized_time[-6:])):
normalized_time += "Z"
created_after = normalized_time
else:
created_after = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
# Format timestamp for display in configured timezone
formatted_time = created_after
try:
time_str = created_after.replace("Z", "+00:00")
dt_utc = datetime.fromisoformat(time_str)
timezone_str = config.get("app", {}).get("timezone", "Asia/Ho_Chi_Minh")
tz = pytz.timezone(timezone_str)
if dt_utc.tzinfo is None:
dt_utc = pytz.UTC.localize(dt_utc)
dt_local = dt_utc.astimezone(tz)
formatted_time = dt_local.strftime("%Y-%m-%d %H:%M:%S")
except Exception as e:
logger.debug(f"Failed to format timestamp: {e}")
logger.info(f"👉 Start time to filter issues: {formatted_time}")
new_issues = monitor.poll_issues(
state="opened",
created_after=created_after,
scope=scope,
labels=labels,
)
gitlab_url = config.get("gitlab", {}).get("url", "")
project_ids = config.get("app", {}).get("project_ids")
if project_ids:
logger.info(
f"✅ Fetched {len(new_issues)} issue(s) from {gitlab_url} by project IDs: {project_ids}"
)
else:
logger.info(f"✅ Fetched {len(new_issues)} issue(s) from {gitlab_url}")
# Filter by project IDs if configured (automation mode only)
if project_ids:
new_issues = [
issue
for issue in new_issues
if issue.get("project_id") in project_ids
]
if new_issues:
# Filter out already cached issues to avoid reprocessing
issues_to_analyze = []
cached_issues = []
for issue in new_issues:
issue_id = issue.get("id")
issue_iid = issue.get("iid")
if issue_id and analysis_cache:
cached_data = analysis_cache.get_issue_with_report(issue_id, issue_iid)
if cached_data:
cached_issues.append(issue)
continue
issues_to_analyze.append(issue)
# Apply limit for testing mode
max_issues = config["app"].get("max_issues_per_poll")
original_count = len(issues_to_analyze)
if max_issues is not None and max_issues > 0:
issues_to_analyze = issues_to_analyze[:max_issues]
if len(issues_to_analyze) < original_count:
logger.info(f"📊 Limited to {len(issues_to_analyze)} issue(s) due to MAX_ISSUES_PER_POLL={max_issues}")
# Log new issues found (not cached)
if issues_to_analyze:
issue_list = [f"#{issue.get('iid', '?')} (PID: {issue.get('project_id', '?')})" for issue in issues_to_analyze]
logger.info(f"✅ Found {len(issues_to_analyze)} new issue(s): {', '.join(issue_list)}")
else:
logger.info("👉 No new issues")
# Update new_issues to only include issues to analyze
new_issues = issues_to_analyze
processed_count = 0
for issue in new_issues:
if shutdown_event.is_set():
break
# Double-check limit as safety measure
max_issues = config["app"].get("max_issues_per_poll")
if (
max_issues is not None
and max_issues > 0
and processed_count >= max_issues
):
break
issue_iid = issue.get("iid")
# Extract project_id from issue data (required for fetching details)
project_id = issue.get("project_id")
if issue_iid:
# Pass the full issue data to avoid re-fetching
if process_issue_from_data(issue, project_id=project_id):
processed_count += 1
else:
logger.warning(f"⚠️ Skipping issue without IID: {issue}")
else:
logger.info("👉 No new issues")
# Wait for next poll (with interruptible sleep)
if not shutdown_event.wait(poll_interval):
continue
else:
break
except KeyboardInterrupt:
shutdown_event.set()
break
except Exception as e:
logger.error(f"❌ Error in polling loop: {e}", exc_info=True)
# Continue polling even if there's an error
if not shutdown_event.wait(min(poll_interval, 60)):
continue
else:
break
@app.route("/webhook", methods=["POST"])
def webhook_handler():
"""
Handle GitLab webhook requests.
Returns:
JSON response
"""
try:
# Check if automation is enabled
enable_automation = (
config.get("app", {}).get("enable_automation", True) if config else True
)
if not enable_automation:
return (
jsonify(
{
"message": "Automation is disabled. Use the dashboard to manually trigger analysis."
}
),
200,
)
# Validate request has JSON payload
if not request.is_json:
return jsonify({"error": "Content-Type must be application/json"}), 400
payload = request.get_json()
if not payload:
return jsonify({"error": "Invalid or empty JSON payload"}), 400
# Get webhook secret from header
header_token = request.headers.get("X-Gitlab-Token")
webhook_secret = config["gitlab"].get("webhook_secret")
# Validate webhook (check gitlab_client is initialized)
if webhook_secret:
if not gitlab_client or not gitlab_client.validate_webhook_secret(
payload, webhook_secret, header_token
):
logger.warning("⚠️ Invalid webhook secret")
return jsonify({"error": "Invalid webhook secret"}), 401
# Process webhook
issue_data = monitor.process_webhook(payload)
if not issue_data:
logger.info("ℹ️ Issue already processed or filtered, skipping")
return jsonify({"message": "Issue already processed or filtered"}), 200
issue_iid = issue_data.get("iid")
if not issue_iid:
logger.warning("⚠️ No issue IID in webhook payload")
return jsonify({"error": "Invalid webhook payload"}), 400
# Extract project_id from issue data if available
project_id = issue_data.get("project_id")
# Process issue using the webhook data (synchronously for now)
# In production, you might want to use a queue/background task
success = process_issue_from_data(issue_data, project_id=project_id)
if success:
return jsonify({"message": "Issue processed successfully"}), 200
else:
return jsonify({"message": "Issue processing failed"}), 500
except ValueError as e:
logger.error(f"❌ Invalid webhook payload: {e}")
return jsonify({"error": str(e)}), 400
except Exception as e:
logger.error(f"❌ Error processing webhook: {e}", exc_info=True)
return jsonify({"error": "Internal server error"}), 500
@app.route("/", methods=["GET"])
@app.route("/dashboard", methods=["GET"])
def dashboard():
"""
Dashboard UI endpoint.
Returns:
Rendered dashboard HTML page
"""
app_version = config.get("app", {}).get("version", "0.1.0") if config else "0.1.0"
return render_template("dashboard.html", version=app_version)
@app.route("/issues", methods=["GET"])
def issues_list():
"""
List all processed issues UI.
Returns:
Rendered issues list HTML page
"""
app_version = config.get("app", {}).get("version", "0.1.0") if config else "0.1.0"
return render_template("issues_list.html", version=app_version, label_colors=LABEL_COLORS)
@app.route("/issues/<int:issue_id>", methods=["GET"])
def issue_view(issue_id: int):
"""
View issue details UI.
Args:
issue_id: GitLab issue ID
Returns:
Rendered issue view HTML page
"""
app_version = config.get("app", {}).get("version", "0.1.0") if config else "0.1.0"
return render_template("issue_view.html", issue_id=issue_id, version=app_version)
@app.route("/api/stats", methods=["GET"])
def get_stats():
"""
Get application statistics.
Returns:
JSON response with statistics
"""
try:
# Get processed count from analysis cache (more accurate and persistent)
processed_count = 0
if analysis_cache:
processed_count = analysis_cache.get_processed_count()
elif monitor:
# Fallback to monitor's processed issues count
processed_count = len(monitor.processed_issues)
app_mode = config.get("app", {}).get("mode", "unknown") if config else "unknown"
gitlab_url = config.get("gitlab", {}).get("url", "") if config else ""
enable_automation = (
config.get("app", {}).get("enable_automation", True) if config else True
)
# Get main configuration
ai_provider = (
config.get("ai", {}).get("provider", "unknown") if config else "unknown"
)
ai_model = config.get("ai", {}).get("model", "unknown") if config else "unknown"
ai_reasoning = (
config.get("ai", {}).get("enable_reasoning", False) if config else False
)
environment = (
config.get("app", {}).get("environment", "unknown") if config else "unknown"
)
poll_interval = (
config.get("app", {}).get("poll_interval", 900) if config else 900
)
max_issues_per_poll = (
config.get("app", {}).get("max_issues_per_poll") if config else None
)
gitlab_labels = (
config.get("gitlab", {}).get("issue_filter", {}).get("labels")
if config
else None
)
return (
jsonify(
{
"processed_count": processed_count,
"app_mode": app_mode,
"gitlab_url": gitlab_url,
"automation_enabled": enable_automation,
"config": {
"gitlab_labels": gitlab_labels,
"gitlab_url": gitlab_url,
"ai_provider": ai_provider,
"ai_model": ai_model,
"ai_reasoning": ai_reasoning,
"environment": environment,
"poll_interval": poll_interval,
"max_issues_per_poll": max_issues_per_poll,
},
}
),
200,
)
except Exception as e:
logger.error(f"❌ Error getting stats: {e}", exc_info=True)
return jsonify({"error": "Failed to get statistics"}), 500
@app.route("/api/trigger", methods=["POST"])
def trigger_analysis():
"""
Manually trigger issue analysis.
Request body:
{
"project_id": int,
"issue_iid": int,
"to_email": str or list[str] (optional) - Custom recipient email(s)
}
Returns:
JSON response with result
"""
try:
if not request.json:
return jsonify({"error": "Request body is required"}), 400
project_id = request.json.get("project_id")
issue_iid = request.json.get("issue_iid")
to_email = request.json.get("to_email") # Optional custom recipient email(s)
if not project_id or not issue_iid:
return (
jsonify({"error": "Both 'project_id' and 'issue_iid' are required"}),
400,
)
# Validate components are initialized
if not gitlab_client or not analyzer or not email_sender or not monitor:
return jsonify({"error": "Application components not initialized"}), 503
logger.info(
f"🎯 Manual trigger process issue #{issue_iid} from project {project_id} (skipping cache)"
)
if to_email:
logger.info(f"📧 Using custom recipient email(s): {to_email}")
# Process the issue (skip cache for manual triggers to always get fresh analysis)
try:
success = process_issue(
issue_iid, project_id=project_id, skip_cache=True, to_email=to_email
)
if success:
return (
jsonify(
{
"success": True,
"message": f"Issue #{issue_iid} from project {project_id} processed successfully",
}
),
200,
)
else:
# Check if there's an error stored in cache for this issue
error_message = None
if analysis_cache:
# Try to get the issue from cache to see if there's an error
issue_data = None
try:
# Fetch issue data to get issue_id
issue_data = gitlab_client.fetch_comprehensive_issue_data(
issue_iid, project_id=project_id
)
issue_id = issue_data.get("id") if issue_data else None
if issue_id:
cached_data = analysis_cache.get_issue_with_report(issue_id, issue_iid)
if cached_data and cached_data.get("error"):
error_message = cached_data.get("error")
except Exception:
pass # Ignore errors when fetching issue data for error retrieval
return (
jsonify(
{
"success": False,
"error": error_message or f"Failed to process issue #{issue_iid} from project {project_id}",
}
),
500,
)
except AnalysisError as e:
# AnalysisError was raised, store it and return the error message
error_message = str(e)
# Try to store error in cache if we have issue data
try:
issue_data = gitlab_client.fetch_comprehensive_issue_data(
issue_iid, project_id=project_id
)
issue_id = issue_data.get("id") if issue_data else None
if issue_id and analysis_cache:
analysis_cache.set(
issue_id,
{}, # Empty analysis
issue_iid,
issue_data,
email_report=None,
error=error_message,
)
except Exception:
pass # Ignore errors when storing error in cache
return (
jsonify(
{
"success": False,
"error": error_message,
}
),
500,
)
except ValueError as e:
logger.error(f"❌ Invalid request: {e}")
return jsonify({"error": str(e)}), 400
except Exception as e:
logger.error(f"❌ Error triggering analysis: {e}", exc_info=True)
return jsonify({"error": "Internal server error"}), 500
@app.route("/api/issues", methods=["GET"])
def list_processed_issues():
"""
List all processed issues with latest state from GitLab.
Fetches the current state of each issue from GitLab and updates the cache.
Returns:
JSON response with list of processed issues with updated states
"""
try:
if not analysis_cache:
return jsonify({"error": "Analysis cache not initialized"}), 503
if not gitlab_client:
return jsonify({"error": "GitLab client not initialized"}), 503
# Get all cached issues
issues = analysis_cache.get_all_issues()
# Update states from GitLab for each issue
updated_count = 0
for issue in issues:
issue_iid = issue.get("issue_iid")
issue_id = issue.get("issue_id")
project_id = issue.get("project_id")
# Skip if we don't have required info to fetch from GitLab
if not issue_iid or not project_id:
continue
try:
# Fetch latest issue data from GitLab
latest_issue = gitlab_client.get_issue(issue_iid, project_id=project_id)
latest_state = latest_issue.get("state")
latest_updated_at = latest_issue.get("updated_at")
# Update cache if state changed
if latest_state and latest_state != issue.get("state"):
analysis_cache.update_issue_state(
issue_id, latest_state, latest_updated_at
)
# Update the issue in our list
issue["state"] = latest_state
if latest_updated_at:
issue["updated_at"] = latest_updated_at
updated_count += 1
elif latest_state:
# State is the same, but update updated_at if available
if latest_updated_at and latest_updated_at != issue.get("updated_at"):
analysis_cache.update_issue_state(
issue_id, latest_state, latest_updated_at
)
issue["updated_at"] = latest_updated_at
except GitLabAPIError as e:
# Log error but continue with other issues
logger.warning(
f"⚠️ Failed to fetch latest state for issue #{issue_iid} (ID: {issue_id}): {e}"
)
continue
except Exception as e:
# Log unexpected errors but continue
logger.warning(
f"⚠️ Unexpected error fetching state for issue #{issue_iid} (ID: {issue_id}): {e}"
)
continue
if updated_count > 0: