Skip to content

Commit cf20131

Browse files
committed
add repo url to repo_info
1 parent 94cac70 commit cf20131

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

code_graph/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .info import *
12
from .graph import *
23
from .entities import *
34
from .git_utils import *

code_graph/info.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import redis
3+
import logging
4+
from typing import Optional, Dict
5+
6+
# Configure logging
7+
logging.basicConfig(level=logging.INFO)
8+
9+
def get_redis_connection() -> redis.Redis:
10+
"""
11+
Establishes a connection to Redis using environment variables.
12+
13+
Returns:
14+
redis.Redis: A Redis connection object.
15+
"""
16+
try:
17+
return redis.Redis(
18+
host = os.getenv('FALKORDB_HOST'),
19+
port = os.getenv('FALKORDB_PORT'),
20+
username = os.getenv('FALKORDB_USERNAME'),
21+
password = os.getenv('FALKORDB_PASSWORD'),
22+
decode_responses = True # To ensure string responses
23+
)
24+
except Exception as e:
25+
logging.error(f"Error connecting to Redis: {e}")
26+
raise
27+
28+
29+
def save_repo_info(repo_name: str, repo_url: str) -> None:
30+
"""
31+
Saves repository information (URL) to Redis under a hash named {repo_name}_info.
32+
33+
Args:
34+
repo_name (str): The name of the repository.
35+
repo_url (str): The URL of the repository.
36+
"""
37+
38+
try:
39+
r = get_redis_connection()
40+
key = f"{{ {repo_name} }}_info" # Safely format the key
41+
42+
# Save the repository URL
43+
r.hset(key, 'repo_url', repo_url)
44+
logging.info(f"Repository info saved for {repo_name}")
45+
46+
except Exception as e:
47+
logging.error(f"Error saving repo info for '{repo_name}': {e}")
48+
raise
49+
50+
def get_repo_info(repo_name: str) -> Optional[Dict[str, str]]:
51+
"""
52+
Retrieves repository information from Redis.
53+
54+
Args:
55+
repo_name (str): The name of the repository.
56+
57+
Returns:
58+
Optional[Dict[str, str]]: A dictionary of repository information, or None if not found.
59+
"""
60+
try:
61+
r = get_redis_connection()
62+
key = f"{{{repo_name}}}_info"
63+
64+
# Retrieve all information about the repository
65+
repo_info = r.hgetall(key)
66+
if not repo_info:
67+
logging.warning(f"No repository info found for {repo_name}")
68+
return None
69+
70+
logging.info(f"Repository info retrieved for {repo_name}")
71+
return repo_info
72+
73+
except Exception as e:
74+
logging.error(f"Error retrieving repo info for '{repo_name}': {e}")
75+
raise
76+

main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import redis
32
import datetime
43
from code_graph import *
54
from typing import Optional
@@ -70,6 +69,7 @@ def get_neighbors():
7069
Endpoint to get neighbors of a specific node in the graph.
7170
Expects 'repo' and 'node_id' as query parameters.
7271
"""
72+
7373
repo = request.args.get('repo')
7474
node_id = request.args.get('node_id')
7575

@@ -155,6 +155,8 @@ def process_repo():
155155
logger.error(f'An error occurred: {e}')
156156
return jsonify({'status': f'Failed to process repository: {git_url}'}), 400
157157

158+
save_repo_info(repo_name, repo_url)
159+
158160
# Create a response
159161
response = {
160162
'status': 'success',
@@ -358,7 +360,7 @@ def repo_info():
358360
g = Graph(repo)
359361

360362
# Retrieve statistics from the graph
361-
stats = g.stats()
363+
stats = g.stats() | get_repo_info(repo)
362364

363365
# Create a response
364366
response = {

0 commit comments

Comments
 (0)