Skip to content

Commit 4f8b7ec

Browse files
authored
chore: github list open pr given owner (#53)
1 parent 7c2c71b commit 4f8b7ec

File tree

4 files changed

+265
-201
lines changed

4 files changed

+265
-201
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-github-pr-issue-analyser"
3-
version = "2.2.2"
3+
version = "2.3.0"
44
description = "MCP GitHub Issues Create/Update and PR Analyse"
55
readme = "README.md"
66
requires-python = ">=3.12"

src/mcp_github/github_integration.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,49 @@ def update_pr_description(self, repo_owner: str, repo_name: str, pr_number: int,
270270
traceback.print_exc()
271271
return None
272272

273+
def list_open_prs(self, repo_owner: str) -> Dict[str, Any]:
274+
"""
275+
Lists all open pull requests for a given repository owner.
276+
Args:
277+
repo_owner (str): The owner of the repository.
278+
Returns:
279+
Dict[str, Any]: A dictionary containing the list of open pull requests.
280+
None: If an error occurs during the request.
281+
Error Handling:
282+
Logs an error message and prints the traceback if the request fails or an exception is raised.
283+
"""
284+
logging.info(f"Listing open PRs for {repo_owner}")
285+
286+
# Construct the search URL
287+
search_url = f"https://api.github.com/search/issues?q=is:pr+is:open+user:{repo_owner}"
288+
289+
try:
290+
response = requests.get(search_url, headers=self._get_headers())
291+
response.raise_for_status()
292+
pr_data = response.json()
293+
open_prs = {
294+
"total_open_prs": pr_data['total_count'],
295+
"pull_requests": [
296+
{
297+
"url": item['url'],
298+
"title": item['title'],
299+
"number": item['number'],
300+
"state": item['state'],
301+
"created_at": item['created_at'],
302+
"label_names": [label['name'] for label in item.get('labels', [])]
303+
}
304+
for item in pr_data['items']
305+
]
306+
}
307+
308+
logging.info(f"Open PRs listed successfully")
309+
return open_prs
310+
311+
except Exception as e:
312+
logging.error(f"Error listing open PRs: {str(e)}")
313+
traceback.print_exc()
314+
return None
315+
273316
def create_issue(self, repo_owner: str, repo_name: str, title: str, body: str, labels: list[str]) -> Dict[str, Any]:
274317
"""
275318
Creates a new issue in the specified GitHub repository.

src/mcp_github/issues_pr_analyser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,27 @@ async def add_github_pr_comment(repo_owner: str, repo_name: str, pr_number: int,
222222
traceback.print_exc(file=sys.stderr)
223223
return error_msg
224224

225+
@self.mcp.tool()
226+
async def list_github_prs(repo_owner: str) -> str:
227+
"""
228+
Lists all open pull requests for a given repository owner.
229+
Args:
230+
repo_owner (str): The owner of the repository.
231+
Returns:
232+
Dict[str, Any]: A dictionary containing the list of open pull requests.
233+
Error Handling:
234+
Catches and logs any exceptions that occur during the fetch operation. If an error is encountered, the error message is logged and an empty dictionary is returned.
235+
"""
236+
logging.info(f"Listing open PRs for {repo_owner}")
237+
try:
238+
open_prs = self.gi.list_open_prs(repo_owner)
239+
return f"Successfully listed open PRs for {repo_owner}: {open_prs}"
240+
except Exception as e:
241+
error_msg = f"Error adding comment to PR: {str(e)}"
242+
logging.error(error_msg)
243+
traceback.print_exc(file=sys.stderr)
244+
return error_msg
245+
225246
@self.mcp.tool()
226247
async def create_github_issue(repo_owner: str, repo_name: str, title: str, body: str, labels: list[str]) -> str:
227248
"""

0 commit comments

Comments
 (0)