Skip to content
Closed
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
86 changes: 41 additions & 45 deletions penify_hook/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@

class APIClient:
def __init__(self, api_url, api_token: str = None, bearer_token: str = None):
"""Initialize an instance of the API client with necessary authentication tokens.

Args:
api_url (str): The base URL of the API.
api_token (str?): An API token for authentication. Defaults to None.
bearer_token (str?): A Bearer token for authentication. Defaults to None.
"""
self.api_url = api_url
self.AUTH_TOKEN = api_token
self.BEARER_TOKEN = bearer_token

def send_file_for_docstring_generation(self, file_name, content, line_numbers, repo_details = None):
"""Send file content and modified lines to the API and return modified
content.

This function constructs a payload containing the file path, content,
and modified line numbers, and sends it to a specified API endpoint for
processing. It handles the response from the API, returning the modified
content if the request is successful. If the request fails, it logs the
error details and returns the original content.

"""Send file content and modified lines to the API and return modified content.

This function constructs a payload containing the file path, content, and modified line numbers, and sends it to a
specified API endpoint for processing. It handles the response from the API, returning the modified content if the
request is successful. If the request fails, it logs the error details and returns the original content.

Args:
file_name (str): The path to the file being sent.
content (str): The content of the file to be processed.
line_numbers (list): A list of line numbers that have been modified.
repo_details (str?): Additional repository details if applicable. Defaults to None.

Returns:
str: The modified content returned by the API, or the original content if the
request fails.

str: The modified content returned by the API, or the original content if the request fails.

Raises:
Exception: If there is an error in processing the file and no specific error
message is provided.
Exception: If there is an error in processing the file and no specific error message is provided.
"""
payload = {
'file_path': file_name,
Expand All @@ -54,22 +56,20 @@ def send_file_for_docstring_generation(self, file_name, content, line_numbers, r

def generate_commit_summary(self, git_diff, instruction: str = "", repo_details = None, jira_context: dict = None):
"""Generate a commit summary by sending a POST request to the API endpoint.

This function constructs a payload containing the git diff and any
additional instructions provided. It then sends this payload to a
specified API endpoint to generate a summary of the commit. If the
request is successful, it returns the response from the API; otherwise,
it returns None.


This function constructs a payload containing the git diff and any additional instructions provided. It then sends this
payload to a specified API endpoint to generate a summary of the commit. If the request is successful, it returns the
response from the API; otherwise, it returns None.

Args:
git_diff (str): The git diff of the commit.
instruction (str??): Additional instruction for the commit. Defaults to "".
repo_details (dict??): Details of the git repository. Defaults to None.
jira_context (dict??): JIRA issue details to enhance the commit summary. Defaults to None.

instruction (str): Additional instruction for the commit. Defaults to "".
repo_details (dict): Details of the git repository. Defaults to None.
jira_context (dict): JIRA issue details to enhance the commit summary. Defaults to None.
Returns:
dict: The response from the API if the request is successful, None otherwise.

Raises:
Exception: If there is an error during the API request.
"""
Expand Down Expand Up @@ -100,18 +100,16 @@ def generate_commit_summary(self, git_diff, instruction: str = "", repo_details
return None

def get_supported_file_types(self) -> list[str]:
"""Retrieve the supported file types from the API.

This function sends a request to the API endpoint
`/v1/file/supported_languages` to obtain a list of supported file types.
If the API call is successful (status code 200), it parses the JSON
response and returns the list of supported file types. If the API call
fails, it returns a default list of common file types.

"""Retrieve the supported file types from the API.

This function sends a request to the API endpoint `/v1/file/supported_languages` to obtain a list of supported file
types. If the API call is successful (status code 200), it parses the JSON response and returns the list of supported
file types. If the API call fails, it returns a default list of common file types.

Returns:
list[str]: A list of supported file types, either from the API or a default set.
"""

url = self.api_url+"/v1/file/supported_languages"
response = requests.get(url)
if response.status_code == 200:
Expand All @@ -121,18 +119,17 @@ def get_supported_file_types(self) -> list[str]:
return ["py", "js", "ts", "java", "kt", "cs", "c"]

def generate_commit_summary_with_llm(self, diff, message, generate_description: bool, repo_details, llm_client : LLMClient, jira_context=None):
"""Generates a commit summary using a local LLM client. If an error occurs
during the generation process,
"""Generates a commit summary using a local LLM client. If an error occurs during the generation process,
it falls back to using the API.

Args:
diff (str): The Git diff of changes.
message (str): User-provided commit message or instructions.
generate_description (bool): Flag indicating whether to generate a description for the commit.
repo_details (dict): Details about the repository.
llm_client (LLMClient): An instance of LLMClient used to generate the summary.
jira_context (JIRAContext?): Optional JIRA issue context to enhance the summary.

Returns:
dict: A dictionary containing the title and description for the commit.
"""
Expand All @@ -144,17 +141,16 @@ def generate_commit_summary_with_llm(self, diff, message, generate_description:
return self.generate_commit_summary(diff, message, repo_details, jira_context)

def get_api_key(self):
"""Fetch an API key from a specified URL.

This function sends a GET request to retrieve an API token using a
Bearer token in the headers. It handles the response and returns the API
key if the request is successful, or `None` otherwise.

"""Fetch an API key from a specified URL.

This function sends a GET request to retrieve an API token using a Bearer token in the headers. It handles the response
and returns the API key if the request is successful, or `None` otherwise.

Returns:
str: The API key if the request is successful, `None` otherwise.
"""


url = self.api_url+"/v1/apiToken/get"
response = requests.get(url, headers={"Authorization": f"Bearer {self.BEARER_TOKEN}"}, timeout=60*10)
if response.status_code == 200:
Expand Down
6 changes: 6 additions & 0 deletions penify_hook/base_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
class BaseAnalyzer:

def __init__(self, folder_path: str, api_client: APIClient):
"""Initialize an instance of YourClassName with a folder path and an API client.

Args:
folder_path (str): The path to the directory containing the repository.
api_client (APIClient): An instance of the APIClient class for interacting with external services.
"""
self.folder_path = folder_path
self.repo_path = recursive_search_git_folder(folder_path)
self.repo = None
Expand Down
45 changes: 19 additions & 26 deletions penify_hook/commands/commit_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@
def commit_code(api_url, token, message, open_terminal, generate_description,
llm_model=None, llm_api_base=None, llm_api_key=None,
jira_url=None, jira_user=None, jira_api_token=None):
"""Enhance Git commits with AI-powered commit messages.

This function allows for the generation of enhanced commit messages
using natural language processing models and optionally integrates with
JIRA for additional context. It processes the current Git folder to find
relevant files and generates a detailed commit message based on the
provided parameters.

"""Enhance Git commits with AI-powered commit messages.

This function allows for the generation of enhanced commit messages using natural language processing models and
optionally integrates with JIRA for additional context. It processes the current Git folder to find relevant files and
generates a detailed commit message based on the provided parameters.

Args:
api_url (str): URL of the API endpoint.
token (str): Authentication token for the API.
message (str): Initial commit message provided by the user.
open_terminal (bool): Whether to open the terminal after committing.
generate_description (bool): Whether to generate a detailed description in the commit message.
llm_model (str?): The language model to use for generating the commit message. Defaults to
None.
llm_model (str?): The language model to use for generating the commit message. Defaults to None.
llm_api_base (str?): Base URL of the LLM API. Defaults to None.
llm_api_key (str?): API key for accessing the LLM service. Defaults to None.
jira_url (str?): URL of the JIRA instance. Defaults to None.
jira_user (str?): Username for authenticating with JIRA. Defaults to None.
jira_api_token (str?): API token for accessing JIRA. Defaults to None.
"""

from penify_hook.ui_utils import print_error
from penify_hook.utils import recursive_search_git_folder
from ..commit_analyzer import CommitDocGenHook
Expand Down Expand Up @@ -98,18 +95,16 @@ def commit_code(api_url, token, message, open_terminal, generate_description,


def setup_commit_parser(parser):
"""Generates a parser for setting up a command to generate smart commit
messages.

This function sets up an argument parser that can be used to generate
commit messages with contextual information. It allows users to specify
options such as including a message, opening an edit terminal before
committing, and generating a detailed commit message.

"""Generates a parser for setting up a command to generate smart commit messages.

This function sets up an argument parser that can be used to generate commit messages with contextual information. It
allows users to specify options such as including a message, opening an edit terminal before committing, and generating
a detailed commit message.

Args:
parser (argparse.ArgumentParser): The ArgumentParser object to be configured.
"""

commit_parser_description = """
It generates smart commit messages. By default, it will just generate just the Title of the commit message.
1. If you have not configured LLM, it will give an error. You either need to configure LLM or use the API key.
Expand All @@ -126,19 +121,17 @@ def setup_commit_parser(parser):
parser.add_argument("-d", "--description", action="store_false", help="It will generate commit message with title and description.", default=False)

def handle_commit(args):
"""Handle the commit functionality by processing arguments and invoking the
appropriate commands.

This function processes the provided command-line arguments to configure
settings for commit operations, including LLM (Language Model) and Jira
configurations. It then calls the `commit_code` function with these
configurations to perform the actual commit operation.

"""Handle the commit functionality by processing arguments and invoking the appropriate commands.

This function processes the provided command-line arguments to configure settings for commit operations, including LLM
(Language Model) and Jira configurations. It then calls the `commit_code` function with these configurations to perform
the actual commit operation.

Args:
args (argparse.Namespace): The parsed command-line arguments containing options like terminal,
description, message, etc.
"""

from penify_hook.commands.commit_commands import commit_code
from penify_hook.commands.config_commands import get_jira_config, get_llm_config, get_token
from penify_hook.constants import API_URL
Expand Down
46 changes: 19 additions & 27 deletions penify_hook/commands/doc_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import os

def generate_doc(api_url, token, location=None):
"""Generates documentation based on the given parameters.

This function initializes an API client using the provided API URL and
token. It then generates documentation by analyzing the specified
location, which can be a folder, a file, or the current working
directory if no location is provided. The function handles different
types of analysis based on the input location and reports any errors
"""Generates documentation based on the given parameters.

This function initializes an API client using the provided API URL and token. It then generates documentation by
analyzing the specified location, which can be a folder, a file, or the current working directory if no location is
provided. The function handles different types of analysis based on the input location and reports any errors
encountered during the process.

Args:
api_url (str): The URL of the API to connect to for documentation generation.
token (str): The authentication token for accessing the API.
location (str?): The path to a specific file or folder to analyze. If not provided, the
current working directory is used.
"""

import os
import sys
from ..folder_analyzer import FolderAnalyzerGenHook
Expand Down Expand Up @@ -80,19 +78,16 @@ def generate_doc(api_url, token, location=None):
"""

def setup_docgen_parser(parser):
"""Set up and configure a parser for documentation generation using Git
commands.

This function configures a parser with various subcommands and arguments
necessary for generating documentation for Git diffs, files, or folders.
It also installs and uninstalls commit hooks to automate documentation
generation on commits.

# We don't need to create a new docgen_parser since it's passed as a parameter
"""Set up and configure a parser for documentation generation using Git commands.

This function configures a parser with various subcommands and arguments necessary for generating documentation for Git
diffs, files, or folders. It also installs and uninstalls commit hooks to automate documentation generation on commits.

Args:
parser (argparse.ArgumentParser): The parser to configure.
"""

# We don't need to create a new docgen_parser since it's passed as a parameter
docgen_parser_description = """
It generates Documentation for the Git diff, file or folder.
1. By default, it will git diff documentation - visit https://penify.wiki/dcdc for more details.
Expand Down Expand Up @@ -122,19 +117,16 @@ def setup_docgen_parser(parser):
default=os.getcwd())

def handle_docgen(args):
"""Handle various subcommands related to document generation and hook
management.

This function processes different subcommands such as installing or
uninstalling git hooks, and directly generating documentation based on
provided arguments.

# Only import dependencies needed for docgen functionality here
"""Handle various subcommands related to document generation and hook management.

This function processes different subcommands such as installing or uninstalling git hooks, and directly generating
documentation based on provided arguments.

Args:
args (Namespace): Parsed command-line arguments containing the subcommand and location
details.
args (Namespace): Parsed command-line arguments containing the subcommand and location details.
"""

# Only import dependencies needed for docgen functionality here
from penify_hook.commands.config_commands import get_token
import sys
from penify_hook.commands.doc_commands import generate_doc
Expand Down
20 changes: 8 additions & 12 deletions penify_hook/commands/hook_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
"""

def install_git_hook(location, token):
"""Install a post-commit hook in the specified location that generates
documentation
for changed files after each commit.

"""Install a post-commit hook in the specified location that generates documentation for changed files after each commit.

Args:
location (str): The path to the Git repository where the hook should be installed.
token (str): The authentication token required to access the documentation generation
service.
token (str): The authentication token required to access the documentation generation service.
"""
hooks_dir = Path(location) / ".git/hooks"
hook_path = hooks_dir / HOOK_FILENAME
Expand All @@ -35,12 +32,11 @@ def install_git_hook(location, token):

def uninstall_git_hook(location):
"""Uninstalls the post-commit hook from the specified location.

This function attempts to remove a post-commit git hook located at the
given path. It constructs the path to the hook and checks if it exists.
If the hook is found, it is deleted, and a confirmation message is
printed. If no hook is found, a message indicating this is also printed.


This function attempts to remove a post-commit git hook located at the given path. It constructs the path to the hook
and checks if it exists. If the hook is found, it is deleted, and a confirmation message is printed. If no hook is
found, a message indicating this is also printed.

Args:
location (Path): The base directory where the .git/hooks directory is located.
"""
Expand Down
Loading
Loading