LukeMattingly commited on
Commit
8e307ce
·
1 Parent(s): 650b8c5

updated method for getting list of files changed

Browse files
Files changed (1) hide show
  1. app.py +48 -7
app.py CHANGED
@@ -6,6 +6,8 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  import re
8
  import ast
 
 
9
 
10
  from Gradio_UI import GradioUI
11
 
@@ -99,10 +101,49 @@ def get_pr_diff(github_url: str, pr_number: int, start_line: int = None, end_lin
99
  return "\n".join(diff_lines)
100
  except Exception as e:
101
  return f"Error retrieving PR diff: {str(e)}"
 
 
 
 
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  @tool
105
- def get_pr_files_changed(github_url: str, pr_number: int) -> str:
106
  """Retrieves the list of files changed in a given pull request.
107
 
108
  Args:
@@ -110,8 +151,8 @@ def get_pr_files_changed(github_url: str, pr_number: int) -> str:
110
  pr_number: The pull request number for which the changed files should be retrieved.
111
 
112
  Returns:
113
- A string listing the files modified in the specified pull request.
114
- If no files were found or an error occurs, returns an appropriate message.
115
  """
116
  try:
117
  owner_repo = github_url.replace("https://github.com/", "")
@@ -119,13 +160,13 @@ def get_pr_files_changed(github_url: str, pr_number: int) -> str:
119
  response = requests.get(api_url)
120
 
121
  if response.status_code != 200:
122
- return f"Error fetching PR files: {response.json().get('message', 'Unknown error')}"
123
 
124
  files = response.json()
125
- return "\n".join([file['filename'] for file in files])
126
 
127
  except Exception as e:
128
- return f"Error retrieving files for PR #{pr_number}: {str(e)}"
129
 
130
  @tool
131
  def detect_code_smells(code: str) -> str:
@@ -281,7 +322,7 @@ with open("prompts.yaml", 'r') as stream:
281
 
282
  agent = CodeAgent(
283
  model=model,
284
- tools=[final_answer, get_open_pull_requests, find_todo_comments, get_pr_diff, get_pr_files_changed, detect_code_smells, get_file_content, security_check_code, check_documentation_updates, lint_code ], ## add your tools here (don't remove final answer)
285
  max_steps=6,
286
  verbosity_level=1,
287
  grammar=None,
 
6
  from tools.final_answer import FinalAnswerTool
7
  import re
8
  import ast
9
+ from typing import List
10
+
11
 
12
  from Gradio_UI import GradioUI
13
 
 
101
  return "\n".join(diff_lines)
102
  except Exception as e:
103
  return f"Error retrieving PR diff: {str(e)}"
104
+
105
+ @tool
106
+ def get_pr_diff_for_file(github_url: str, pr_number: int, file_path: str) -> str:
107
+ """Fetches the code diff for a specific file in a given pull request.
108
 
109
+ Args:
110
+ github_url: The URL of the GitHub repository where the pull request is located.
111
+ (e.g., 'https://github.com/crewAIInc/crewAI').
112
+ pr_number: The pull request number for which the diff should be retrieved.
113
+ file_path: The relative path of the file within the repository to retrieve the diff for
114
+ (e.g., 'src/module.py').
115
+
116
+ Returns:
117
+ A string containing the code diff (patch) for the specified file in the pull request.
118
+ If the file is not found in the PR or if its diff is not available, returns an error message.
119
+ """
120
+ try:
121
+ # Extract owner and repo from the URL
122
+ owner_repo = github_url.replace("https://github.com/", "")
123
+ # API endpoint to get files changed in the PR
124
+ api_url = f"https://api.github.com/repos/{owner_repo}/pulls/{pr_number}/files"
125
+ response = requests.get(api_url)
126
+
127
+ if response.status_code != 200:
128
+ return f"Error fetching PR files: {response.json().get('message', 'Unknown error')}"
129
+
130
+ files = response.json()
131
+ # Look for the specific file in the list
132
+ for file_info in files:
133
+ if file_info.get('filename') == file_path:
134
+ patch = file_info.get('patch')
135
+ if patch:
136
+ return patch
137
+ else:
138
+ return f"No diff (patch) available for file: {file_path}"
139
+
140
+ return f"File '{file_path}' not found in the pull request."
141
+ except Exception as e:
142
+ return f"Error retrieving PR diff for file: {str(e)}"
143
+
144
 
145
  @tool
146
+ def get_pr_files_changed(github_url: str, pr_number: int) -> List[str]:
147
  """Retrieves the list of files changed in a given pull request.
148
 
149
  Args:
 
151
  pr_number: The pull request number for which the changed files should be retrieved.
152
 
153
  Returns:
154
+ A list of strings, where each string is a file path that was modified in the specified pull request.
155
+ If no files are found or an error occurs, returns a list with an appropriate error message.
156
  """
157
  try:
158
  owner_repo = github_url.replace("https://github.com/", "")
 
160
  response = requests.get(api_url)
161
 
162
  if response.status_code != 200:
163
+ return [f"Error fetching PR files: {response.json().get('message', 'Unknown error')}"]
164
 
165
  files = response.json()
166
+ return [file['filename'] for file in files]
167
 
168
  except Exception as e:
169
+ return [f"Error retrieving files for PR #{pr_number}: {str(e)}"]
170
 
171
  @tool
172
  def detect_code_smells(code: str) -> str:
 
322
 
323
  agent = CodeAgent(
324
  model=model,
325
+ tools=[final_answer, get_open_pull_requests, find_todo_comments, get_pr_diff, get_pr_files_changed, detect_code_smells, get_file_content, security_check_code, check_documentation_updates, lint_code, get_pr_diff_for_file ], ## add your tools here (don't remove final answer)
326
  max_steps=6,
327
  verbosity_level=1,
328
  grammar=None,