-
Notifications
You must be signed in to change notification settings - Fork 967
Cog 334 structure routing #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f09c28a
f8f2746
4fd4651
02d6750
fc3eb9b
eca1b9f
de1ba5c
d5a220e
801efeb
7425409
bce6540
ddf4952
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .get_add_router import get_add_router |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||
| from fastapi import Form, UploadFile, Depends | ||||||
| from fastapi.responses import JSONResponse | ||||||
| from fastapi import APIRouter | ||||||
| from typing import List | ||||||
| import aiohttp | ||||||
| import os | ||||||
|
|
||||||
| from cognee.modules.users.models import User | ||||||
| from cognee.modules.users.methods import get_authenticated_user | ||||||
|
|
||||||
| def get_add_router(): | ||||||
| router = APIRouter() | ||||||
|
|
||||||
| @router.post("/", response_model=None) | ||||||
| async def add( | ||||||
| data: List[UploadFile], | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Type Annotation for 'data' Parameter The parameter Update the type annotation to accept both - from typing import List
+ from typing import List, Union
...
- data: List[UploadFile],
+ data: Union[List[UploadFile], str],
|
||||||
| datasetId: str = Form(...), | ||||||
| user: User = Depends(get_authenticated_user), | ||||||
| ): | ||||||
| """ This endpoint is responsible for adding data to the graph.""" | ||||||
| from cognee.api.v1.add import add as cognee_add | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Import Statement Inside Function Scope Importing modules inside functions is generally discouraged unless necessary, as it can lead to repeated imports and affect performance. Move the import statement to the module level: - from cognee.api.v1.add import add as cognee_add
+ from cognee.api.v1.add import add as cognee_add
...
def get_add_router():
|
||||||
| try: | ||||||
| if isinstance(data, str) and data.startswith("http"): | ||||||
| if "github" in data: | ||||||
|
Comment on lines
+26
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Checking Error: Inconsistent Handling of 'data' The condition Ensure that |
||||||
| # Perform git clone if the URL is from GitHub | ||||||
| repo_name = data.split("/")[-1].replace(".git", "") | ||||||
| os.system(f"git clone {data} .data/{repo_name}") | ||||||
|
||||||
| os.system(f"git clone {data} .data/{repo_name}") | |
| subprocess.run(["git", "clone", data, f".data/{repo_name}"], check=True) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security Issue: Potential Path Traversal when Saving Files
Using data.split('/')[-1] directly as a filename can expose the application to path traversal attacks if data contains malicious input.
Sanitize the filename to prevent path traversal. Use os.path.basename() to extract the filename safely:
- with open(f".data/{data.split('/')[-1]}", "wb") as f:
+ filename = os.path.basename(data)
+ with open(f".data/{filename}", "wb") as f:Committable suggestion was skipped due to low confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concurrency Issue: Asynchronous File Operations Without Proper Locking
The code performs file write operations within an asynchronous function but lacks synchronization mechanisms. This could lead to race conditions in a concurrent environment.
Implement proper synchronization when performing file I/O operations in an asynchronous context. Consider using asynchronous file operations provided by aiofiles:
+ import aiofiles
...
- with open(f".data/{filename}", "wb") as f:
- f.write(file_data)
+ async with aiofiles.open(f".data/{filename}", "wb") as f:
+ await f.write(file_data)Committable suggestion was skipped due to low confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Function Complexity: Consider Refactoring for Better Maintainability
The add endpoint function handles multiple responsibilities, including URL validation, GitHub cloning, HTTP fetching, and data addition. This makes the function complex and harder to maintain.
Refactor the code by extracting logically distinct operations into separate functions or methods. This improves readability and makes the codebase easier to test and maintain.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .get_cognify_router import get_cognify_router |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move module-level imports to the top of the file
According to PEP 8 guidelines, all module-level imports should be placed at the top of the file, after any module comments and docstrings, and before module-level variables and constants. This enhances code readability and maintainability.
Apply this diff to fix the issue:
🧰 Tools
🪛 Ruff