-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_analyzer.py
More file actions
129 lines (106 loc) · 5.65 KB
/
commit_analyzer.py
File metadata and controls
129 lines (106 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import re
import subprocess
import tempfile
from typing import Optional, List
from git import Repo
from tqdm import tqdm
from penify_hook.base_analyzer import BaseAnalyzer
from penify_hook.jira_client import JiraClient
from penify_hook.ui_utils import print_info, print_success, print_warning
from .api_client import APIClient
class CommitDocGenHook(BaseAnalyzer):
def __init__(self, repo_path: str, api_client: APIClient, llm_client=None, jira_client=None):
super().__init__(repo_path, api_client)
self.llm_client = llm_client # Add LLM client as an optional parameter
self.jira_client: JiraClient = jira_client # Add JIRA client as an optional parameter
def get_summary(self, instruction: str, generate_description: bool) -> dict:
"""Generate a summary for the commit based on the staged changes."""
diff = self.repo.git.diff('--cached')
if not diff:
raise ValueError("No changes to commit")
# Get JIRA context if available
jira_context = None
if self.jira_client and self.jira_client.is_connected():
try:
# Check branch name for JIRA issues
current_branch = self.repo.active_branch.name
issue_keys = self.jira_client.extract_issue_keys_from_branch(current_branch)
# If issues found in branch, get context
if issue_keys:
jira_context = self.jira_client.get_commit_context_from_issues(issue_keys)
except Exception as e:
print(f"Could not get JIRA context: {e}")
# Use LLM client if provided, otherwise use API client
print_info("Fetching commit summary from LLM...")
if self.llm_client:
return self.api_client.generate_commit_summary_with_llm(
diff, instruction, generate_description, self.repo_details, self.llm_client, jira_context
)
else:
return self.api_client.generate_commit_summary(diff, instruction, self.repo_details, jira_context)
def run(self, msg: Optional[str], edit_commit_message: bool, generate_description: bool):
"""Run the post-commit hook and create an auto-commit with optional JIRA
integration."""
summary: dict = self.get_summary(msg, True)
if not summary:
raise Exception("Error generating commit summary")
title = summary.get('title', "")
description = summary.get('description', "")
# If JIRA client is available, integrate JIRA information
if self.jira_client and self.jira_client.is_connected():
# Add JIRA information to commit message
self.process_jira_integration(title, description, msg)
# commit the changes to the repository with above details
commit_msg = f"{title}\n\n{description}" if generate_description else title
self.repo.git.commit('-m', commit_msg)
print_success(f"Commit: {commit_msg}")
if edit_commit_message:
# Open the git commit edit terminal
print_info("Opening git commit edit terminal...")
self._amend_commit()
def process_jira_integration(self, title: str, description: str, msg: str) -> tuple:
# Look for JIRA issue keys in commit message, title, description and user message
"""Process JIRA integration to update commit message with issue keys."""
issue_keys = []
if self.jira_client:
# Extract from message content
issue_keys = self.jira_client.extract_issue_keys(f"{title} {description} {msg}")
# Also check the branch name (which often follows JIRA naming conventions)
try:
current_branch = self.repo.active_branch.name
branch_issue_keys = self.jira_client.extract_issue_keys_from_branch(current_branch)
# Add any new keys found in branch name
for key in branch_issue_keys:
if key not in issue_keys:
issue_keys.append(key)
print_info(f"Added JIRA issue {key} from branch name: {current_branch}")
except Exception as e:
print_warning(f"Could not extract JIRA issues from branch name: {e}")
if issue_keys:
print_info(f"Found JIRA issues: {', '.join(issue_keys)}")
# Format commit message with JIRA info
# Add comments to JIRA issues
for issue_key in issue_keys:
comment = (
f"Commit related to this issue:\n\n"
f"**{title}**\n\n"
f"{description}\n\n"
)
self.jira_client.add_comment(issue_key, comment)
else:
print_warning("No JIRA issues found in commit message or branch name")
return title, description
def _amend_commit(self):
"""Open the default git editor to amend the last commit message."""
try:
# Change to the repository directory
os.chdir(self.repo_path)
# Run git commit --amend
subprocess.run(['git', 'commit', '--amend'], check=True)
print("Commit message amended successfully.")
except subprocess.CalledProcessError as e:
print(f"Error amending commit message: {e}")
finally:
# Change back to the original directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))