Skip to content

Commit 6ac3aa8

Browse files
Generated Documentation (#48)
Co-authored-by: penify-dev[bot] <146478655+penify-dev[bot]@users.noreply.github.com>
1 parent 773f3be commit 6ac3aa8

9 files changed

Lines changed: 352 additions & 141 deletions

File tree

penify_hook/api_client.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ def send_file_for_docstring_generation(self, file_name, content, line_numbers, r
2323
file_name (str): The path to the file being sent.
2424
content (str): The content of the file to be processed.
2525
line_numbers (list): A list of line numbers that have been modified.
26-
repo_details (str?): Additional repository details if applicable.
26+
repo_details (str?): Additional repository details if applicable. Defaults to None.
2727
2828
Returns:
2929
str: The modified content returned by the API, or the original content if the
3030
request fails.
31+
32+
Raises:
33+
Exception: If there is an error in processing the file and no specific error
34+
message is provided.
3135
"""
3236
payload = {
3337
'file_path': file_name,
@@ -65,6 +69,9 @@ def generate_commit_summary(self, git_diff, instruction: str = "", repo_details
6569
6670
Returns:
6771
dict: The response from the API if the request is successful, None otherwise.
72+
73+
Raises:
74+
Exception: If there is an error during the API request.
6875
"""
6976
payload = {
7077
'git_diff': git_diff,
@@ -113,18 +120,20 @@ def get_supported_file_types(self) -> list[str]:
113120
return ["py", "js", "ts", "java", "kt", "cs", "c"]
114121

115122
def generate_commit_summary_with_llm(self, diff, message, generate_description: bool, repo_details, llm_client : LLMClient, jira_context=None):
116-
"""
117-
Generate a commit summary using a local LLM client instead of the API.
118-
123+
"""Generates a commit summary using a local LLM client. If an error occurs
124+
during the generation process,
125+
it falls back to using the API.
126+
119127
Args:
120-
diff: Git diff of changes
121-
message: User-provided commit message or instructions
122-
repo_details: Details about the repository
123-
llm_client: Instance of LLMClient
124-
jira_context: Optional JIRA issue context to enhance the summary
125-
128+
diff (str): The Git diff of changes.
129+
message (str): User-provided commit message or instructions.
130+
generate_description (bool): Flag indicating whether to generate a description for the commit.
131+
repo_details (dict): Details about the repository.
132+
llm_client (LLMClient): An instance of LLMClient used to generate the summary.
133+
jira_context (JIRAContext?): Optional JIRA issue context to enhance the summary.
134+
126135
Returns:
127-
Dict with title and description for the commit
136+
dict: A dictionary containing the title and description for the commit.
128137
"""
129138
try:
130139
return llm_client.generate_commit_summary(diff, message, generate_description, repo_details, jira_context)
@@ -134,6 +143,16 @@ def generate_commit_summary_with_llm(self, diff, message, generate_description:
134143
return self.generate_commit_summary(diff, message, repo_details, jira_context)
135144

136145
def get_api_key(self):
146+
"""Get an API key from the specified URL.
147+
148+
It constructs a request to fetch an API token using a Bearer token in
149+
the headers. The function handles the response and returns the API key
150+
if successful, or `None` otherwise.
151+
152+
Returns:
153+
str: The API key if the request is successful, `None` otherwise.
154+
"""
155+
137156

138157
url = self.api_url+"/v1/apiToken/get"
139158
response = requests.get(url, headers={"Authorization": f"Bearer {self.BEARER_TOKEN}"}, timeout=60*10)

penify_hook/commit_analyzer.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ def get_summary(self, instruction: str, generate_description: bool) -> dict:
2424
This function retrieves the differences of the staged changes in the
2525
repository and generates a commit summary using the provided
2626
instruction. If there are no changes staged for commit, an exception is
27-
raised. If an LLM client is provided, it will use that for generating
28-
the summary, otherwise it will use the API client.
27+
raised. If a JIRA client is connected, it will attempt to extract issue
28+
keys from the current branch and use them to fetch context. The summary
29+
can be generated either with a Language Model (LLM) client or through
30+
the API client.
2931
3032
Args:
3133
instruction (str): A string containing instructions for generating the commit summary.
34+
generate_description (bool): Whether to include detailed descriptions in the summary.
3235
3336
Returns:
34-
str: The generated commit summary based on the staged changes and provided
35-
instruction.
37+
dict: The generated commit summary based on the staged changes, provided
38+
instruction, and any relevant JIRA context. The dictionary contains keys
39+
such as 'summary', 'description', etc., depending on whether a
40+
description was requested.
3641
3742
Raises:
38-
Exception: If there are no changes staged for commit.
43+
ValueError: If there are no changes staged for commit.
3944
"""
4045
diff = self.repo.git.diff('--cached')
4146
if not diff:
@@ -68,17 +73,17 @@ def get_summary(self, instruction: str, generate_description: bool) -> dict:
6873
def run(self, msg: Optional[str], edit_commit_message: bool, generate_description: bool):
6974
"""Run the post-commit hook.
7075
71-
This method retrieves the list of modified files from the last commit
72-
and processes each file. It stages any files that have been modified
73-
during processing and creates an auto-commit if changes were made. A
74-
progress bar is displayed to indicate the processing status of each
75-
file. If there is an error generating the commit summary, an exception
76-
is raised.
76+
This method processes the modified files from the last commit, stages
77+
them, and creates an auto-commit with an optional message. It also
78+
handles JIRA integration if available. If there is an error generating
79+
the commit summary, an exception is raised.
7780
7881
Args:
7982
msg (Optional[str]): An optional message to include in the commit.
80-
edit_commit_message (bool): A flag indicating whether to open the
81-
git commit edit terminal after committing.
83+
edit_commit_message (bool): A flag indicating whether to open the git commit edit terminal after
84+
committing.
85+
generate_description (bool): A flag indicating whether to include a description in the commit
86+
message.
8287
8388
Raises:
8489
Exception: If there is an error generating the commit summary.
@@ -106,16 +111,16 @@ def run(self, msg: Optional[str], edit_commit_message: bool, generate_descriptio
106111
self._amend_commit()
107112

108113
def process_jira_integration(self, title: str, description: str, msg: str) -> tuple:
109-
"""
110-
Process JIRA integration for the commit message.
111-
114+
"""Process JIRA integration for the commit message.
115+
112116
Args:
113-
title: Generated commit title
114-
description: Generated commit description
115-
msg: Original user message that might contain JIRA references
116-
117+
title (str): Generated commit title.
118+
description (str): Generated commit description.
119+
msg (str): Original user message that might contain JIRA references.
120+
117121
Returns:
118-
tuple: (updated_title, updated_description) with JIRA information
122+
tuple: A tuple containing the updated commit title and description with
123+
included JIRA information.
119124
"""
120125
# Look for JIRA issue keys in commit message, title, description and user message
121126
issue_keys = []

penify_hook/file_analyzer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ def process_file(self, file_path, pbar):
103103
return False
104104

105105
def print_processing(self, file_path):
106-
"""Print a processing message for a file."""
106+
"""Print a processing message for a file.
107+
108+
Args:
109+
file_path (str): The path to the file being processed.
110+
"""
107111
formatted_path = format_file_path(file_path)
108112
print(f"\n{format_highlight(f'Processing file: {formatted_path}')}")
109113

@@ -116,6 +120,9 @@ def run(self):
116120
an error message indicating that the file was not processed. The method
117121
displays a progress bar and colored output to provide visual feedback on
118122
the processing status.
123+
124+
Args:
125+
self (PostCommitHook): An instance of the PostCommitHook class.
119126
"""
120127

121128
# Create a progress bar with appropriate stages

penify_hook/folder_analyzer.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ def __init__(self, dir_path: str, api_client: APIClient):
1212
super().__init__(dir_path, api_client)
1313

1414
def list_all_files_in_dir(self, dir_path: str):
15-
"""List all files in a directory and its subdirectories."""
15+
"""List all files in a directory and its subdirectories.
16+
17+
This function traverses the specified directory using `os.walk`,
18+
collecting paths of all non-hidden files into a list. It filters out
19+
hidden directories (those starting with a dot) to ensure only visible
20+
files are returned.
21+
22+
Args:
23+
dir_path (str): The path to the directory whose files and subdirectory files need to be
24+
listed.
25+
26+
Returns:
27+
list: A list containing the full paths of all non-hidden files within the
28+
specified directory and its subdirectories.
29+
"""
1630

1731
files = []
1832
for dirpath, dirnames, filenames in os.walk(dir_path):
@@ -24,7 +38,17 @@ def list_all_files_in_dir(self, dir_path: str):
2438
return files
2539

2640
def run(self):
27-
"""Run the post-commit hook."""
41+
"""Run the post-commit hook.
42+
43+
This function processes all files in a specified directory using a
44+
progress bar. It lists all files, initializes a `FileAnalyzerGenHook`
45+
for each file, and runs it. Errors during processing of individual files
46+
are caught and logged, but do not stop the processing of other files. A
47+
progress bar is displayed indicating the number of files processed.
48+
49+
Args:
50+
self (PostCommitHook): The instance of the post-commit hook class.
51+
"""
2852
try:
2953
file_list = self.list_all_files_in_dir(self.dir_path)
3054
total_files = len(file_list)

0 commit comments

Comments
 (0)