Skip to content

Commit 0bb4591

Browse files
committed
Refactor FolderAnalyzerGenHook to handle errors during file analysis
1 parent 15563e6 commit 0bb4591

4 files changed

Lines changed: 96 additions & 27 deletions

File tree

penify_hook/commit_analyzer.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ def __init__(self, repo_path: str, api_client: APIClient):
1616
self.repo_details = self.get_repo_details()
1717

1818
def get_repo_details(self):
19-
"""
20-
Get the details of the repository, including the hosting service (e.g., GitHub, Azure DevOps),
19+
"""Get the details of the repository, including the hosting service,
2120
organization name, and repository name.
2221
23-
This method checks the remote URL of the repository to determine whether it is hosted on
24-
GitHub, Azure DevOps, or another service. It also extracts the organization (or user) name
25-
and the repository name from the URL.
22+
This method checks the remote URL of the repository to determine whether
23+
it is hosted on GitHub, Azure DevOps, Bitbucket, GitLab, or another
24+
service. It extracts the organization (or user) name and the repository
25+
name from the URL. If the hosting service is not recognized, it will
26+
return "Unknown Hosting Service". The method handles potential errors
27+
during the extraction process and returns a dictionary with the relevant
28+
details.
2629
2730
Returns:
28-
dict: A dictionary containing the hosting service, organization name, repository name, and remote URL.
31+
dict: A dictionary containing the organization name, repository name, and
32+
hosting service.
2933
"""
3034
remote_url = None
3135
hosting_service = "Unknown"
@@ -75,6 +79,24 @@ def get_repo_details(self):
7579
}
7680

7781
def get_summary(self, instruction: str):
82+
"""Generate a summary for the commit based on the staged changes.
83+
84+
This function retrieves the differences of the staged changes in the
85+
repository and generates a commit summary using the provided
86+
instruction. If there are no changes staged for commit, an exception is
87+
raised.
88+
89+
Args:
90+
instruction (str): A string containing instructions for generating the commit summary.
91+
92+
Returns:
93+
str: The generated commit summary based on the staged changes and provided
94+
instruction.
95+
96+
Raises:
97+
Exception: If there are no changes staged for commit.
98+
"""
99+
78100
diff = self.repo.git.diff('--cached')
79101
if not diff:
80102
raise Exception("No changes to commit")
@@ -88,7 +110,16 @@ def run(self, msg: Optional[str], edit_commit_message: bool):
88110
and processes each file. It stages any files that have been modified
89111
during processing and creates an auto-commit if changes were made. A
90112
progress bar is displayed to indicate the processing status of each
91-
file.
113+
file. If there is an error generating the commit summary, an exception
114+
is raised.
115+
116+
Args:
117+
msg (Optional[str]): An optional message to include in the commit.
118+
edit_commit_message (bool): A flag indicating whether to open the
119+
git commit edit terminal after committing.
120+
121+
Raises:
122+
Exception: If there is an error generating the commit summary.
92123
"""
93124
summary: dict = self.get_summary(msg)
94125
if not summary:
@@ -106,11 +137,12 @@ def run(self, msg: Optional[str], edit_commit_message: bool):
106137

107138

108139
def _amend_commit(self):
109-
"""
110-
Open the default git editor for editing the commit message.
111-
112-
Args:
113-
initial_message (str): The initial commit message to populate the editor with.
140+
"""Open the default git editor for editing the commit message.
141+
142+
This function changes the current working directory to the repository
143+
path, runs the git command to amend the last commit, and opens the
144+
default editor for the user to modify the commit message. After the
145+
operation, it returns to the original directory.
114146
"""
115147
try:
116148
# Change to the repository directory

penify_hook/folder_analyzer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ def run(self):
4242
them and running a file analyzer on each one. It provides feedback on
4343
the number of files being processed and handles any errors that occur
4444
during the analysis of individual files, ensuring that the progress bar
45-
updates appropriately even in the event of an error.
45+
updates appropriately even in the event of an error. It first retrieves
46+
a list of all files in the specified directory and prints the total
47+
number of files to be processed. Then, it initializes a progress bar to
48+
visually indicate the processing status. For each file, it attempts to
49+
create an instance of `FileAnalyzerGenHook` and run its analysis method.
50+
If an error occurs while processing a file, it catches the exception,
51+
prints an error message, and continues with the next file, ensuring that
52+
the progress bar reflects the ongoing processing.
4653
"""
4754
try:
4855
file_list = self.list_all_files_in_dir(self.dir_path)

penify_hook/git_analyzer.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ def __init__(self, repo_path: str, api_client: APIClient):
1313
self.repo_details = self.get_repo_details()
1414

1515
def get_repo_details(self):
16-
"""
17-
Get the details of the repository, including the hosting service (e.g., GitHub, Azure DevOps),
16+
"""Get the details of the repository, including the hosting service,
1817
organization name, and repository name.
1918
20-
This method checks the remote URL of the repository to determine whether it is hosted on
21-
GitHub, Azure DevOps, or another service. It also extracts the organization (or user) name
22-
and the repository name from the URL.
19+
This method checks the remote URL of the repository to determine whether
20+
it is hosted on GitHub, Azure DevOps, Bitbucket, GitLab, or another
21+
service. It extracts the organization (or user) name and the repository
22+
name from the URL. If the hosting service cannot be determined, it will
23+
return "Unknown Hosting Service".
2324
2425
Returns:
25-
dict: A dictionary containing the hosting service, organization name, repository name, and remote URL.
26+
dict: A dictionary containing the organization name, repository name, and
27+
hosting service.
2628
"""
2729
remote_url = None
2830
hosting_service = "Unknown"
@@ -143,13 +145,13 @@ def process_file(self, file_path):
143145
"""Process a file by checking its type, reading its content, and sending it
144146
to an API.
145147
146-
This method first constructs the absolute path of the file and checks if
147-
the file has a valid extension. If the file type is supported, it reads
148-
the content of the file and retrieves the differences from the last
149-
commit in the repository. If there are changes detected, it sends the
150-
file content along with the modified lines to an API for further
151-
processing. If the API response indicates no changes, it will not
152-
overwrite the original file.
148+
This method constructs the absolute path of the specified file and
149+
verifies if the file has a valid extension. If the file type is
150+
supported, it reads the content of the file and retrieves the
151+
differences from the last commit in the repository. If changes are
152+
detected, it sends the file content along with the modified lines to an
153+
API for further processing. If the API response indicates no changes,
154+
the original file will not be overwritten.
153155
154156
Args:
155157
file_path (str): The relative path to the file to be processed.
@@ -206,7 +208,10 @@ def run(self):
206208
and processes each file. It stages any files that have been modified
207209
during processing and creates an auto-commit if changes were made. A
208210
progress bar is displayed to indicate the processing status of each
209-
file.
211+
file. The method handles any exceptions that occur during file
212+
processing, printing an error message for each file that fails to
213+
process. If any modifications are made to the files, an auto-commit is
214+
created to save those changes.
210215
"""
211216
modified_files = self.get_modified_files_in_last_commit()
212217
changes_made = False

penify_hook/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
api_url = 'https://production-gateway.snorkell.ai/api'
2020

2121
def install_git_hook(location, token):
22+
"""
23+
Install a post-commit hook in the specified location.
24+
Args:
25+
location (str): The path to the Git repository.
26+
token (str): The token to be used in the hook.
27+
Raises:
28+
SystemExit: If the hooks directory does not exist.
29+
Returns:
30+
None
31+
"""
2232
hooks_dir = Path(location) / ".git/hooks"
2333
hook_path = hooks_dir / HOOK_FILENAME
2434

@@ -33,6 +43,13 @@ def install_git_hook(location, token):
3343
print(f"Post-commit hook installed in {hook_path}")
3444

3545
def uninstall_git_hook(location):
46+
"""
47+
Uninstalls the post-commit hook from the specified location.
48+
Parameters:
49+
- location (str): The path to the directory containing the Git repository.
50+
Returns:
51+
None
52+
"""
3653
hook_path = Path(location) / ".git/hooks" / HOOK_FILENAME
3754

3855
if hook_path.exists():
@@ -42,6 +59,14 @@ def uninstall_git_hook(location):
4259
print(f"No post-commit hook found in {hook_path}")
4360

4461
def generate_doc(token, file_path=None, complete_folder_path=None, git_folder_path=None):
62+
"""
63+
Generates documentation based on the given parameters.
64+
Parameters:
65+
- token (str): The API token for authentication.
66+
- file_path (str, optional): The path to a specific file to generate documentation for.
67+
- complete_folder_path (str, optional): The path to a complete folder to generate documentation for.
68+
- git_folder_path (str, optional): The path to a Git repository folder to generate documentation for.
69+
"""
4570

4671
api_client = APIClient(api_url, token)
4772

0 commit comments

Comments
 (0)