This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.py
More file actions
489 lines (379 loc) · 14.2 KB
/
index.py
File metadata and controls
489 lines (379 loc) · 14.2 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
""" Main API module for CodeGraph. """
import os
from pathlib import Path
from functools import wraps
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from api.analyzers.source_analyzer import SourceAnalyzer
from api.git_utils import git_utils
from api.git_utils.git_graph import GitGraph
from api.graph import Graph, get_repos, graph_exists
from api.info import get_repo_info
from api.llm import ask
from api.project import Project
from .auto_complete import prefix_search
# Load environment variables from .env file
load_dotenv()
# Configure the logger
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
SECRET_TOKEN = os.getenv('SECRET_TOKEN')
def verify_token(token):
""" Verify the token provided in the request """
return token == SECRET_TOKEN or (token is None and SECRET_TOKEN is None)
def token_required(f):
""" Decorator to protect routes with token authentication """
@wraps(f)
def decorated_function(*args, **kwargs):
token = request.headers.get('Authorization') # Get token from header
if not verify_token(token):
return jsonify(message="Unauthorized"), 401
return f(*args, **kwargs)
return decorated_function
app = Flask(__name__)
def public_access(f):
""" Decorator to protect routes with public access """
@wraps(f)
def decorated_function(*args, **kwargs):
public = os.environ.get("CODE_GRAPH_PUBLIC", "0") # Get public access setting
if public != "1":
return jsonify(message="Unauthorized"), 401
return f(*args, **kwargs)
return decorated_function
@app.route('/graph_entities', methods=['GET'])
@token_required # Apply token authentication decorator
def graph_entities():
"""
Endpoint to fetch sub-graph entities from a given repository.
The repository is specified via the 'repo' query parameter.
Returns:
- 200: Successfully returns the sub-graph.
- 400: Missing or invalid 'repo' parameter.
- 500: Internal server error or database connection issue.
"""
# Access the 'repo' parameter from the GET request
repo = request.args.get('repo')
if not repo:
logging.error("Missing 'repo' parameter in request.")
return jsonify({"status": "Missing 'repo' parameter"}), 400
if not graph_exists(repo):
logging.error("Missing project %s", repo)
return jsonify({"status": f"Missing project {repo}"}), 400
try:
# Initialize the graph with the provided repo and credentials
g = Graph(repo)
# Retrieve a sub-graph of up to 500 entities
sub_graph = g.get_sub_graph(500)
logging.info("Successfully retrieved sub-graph for repo: %s", repo)
response = {
'status': 'success',
'entities': sub_graph
}
return jsonify(response), 200
except Exception as e:
logging.error("Error retrieving sub-graph for repo '%s': %s", repo, e)
return jsonify({"status": "Internal server error"}), 500
@app.route('/get_neighbors', methods=['POST'])
@token_required # Apply token authentication decorator
def get_neighbors():
"""
Endpoint to get neighbors of a nodes list in the graph.
Expects 'repo' and 'node_ids' as body parameters.
Returns:
JSON response containing neighbors or error messages.
"""
# Get JSON data from the request
data = request.get_json()
# Get query parameters
repo = data.get('repo')
node_ids = data.get('node_ids')
# Validate 'repo' parameter
if not repo:
logging.error("Repository name is missing in the request.")
return jsonify({"status": "Repository name is required."}), 400
# Validate 'node_ids' parameter
if not node_ids:
logging.error("Node IDs is missing in the request.")
return jsonify({"status": "Node IDs is required."}), 400
# Validate repo exists
if not graph_exists(repo):
logging.error("Missing project %s", repo)
return jsonify({"status": f"Missing project {repo}"}), 400
# Initialize the graph with the provided repository
g = Graph(repo)
# Fetch the neighbors of the specified node
neighbors = g.get_neighbors(node_ids)
# Log and return the neighbors
logging.info("Successfully retrieved neighbors for node IDs %s in repo '%s'.", node_ids, repo)
response = {
'status': 'success',
'neighbors': neighbors
}
return jsonify(response), 200
@app.route('/auto_complete', methods=['POST'])
@token_required # Apply token authentication decorator
def auto_complete():
"""
Endpoint to process auto-completion requests for a repository based on a prefix.
Returns:
JSON response with auto-completion suggestions or an error message.
"""
# Get JSON data from the request
data = request.get_json()
# Validate that 'repo' is provided
repo = data.get('repo')
if repo is None:
return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400
# Validate that 'prefix' is provided
prefix = data.get('prefix')
if prefix is None:
return jsonify({'status': 'Missing mandatory parameter "prefix"'}), 400
# Validate repo exists
if not graph_exists(repo):
return jsonify({'status': f'Missing project {repo}'}), 400
# Fetch auto-completion results
completions = prefix_search(repo, prefix)
# Create a success response
response = {
'status': 'success',
'completions': completions
}
return jsonify(response), 200
@app.route('/list_repos', methods=['GET'])
@token_required # Apply token authentication decorator
def list_repos():
"""
Endpoint to list all available repositories.
Returns:
JSON response with a list of repositories or an error message.
"""
# Fetch list of repositories
repos = get_repos()
# Create a success response with the list of repositories
response = {
'status': 'success',
'repositories': repos
}
return jsonify(response), 200
@app.route('/repo_info', methods=['POST'])
@token_required # Apply token authentication decorator
def repo_info():
"""
Endpoint to retrieve information about a specific repository.
Expected JSON payload:
{
"repo": <repository name>
}
Returns:
JSON: A response containing the status and graph statistics (node and edge counts).
- 'status': 'success' if successful, or an error message.
- 'info': A dictionary with the node and edge counts if the request is successful.
"""
# Get JSON data from the request
data = request.get_json()
# Validate the 'repo' parameter
repo = data.get('repo')
if repo is None:
return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400
# Initialize the graph with the provided repository name
g = Graph(repo)
# Retrieve statistics from the graph
stats = g.stats()
info = get_repo_info(repo)
if stats is None or info is None:
return jsonify({'status': f'Missing repository "{repo}"'}), 400
stats |= info
# Create a response
response = {
'status': 'success',
'info': stats
}
return jsonify(response), 200
@app.route('/find_paths', methods=['POST'])
@token_required # Apply token authentication decorator
def find_paths():
"""
Finds all paths between a source node (src) and a destination node (dest) in the graph.
The graph is associated with the repository (repo) provided in the request.
Request Body (JSON):
- repo (str): Name of the repository.
- src (int): ID of the source node.
- dest (int): ID of the destination node.
Returns:
A JSON response with:
- status (str): Status of the request ("success" or "error").
- paths (list): List of paths between the source and destination nodes.
"""
# Get JSON data from the request
data = request.get_json()
# Validate 'repo' parameter
repo = data.get('repo')
if repo is None:
return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400
# Validate 'src' parameter
src = data.get('src')
if src is None:
return jsonify({'status': 'Missing mandatory parameter "src"'}), 400
if not isinstance(src, int):
return jsonify({'status': "src node id must be int"}), 400
# Validate 'dest' parameter
dest = data.get('dest')
if dest is None:
return jsonify({'status': 'Missing mandatory parameter "dest"'}), 400
if not isinstance(dest, int):
return jsonify({'status': "dest node id must be int"}), 400
if not graph_exists(repo):
logging.error("Missing project %s", repo)
return jsonify({"status": f"Missing project {repo}"}), 400
# Initialize graph with provided repo and credentials
g = Graph(repo)
# Find paths between the source and destination nodes
paths = g.find_paths(src, dest)
# Create and return a successful response
response = { 'status': 'success', 'paths': paths }
return jsonify(response), 200
@app.route('/chat', methods=['POST'])
@token_required # Apply token authentication decorator
def chat():
""" Endpoint to chat with the CodeGraph language model. """
# Get JSON data from the request
data = request.get_json()
# Validate 'repo' parameter
repo = data.get('repo')
if repo is None:
return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400
# Get optional 'label' and 'relation' parameters
msg = data.get('msg')
if msg is None:
return jsonify({'status': 'Missing mandatory parameter "msg"'}), 400
answer = ask(repo, msg)
# Create and return a successful response
response = { 'status': 'success', 'response': answer }
return jsonify(response), 200
@app.route('/analyze_folder', methods=['POST'])
@token_required # Apply token authentication decorator
def analyze_folder():
"""
Endpoint to analyze local source code
Expects 'path' and optionally an ignore list.
Returns:
JSON response with status and error message if applicable
Status codes:
200: Success
400: Invalid input
500: Internal server error
"""
# Get JSON data from the request
data = request.get_json()
# Get query parameters
path = data.get('path')
ignore = data.get('ignore', [])
# Validate input parameters
if not path:
logging.error("'path' is missing from the request.")
return jsonify({"status": "'path' is required."}), 400
# Validate path exists and is a directory
if not os.path.isdir(path):
logging.error("Path '%s' does not exist or is not a directory", path)
return jsonify({"status": "Invalid path: must be an existing directory"}), 400
# Validate ignore list contains valid paths
if not isinstance(ignore, list):
logging.error("'ignore' must be a list of paths")
return jsonify({"status": "'ignore' must be a list of paths"}), 400
proj_name = Path(path).name
# Initialize the graph with the provided project name
g = Graph(proj_name)
# Analyze source code within given folder
analyzer = SourceAnalyzer()
analyzer.analyze_local_folder(path, g, ignore)
# Return response
response = {
'status': 'success',
'project': proj_name
}
return jsonify(response), 200
@app.route('/analyze_repo', methods=['POST'])
@public_access # Apply public access decorator
@token_required # Apply token authentication decorator
def analyze_repo():
"""
Analyze a GitHub repository.
Expected JSON payload:
{
"repo_url": "string",
"ignore": ["string"] # optional
}
Returns:
JSON response with processing status
"""
data = request.get_json()
url = data.get('repo_url')
if url is None:
return jsonify({'status': 'Missing mandatory parameter "url"'}), 400
logger.debug('Received repo_url: %s', url)
ignore = data.get('ignore', [])
proj = Project.from_git_repository(url)
proj.analyze_sources(ignore)
proj.process_git_history(ignore)
# Create a response
response = {
'status': 'success',
}
return jsonify(response), 200
@app.route('/switch_commit', methods=['POST'])
@public_access # Apply public access decorator
@token_required # Apply token authentication decorator
def switch_commit():
"""
Endpoint to switch a repository to a specific commit.
Returns:
JSON response with the change set or an error message.
"""
# Get JSON data from the request
data = request.get_json()
# Validate that 'repo' is provided
repo = data.get('repo')
if repo is None:
return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400
# Validate that 'commit' is provided
commit = data.get('commit')
if commit is None:
return jsonify({'status': 'Missing mandatory parameter "commit"'}), 400
# Attempt to switch the repository to the specified commit
git_utils.switch_commit(repo, commit)
# Create a success response
response = {
'status': 'success'
}
return jsonify(response), 200
@app.route('/list_commits', methods=['POST'])
@public_access # Apply public access decorator
@token_required # Apply token authentication decorator
def list_commits():
"""
Endpoint to list all commits of a specified repository.
Request JSON Structure:
{
"repo": "repository_name"
}
Returns:
JSON response with a list of commits or an error message.
"""
# Get JSON data from the request
data = request.get_json()
# Validate the presence of the 'repo' parameter
repo = data.get('repo')
if repo is None:
return jsonify({'status': f'Missing mandatory parameter "repo"'}), 400
# Initialize GitGraph object to interact with the repository
git_graph = GitGraph(git_utils.GitRepoName(repo))
# Fetch commits from the repository
commits = git_graph.list_commits()
# Return success response with the list of commits
response = {
'status': 'success',
'commits': commits
}
return jsonify(response), 200