-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1293 lines (1071 loc) · 48.1 KB
/
main.py
File metadata and controls
1293 lines (1071 loc) · 48.1 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
"""
Web interface for the Code Sorcerer tool.
"""
import os
import logging
from pathlib import Path
import tempfile
import json
import threading
import time
import uuid
from enum import Enum, auto
from dataclasses import dataclass, field
from typing import Dict, List, Tuple, Any, Optional
from flask import Flask, render_template, request, redirect, url_for, flash, session, jsonify, send_file
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
from werkzeug.middleware.proxy_fix import ProxyFix
from werkzeug.utils import secure_filename
# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Initialize Flask app
app = Flask(__name__)
app.secret_key = os.environ.get("SESSION_SECRET", "dev-secret-key")
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
# Create templates directory if it doesn't exist
os.makedirs('templates', exist_ok=True)
os.makedirs('static', exist_ok=True)
os.makedirs('static/css', exist_ok=True)
os.makedirs('static/js', exist_ok=True)
# Setup database
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL", "sqlite:///audit_near.db")
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
db.init_app(app)
# Define models
class AuditReport(db.Model):
id = db.Column(db.Integer, primary_key=True)
repo_name = db.Column(db.String(200), nullable=False)
repo_path = db.Column(db.String(500), nullable=False)
branch = db.Column(db.String(100), default="main")
total_score = db.Column(db.Float, nullable=False)
total_possible = db.Column(db.Float, nullable=False)
report_data = db.Column(db.Text, nullable=False) # JSON string
repo_metadata = db.Column(db.Text, nullable=True) # JSON string for repository metadata
created_at = db.Column(db.DateTime, server_default=db.func.now())
# Create tables
with app.app_context():
# Recreate tables (this will drop and recreate tables if schema has changed)
db.drop_all()
db.create_all()
logger.info("Database tables recreated")
# Import audit functionality
from audit_near.cli import load_config, get_category_handlers
from audit_near.ai_client import AiClient
from audit_near.providers.repo_provider import RepoProvider
from audit_near.providers.repo_analyzer import RepoAnalyzer
from audit_near.providers.github_provider import (
is_github_url, download_github_repo, extract_repo_name_from_url,
get_repository_branches, get_repo_metadata
)
from audit_near.reporters.markdown_reporter import MarkdownReporter
@app.route('/')
def index():
"""Home page."""
# Get recent audits
with app.app_context():
recent_audits = AuditReport.query.order_by(AuditReport.created_at.desc()).limit(5).all()
return render_template('index.html', recent_audits=recent_audits)
@app.route('/audit', methods=['GET', 'POST'])
def audit():
"""Start a new audit."""
# Import the registry and plugin loader
from audit_near.plugins.registry import registry
from audit_near.plugins.management import init_plugins_directory, discover_plugins
from pathlib import Path
# Python 3.11+ includes tomllib in the standard library
try:
import tomllib # Python 3.11+
except ImportError:
import tomli as tomllib # Before Python 3.11
# Initialize plugins directory and load plugins
init_plugins_directory()
available_plugins = discover_plugins()
logger.info(f"Loaded {len(available_plugins)} plugins: {', '.join(available_plugins)}")
# Get available categories from registry
available_categories = []
for category_id in registry.get_all_category_ids():
metadata = registry.get_metadata(category_id)
if metadata:
# Check for max_points in config section (where it's stored in TOML files)
config = metadata.get("config", {})
max_points = config.get("max_points", 10)
logger.info(f"Category {category_id} max points: {max_points}")
available_categories.append({
"id": category_id,
"name": metadata.get("name", category_id),
"description": metadata.get("description", ""),
"max_points": max_points
})
# Get available bundles
bundles_dir = Path("plugins/bundles")
available_bundles = []
if bundles_dir.exists():
for bundle_file in bundles_dir.glob("*.toml"):
try:
with open(bundle_file, "rb") as f:
bundle_data = tomllib.load(f)
if "metadata" in bundle_data:
# Filter categories to include only those set to true
bundle_categories = bundle_data.get("categories", {})
# Log the categories from the bundle
logger.info(f"Bundle {bundle_file.stem} categories: {bundle_categories}")
enabled_categories = [key for key, value in bundle_categories.items() if value is True]
logger.info(f"Bundle {bundle_file.stem} enabled categories: {enabled_categories}")
available_bundles.append({
"id": bundle_file.stem,
"name": bundle_data["metadata"].get("name", bundle_file.stem),
"description": bundle_data["metadata"].get("description", ""),
"categories": enabled_categories
})
except Exception as e:
logger.error(f"Error loading bundle {bundle_file}: {e}")
if request.method == 'POST':
# Handle form submission
repo_path = request.form.get('repo_path')
if not repo_path:
flash('Repository path or GitHub URL is required', 'error')
return redirect(url_for('audit'))
# Check if the input is a GitHub URL
if is_github_url(repo_path):
try:
# Get branch name from form
branch = request.form.get('branch', 'main')
# Extract repo name for display purposes before downloading
repo_name = extract_repo_name_from_url(repo_path)
# Download the repository
logger.info(f"Downloading GitHub repository: {repo_path}, branch: {branch}")
# For actual auditing, we only need the specified branch
temp_repo_path = download_github_repo(repo_path, branch, fetch_all_branches=False)
# Set session variables
session['is_github_repo'] = True
session['github_url'] = repo_path
session['repo_name'] = repo_name # Important: store the actual repo name
session['original_repo_name'] = repo_name # Keep the original name
repo_path = temp_repo_path
logger.info(f"GitHub repository '{repo_name}' downloaded to: {repo_path}")
flash(f"GitHub repository '{repo_name}' downloaded successfully.", 'success')
except Exception as e:
logger.error(f"Error downloading GitHub repository: {e}")
flash(f"Error downloading GitHub repository: {str(e)}", 'error')
return redirect(url_for('audit'))
else:
# Handle local repository path
session['is_github_repo'] = False
# Expand path if it contains a tilde
if '~' in repo_path:
repo_path = os.path.expanduser(repo_path)
# Convert to absolute path if it's relative
if not os.path.isabs(repo_path):
repo_path = os.path.abspath(repo_path)
# Use the repository name as the repo_name
session['repo_name'] = os.path.basename(repo_path)
# Enhanced validation of repository path
validation_result, validation_message = validate_repository_path(repo_path)
if not validation_result:
flash(validation_message, 'error')
return redirect(url_for('audit'))
# Store form data in session
session['repo_path'] = repo_path
session['branch'] = request.form.get('branch', 'main')
# Process category selection
selected_bundle = request.form.get('bundle')
selected_categories = request.form.getlist('categories')
# Store audit configuration in session
session['audit_config'] = {
'categories': {},
'bundle': selected_bundle
}
# If a bundle was selected, use its categories
if selected_bundle:
bundle_path = bundles_dir / f"{selected_bundle}.toml"
if bundle_path.exists():
try:
with open(bundle_path, "rb") as f:
bundle_data = tomllib.load(f)
# Add all enabled categories from the bundle
if "categories" in bundle_data:
for category_id, enabled in bundle_data["categories"].items():
if enabled:
metadata = registry.get_metadata(category_id)
max_points = 10
if metadata:
# Get max_points from config section
config = metadata.get("config", {})
max_points = config.get("max_points", 10)
logger.info(f"Bundle category {category_id} max points: {max_points}")
else:
# Default points for standard categories
if category_id == "code_quality":
max_points = 20
elif category_id in ["functionality", "security", "documentation"]:
max_points = 15
else:
max_points = 10
session['audit_config']['categories'][category_id] = {
'max_points': max_points
}
except Exception as e:
logger.error(f"Error loading bundle {bundle_path}: {e}")
else:
# Use individually selected categories
for category_id in selected_categories:
metadata = registry.get_metadata(category_id)
max_points = 10
if metadata:
# Get max_points from config section
config = metadata.get("config", {})
max_points = config.get("max_points", 10)
logger.info(f"Selected category {category_id} max points: {max_points}")
else:
# Default points for standard categories
if category_id == "code_quality":
max_points = 20
elif category_id in ["functionality", "security", "documentation"]:
max_points = 15
else:
max_points = 10
session['audit_config']['categories'][category_id] = {
'max_points': max_points
}
# If no categories were selected or found in bundle, use defaults
if not session['audit_config']['categories']:
session['audit_config']['categories'] = {
"code_quality": {"max_points": 20},
"functionality": {"max_points": 15},
"security": {"max_points": 20},
"documentation": {"max_points": 15},
"innovation": {"max_points": 10},
"ux_design": {"max_points": 10},
"blockchain_integration": {"max_points": 10}
}
# Add debug info to session
repo_stats = get_repository_stats(repo_path)
session['repo_stats'] = repo_stats
logger.info(f"Repository validated: {repo_path} with {repo_stats['total_files']} files")
logger.info(f"Selected categories: {list(session['audit_config']['categories'].keys())}")
return redirect(url_for('run_audit'))
# Get list of sample test repositories for the dropdown
test_repos_dir = os.path.join(os.path.dirname(__file__), 'test_repos')
sample_repos = []
if os.path.isdir(test_repos_dir):
sample_repos = [os.path.join(test_repos_dir, d) for d in os.listdir(test_repos_dir)
if os.path.isdir(os.path.join(test_repos_dir, d))]
# Log the bundles that will be rendered in the template
for bundle in available_bundles:
logger.info(f"Rendering bundle: {bundle['id']} with categories: {bundle['categories']}")
return render_template('audit_form.html',
sample_repos=sample_repos,
available_categories=available_categories,
available_bundles=available_bundles,
plugin_count=len(available_plugins))
def validate_repository_path(repo_path):
"""
Validate that the given path is a valid git repository with source code files.
Args:
repo_path: Path to the repository
Returns:
Tuple of (is_valid, message)
"""
# Check if directory exists
if not os.path.isdir(repo_path):
return False, f"Directory does not exist: {repo_path}"
# Check if it has any files
files = []
for root, _, filenames in os.walk(repo_path):
for filename in filenames:
# Skip hidden files and directories
if filename.startswith('.') or '/.git/' in root:
continue
file_path = os.path.join(root, filename)
files.append(file_path)
if not files:
return False, f"No files found in repository: {repo_path}"
# Check for source code files
code_extensions = ['.js', '.ts', '.py', '.rs', '.go', '.java', '.c', '.cpp', '.h', '.jsx', '.tsx', '.html', '.css']
code_files = [f for f in files if os.path.splitext(f)[1].lower() in code_extensions]
if not code_files:
return False, f"No source code files found in repository: {repo_path}"
return True, "Repository is valid"
def get_repository_stats(repo_path):
"""
Get statistics about the repository.
Args:
repo_path: Path to the repository
Returns:
Dictionary with repository statistics
"""
stats = {
'total_files': 0,
'code_files': 0,
'doc_files': 0,
'other_files': 0,
'file_types': {},
'largest_files': [],
'directories': []
}
code_extensions = ['.js', '.ts', '.py', '.rs', '.go', '.java', '.c', '.cpp', '.h', '.jsx', '.tsx', '.php', '.rb']
doc_extensions = ['.md', '.txt', '.rst', '.pdf', '.doc', '.docx']
all_files = []
for root, dirs, files in os.walk(repo_path):
# Skip hidden directories and .git
dirs[:] = [d for d in dirs if not d.startswith('.') and d != '.git']
# Add to directory list
rel_dir = os.path.relpath(root, repo_path)
if rel_dir != '.' and not rel_dir.startswith('.'):
stats['directories'].append(rel_dir)
for filename in files:
# Skip hidden files
if filename.startswith('.'):
continue
file_path = os.path.join(root, filename)
rel_path = os.path.relpath(file_path, repo_path)
ext = os.path.splitext(filename)[1].lower()
# Skip very large files
try:
size = os.path.getsize(file_path)
if size > 1000000: # 1MB
continue
# Aggregate statistics
stats['total_files'] += 1
# Categorize file types
if ext in code_extensions:
stats['code_files'] += 1
elif ext in doc_extensions:
stats['doc_files'] += 1
else:
stats['other_files'] += 1
# Count file types
if ext not in stats['file_types']:
stats['file_types'][ext] = 0
stats['file_types'][ext] += 1
# Track file info for largest files
all_files.append((rel_path, size, ext))
except (IOError, OSError):
# Skip files that can't be accessed
continue
# Get top 10 largest files
all_files.sort(key=lambda x: x[1], reverse=True)
stats['largest_files'] = [(path, size) for path, size, _ in all_files[:10]]
# Limit the number of directories shown
stats['directories'] = stats['directories'][:20]
return stats
@app.route('/validate-repository')
def validate_repository_endpoint():
"""API endpoint to validate a repository path."""
repo_path = request.args.get('path')
is_github_param = request.args.get('is_github', 'false').lower() == 'true'
# Added timeout to prevent worker process hanging
timeout_seconds = 30
if not repo_path:
return jsonify({
'valid': False,
'message': 'Repository path or GitHub URL is required'
})
# Check if it's a GitHub URL (either from parameter or by detecting URL)
if is_github_param or is_github_url(repo_path):
try:
# Extract repository name first (for display)
repo_name = extract_repo_name_from_url(repo_path)
# Actually download the repository to get stats
logger.info(f"Downloading GitHub repository for validation: {repo_path}")
branch = request.args.get('branch', 'main')
# For validation, we want to fetch all branches to show them in the dropdown
temp_repo_path = download_github_repo(repo_path, branch, fetch_all_branches=True)
# Get repository statistics from the downloaded repo
repo_stats = get_repository_stats(temp_repo_path)
# Get branch information from the repository
branches = get_repository_branches(temp_repo_path)
# Clean up the temporary repository - we'll download it again during the actual audit
try:
import shutil
shutil.rmtree(temp_repo_path)
logger.info(f"Cleaned up temporary repository: {temp_repo_path}")
except Exception as cleanup_error:
logger.error(f"Error cleaning up temp directory: {cleanup_error}")
response_data = {
'valid': True,
'is_github_url': True,
'message': f"Valid GitHub repository URL: {repo_path}",
'github_url': repo_path,
'repo_name': repo_name,
'stats': repo_stats,
'branches': branches
}
return jsonify(response_data)
except Exception as e:
logger.error(f"Error validating GitHub repository: {e}", exc_info=True)
return jsonify({
'valid': False,
'is_github_url': True,
'message': f"Error validating GitHub repository: {str(e)}"
})
# Local repository path validation
# Expand path if it contains a tilde
if '~' in repo_path:
repo_path = os.path.expanduser(repo_path)
# Convert to absolute path if it's relative
if not os.path.isabs(repo_path):
repo_path = os.path.abspath(repo_path)
# Validate repository
is_valid, message = validate_repository_path(repo_path)
if is_valid:
# Get repository stats
stats = get_repository_stats(repo_path)
# Get branch information for local repositories
branches = get_repository_branches(repo_path)
return jsonify({
'valid': True,
'is_github_url': False,
'message': message,
'stats': stats,
'branches': branches
})
else:
return jsonify({
'valid': False,
'is_github_url': False,
'message': message
})
@app.route('/repository-structure')
def repository_structure_endpoint():
"""API endpoint to get the directory structure of a repository."""
repo_path = request.args.get('path')
if not repo_path:
return jsonify({
'error': 'Repository path is required'
})
# Expand and convert path
if '~' in repo_path:
repo_path = os.path.expanduser(repo_path)
if not os.path.isabs(repo_path):
repo_path = os.path.abspath(repo_path)
# Check if path exists
if not os.path.isdir(repo_path):
return jsonify({
'error': f'Directory does not exist: {repo_path}'
})
# Get directories
try:
directories = []
for item in os.listdir(repo_path):
if os.path.isdir(os.path.join(repo_path, item)) and not item.startswith('.'):
directories.append(item)
return jsonify({
'directories': sorted(directories)
})
except Exception as e:
return jsonify({
'error': f'Error getting directory structure: {str(e)}'
})
@app.route('/directory-contents')
def directory_contents_endpoint():
"""API endpoint to get the contents of a directory."""
path = request.args.get('path', '/')
# For security, we'll limit this to just returning directories, not files
try:
if path == '/':
# For root, just return some common top-level directories
directories = ['home', 'var', 'opt', 'usr', 'tmp', 'mnt', 'etc']
else:
# For any other path, check if it exists first
if not os.path.isdir(path):
return jsonify({
'error': f'Directory does not exist: {path}'
})
# List directories only (not files)
directories = []
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path) and not item.startswith('.'):
directories.append(item)
return jsonify({
'directories': sorted(directories)
})
except Exception as e:
return jsonify({
'error': f'Error getting directory contents: {str(e)}'
})
# Progress tracking infrastructure
class AuditStep(Enum):
REPO_VALIDATION = auto()
FILE_GATHERING = auto()
CODE_ANALYSIS = auto()
REPORT_GENERATION = auto()
@dataclass
class CategoryProgress:
name: str
max_points: int
score: Optional[int] = None
completed: bool = False
@dataclass
class AuditProgress:
id: str
repo_path: str
branch: str
repo_name: Optional[str] = None # Store original repo name for GitHub repositories
steps: Dict[str, int] = field(default_factory=lambda: {
"repo_validation": 0,
"file_gathering": 0,
"code_analysis": 0,
"report_generation": 0
})
current_task: str = "Initializing..."
overall_percentage: int = 0
categories_pending: List[Dict] = field(default_factory=list)
categories_completed: List[Dict] = field(default_factory=list)
report_id: Optional[int] = None
error: Optional[str] = None
def update_step_progress(self, step: AuditStep, percentage: int, task_description: str = None):
"""Update progress for a specific step"""
step_name = step.name.lower()
step_key = {
"repo_validation": "repo_validation",
"file_gathering": "file_gathering",
"code_analysis": "code_analysis",
"report_generation": "report_generation"
}.get(step_name, step_name)
self.steps[step_key] = percentage
if task_description:
self.current_task = task_description
# Calculate overall percentage
total = sum(self.steps.values())
self.overall_percentage = min(100, total // len(self.steps))
def add_pending_category(self, name: str, max_points: int):
"""Add a category to the pending list"""
self.categories_pending.append({
"name": name,
"max_points": max_points,
"score": None
})
def complete_category(self, name: str, score: int):
"""Mark a category as completed"""
# Remove from pending
pending = [c for c in self.categories_pending if c["name"] != name]
# Find the max points
max_points = next((c["max_points"] for c in self.categories_pending if c["name"] == name), 10)
# Add to completed
self.categories_completed.append({
"name": name,
"max_points": max_points,
"score": score
})
# Update pending list
self.categories_pending = pending
def set_report_id(self, report_id: int):
"""Set the final report ID"""
self.report_id = report_id
self.overall_percentage = 100
self.current_task = "Audit complete! Generating final report..."
def set_error(self, error_message: str):
"""Set an error message"""
self.error = error_message
self.current_task = "Error encountered"
# Dictionary to store audit progress by ID
audit_progress_store = {}
@app.route('/debug-repository')
def debug_repository():
"""Debug repository files and structure."""
repo_path = session.get('repo_path')
if not repo_path:
flash('Repository information not found', 'error')
return redirect(url_for('audit'))
# Get repository statistics
repo_stats = get_repository_stats(repo_path)
# Get sample file contents
code_samples = []
extensions = ['.js', '.ts', '.py', '.rs', '.go', '.java', '.jsx', '.tsx']
count = 0
for root, _, files in os.walk(repo_path):
if count >= 5: # Limit to 5 code samples
break
for file in files:
if count >= 5:
break
ext = os.path.splitext(file)[1].lower()
if ext in extensions:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, repo_path)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read(1000) # First 1000 chars
code_samples.append({
'path': rel_path,
'content': content[:1000] + ('...' if len(content) > 1000 else '')
})
count += 1
except:
pass
return render_template(
'debug_repository.html',
repo_path=repo_path,
repo_stats=repo_stats,
code_samples=code_samples
)
@app.route('/audit-progress')
def audit_progress():
"""Show the audit progress page."""
# Get progress ID from session
progress_id = session.get('audit_progress_id')
if not progress_id or progress_id not in audit_progress_store:
flash('No audit in progress', 'error')
return redirect(url_for('audit'))
# Get progress data
progress = audit_progress_store[progress_id]
# If complete, redirect to report
if progress.report_id and progress.overall_percentage >= 100:
return redirect(url_for('view_report', report_id=progress.report_id))
return render_template('audit_progress.html', progress=progress)
@app.route('/check-audit-progress')
def check_audit_progress():
"""Check the current audit progress."""
# Get progress ID from session
progress_id = session.get('audit_progress_id')
if not progress_id or progress_id not in audit_progress_store:
# Instead of error redirection, show a progress page with initialization state
# This is for cases where JS might make API calls before the audit is fully registered
progress = AuditProgress(
id="initializing",
repo_path="",
branch="",
current_task="Initializing audit, please wait...",
overall_percentage=0
)
return render_template('audit_progress.html', progress=progress, initializing=True)
# Get progress data
progress = audit_progress_store[progress_id]
# If complete with a report ID, redirect to report
if progress.report_id and progress.overall_percentage >= 100:
return redirect(url_for('view_report', report_id=progress.report_id))
# Otherwise, return to progress page
return render_template('audit_progress.html', progress=progress)
@app.route('/api/audit-progress')
def api_audit_progress():
"""
API endpoint to check the current audit progress.
### IMPORTANT WARNING - RACE CONDITION HANDLING ###
This endpoint is designed to handle a critical race condition between
the initialization of an audit and the first AJAX call from the frontend.
DO NOT modify the behavior where this returns a 200 status with initialization data
instead of an error when the audit progress isn't found. This approach prevents
the "No audit in progress" error that occurs when the frontend JS makes its first
AJAX call before the audit job is fully registered in the server's memory.
Any changes to this endpoint should be thoroughly tested with new audits
to ensure the race condition isn't reintroduced.
"""
# Get progress ID from session
progress_id = session.get('audit_progress_id')
if not progress_id or progress_id not in audit_progress_store:
# Instead of returning an error, return a status indicating initialization
# This is critical for handling the race condition with the frontend
return jsonify({
"status": "initializing",
"message": "Audit is initializing, please wait...",
"overall_percentage": 0,
"current_task": "Initializing audit...",
"steps": {
"repo_validation": 0,
"file_gathering": 0,
"code_analysis": 0,
"report_generation": 0
},
"categories_completed": [],
"categories_pending": [],
"report_id": None,
"error": None
}), 200 # Return 200 OK, not 404 - this is intentional
# Get progress data
progress = audit_progress_store[progress_id]
# Convert progress data to JSON-serializable dict
progress_data = {
"status": "in_progress",
"overall_percentage": progress.overall_percentage,
"current_task": progress.current_task,
"steps": progress.steps,
"categories_completed": progress.categories_completed,
"categories_pending": progress.categories_pending,
"report_id": progress.report_id,
"error": progress.error
}
return jsonify(progress_data)
def run_audit_in_background(progress_id, repo_path, branch, config):
"""
Run the audit process in a background thread with progress updates.
Args:
progress_id: ID of the audit progress
repo_path: Path to the repository
branch: Branch name
config: Configuration dictionary
"""
progress = audit_progress_store[progress_id]
try:
# Initialize AI client
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
progress.set_error('OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.')
return
# Import required modules for plugin loading
try:
import tomllib # Python 3.11+
except ImportError:
import tomli as tomllib # Before Python 3.11
from audit_near.plugins.loader import loader
from audit_near.plugins.management import init_plugins_directory
from audit_near.plugins.registry import registry
# Initialize the plugins directory and load all plugins
plugins_dir = init_plugins_directory()
loaded_plugins = loader.load_plugins()
logger.info(f"Loaded {len(loaded_plugins)} plugins before audit: {', '.join(loaded_plugins)}")
# Log registry state
all_categories = registry.get_all_category_ids()
logger.info(f"Available categories in registry: {', '.join(all_categories)}")
# Check if UX Design and Blockchain Integration are in the configuration
logger.info(f"Selected categories for audit: {', '.join(config['categories'].keys())}")
# Update progress - Repo validation (10%)
progress.update_step_progress(
AuditStep.REPO_VALIDATION, 25,
"Validating repository and initializing systems..."
)
ai_client = AiClient(api_key=api_key, config=config)
# Initialize repo provider with branch
progress.update_step_progress(
AuditStep.REPO_VALIDATION, 50,
"Initializing repository provider..."
)
repo_provider = RepoProvider(repo_path=repo_path, branch=branch)
# Update progress - Repo validation complete
progress.update_step_progress(
AuditStep.REPO_VALIDATION, 100,
"Repository validated successfully!"
)
# Update progress - File gathering (start)
progress.update_step_progress(
AuditStep.FILE_GATHERING, 10,
"Collecting files from repository..."
)
# Get files from repo
files = list(repo_provider.get_files())
# Log detailed information about the files being processed
logger.info(f"Repository: {repo_path}, Branch: {branch}")
logger.info(f"Number of files retrieved: {len(files)}")
# Check if we have enough code files for analysis
code_extensions = ['.js', '.ts', '.py', '.rs', '.go', '.java', '.c', '.cpp', '.jsx', '.tsx']
code_files = [f for f in files if os.path.splitext(f[0])[1].lower() in code_extensions]
logger.info(f"Number of code files: {len(code_files)}")
# Update progress - File gathering (50%)
progress.update_step_progress(
AuditStep.FILE_GATHERING, 50,
f"Found {len(files)} files, including {len(code_files)} code files."
)
# Initialize repository analyzer to provide enhanced analysis
repo_analyzer = RepoAnalyzer(repo_path=repo_path, branch=branch)
# Update progress - File gathering (70%)
progress.update_step_progress(
AuditStep.FILE_GATHERING, 70,
"Analyzing repository structure and language usage..."
)
# Log analyzer info
repo_analysis = repo_analyzer.analyze()
# Update progress - File gathering complete
progress.update_step_progress(
AuditStep.FILE_GATHERING, 100,
"File gathering and analysis complete!"
)
# Get category handlers, passing branch parameter
category_handlers = get_category_handlers(config, ai_client, repo_path, branch)
# Update category list in progress
for category_name, handler in category_handlers.items():
max_points = config['categories'][category_name]['max_points']
progress.add_pending_category(category_name, max_points)
# Update progress - AI Analysis (start)
progress.update_step_progress(
AuditStep.CODE_ANALYSIS, 5,
"Starting AI code analysis..."
)
# Process each category
results = {}
total_score = 0
total_possible = 0
# Calculate progress increment per category
analysis_increment = 90 // len(category_handlers) if category_handlers else 90
analysis_progress = 5 # Start at 5%
for category_name, handler in category_handlers.items():
# Update progress for this category
progress.update_step_progress(
AuditStep.CODE_ANALYSIS,
analysis_progress + analysis_increment // 2,
f"Analyzing {category_name}..."
)
# Process the category
logger.info(f"Processing category: {category_name}")
score, feedback = handler.process(files)
# Update progress
max_points = config['categories'][category_name]['max_points']
total_possible += max_points
total_score += score
# Store results
results[category_name] = {
'score': score,
'max_points': max_points,
'feedback': feedback
}
# Mark category as complete
progress.complete_category(category_name, score)
# Increment progress
analysis_progress += analysis_increment
progress.update_step_progress(
AuditStep.CODE_ANALYSIS,
analysis_progress,