Skip to content

Add list commits #84

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

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions api/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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
Expand Down Expand Up @@ -448,3 +449,41 @@ def switch_commit():
}

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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using git_utils.GitRepoName(repo).name directly to avoid creating an intermediate GitRepoName object if you only need the name. This can improve readability and potentially reduce object creation overhead.

Suggested change
git_graph = GitGraph(git_utils.GitRepoName(repo))
git_graph = GitGraph(repo)


# Fetch commits from the repository
commits = git_graph.list_commits()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It would be beneficial to add error handling around the git_graph.list_commits() call. If this call fails (e.g., due to repository issues), the API should return an appropriate error response to the client. This enhances the robustness of the API.

    try:
        commits = git_graph.list_commits()
    except Exception as e:
        return jsonify({'status': f'Failed to list commits: {str(e)}'}), 500


# Return success response with the list of commits
response = {
'status': 'success',
'commits': commits
}

return jsonify(response), 200
Comment on lines +453 to +489
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Good implementation of the new endpoint with a few improvements needed.

The new endpoint for listing commits is well-structured and follows the established patterns in the codebase. However, there are a few issues to address:

  1. Missing repository existence check before attempting to fetch commits
  2. No error handling for potential GitGraph operations failures
  3. Unnecessary f-string in the error message

Here's how to address these issues:

@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
+       return jsonify({'status': 'Missing mandatory parameter "repo"'}), 400

+   # Validate repo exists
+   if not graph_exists(repo):
+       logging.error("Missing project %s", repo)
+       return jsonify({"status": f"Missing project {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()
+   try:
+       git_graph = GitGraph(git_utils.GitRepoName(repo))
+       
+       # Fetch commits from the repository
+       commits = git_graph.list_commits()
+   except Exception as e:
+       logging.error("Error retrieving commits for repo '%s': %s", repo, e)
+       return jsonify({"status": "Error retrieving commits"}), 500

    # Return success response with the list of commits
    response = {
        'status': 'success',
        'commits': commits
    }

    return jsonify(response), 200

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff (0.8.2)

475-475: f-string without any placeholders

Remove extraneous f prefix

(F541)

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "code-graph-backend"
version = "0.4.1"
version = "0.4.2"
description = "code_graph is designed to help developers visualize and analyze the structure of their source code. It takes source code as input and generates a graph representation, making it easier to understand relationships and dependencies within the codebase."
authors = ["Roi Lipman <roilipman@gmail.com>"]
readme = "README.md"
Expand Down
Loading