From 7b500c3f2eac13920f63b04e204c9f70ece437af Mon Sep 17 00:00:00 2001 From: "Jon Michael Aanes (aider)" Date: Mon, 21 Apr 2025 16:09:04 +0200 Subject: [PATCH 1/4] feat: add handling of unresolved PR comments with context and auto-resolve via Aider --- aider_gitea/__init__.py | 11 +++++++++++ aider_gitea/gitea_client.py | 9 +++++++++ aider_gitea/seen_issues_db.py | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index 5b84aed..c77eb63 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -78,6 +78,8 @@ import sys import tempfile from pathlib import Path +from .seen_issues_db import SeenIssuesDB + from . import secrets from ._version import __version__ # noqa: F401 @@ -516,6 +518,15 @@ def solve_issues_in_repository( ) if issue_resolution.success: + # Handle unresolved pull request comments + handle_pr_comments( + repository_config, + issue_resolution.pull_request_id, + Path(repository_path), + client, + seen_issues_db, + issue_url, + ) seen_issues_db.mark_as_seen(issue_url, str(issue_number)) seen_issues_db.update_pr_info( issue_url, diff --git a/aider_gitea/gitea_client.py b/aider_gitea/gitea_client.py index d392cab..f9a91a5 100644 --- a/aider_gitea/gitea_client.py +++ b/aider_gitea/gitea_client.py @@ -168,3 +168,12 @@ class GiteaClient: response = self.session.post(url, json=json_data) response.raise_for_status() return response.json() + + def get_pull_request_comments(self, owner: str, repo: str, pr_number: str) -> list[dict]: + """ + Fetch comments for a pull request. + """ + url = f"{self.gitea_url}/repos/{owner}/{repo}/pulls/{pr_number}/comments" + response = self.session.get(url) + response.raise_for_status() + return response.json() diff --git a/aider_gitea/seen_issues_db.py b/aider_gitea/seen_issues_db.py index 0a34383..9bf9f6c 100644 --- a/aider_gitea/seen_issues_db.py +++ b/aider_gitea/seen_issues_db.py @@ -46,6 +46,13 @@ class SeenIssuesDB: created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """) + self.conn.execute(""" + CREATE TABLE IF NOT EXISTS resolved_comments ( + issue_url TEXT, + comment_id TEXT, + PRIMARY KEY(issue_url, comment_id) + ) + """) def mark_as_seen( self, -- 2.45.1 From a4f7caf125886613ca58f73c26cbe483a4eb8e7f Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 21 Apr 2025 16:10:29 +0200 Subject: [PATCH 2/4] Ruff after aider --- aider_gitea/__init__.py | 3 +-- aider_gitea/gitea_client.py | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index c77eb63..7da08c6 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -78,10 +78,9 @@ import sys import tempfile from pathlib import Path -from .seen_issues_db import SeenIssuesDB - from . import secrets from ._version import __version__ # noqa: F401 +from .seen_issues_db import SeenIssuesDB logger = logging.getLogger(__name__) diff --git a/aider_gitea/gitea_client.py b/aider_gitea/gitea_client.py index f9a91a5..e486d13 100644 --- a/aider_gitea/gitea_client.py +++ b/aider_gitea/gitea_client.py @@ -169,11 +169,13 @@ class GiteaClient: response.raise_for_status() return response.json() - def get_pull_request_comments(self, owner: str, repo: str, pr_number: str) -> list[dict]: + def get_pull_request_comments( + self, owner: str, repo: str, pr_number: str, + ) -> list[dict]: """ Fetch comments for a pull request. """ - url = f"{self.gitea_url}/repos/{owner}/{repo}/pulls/{pr_number}/comments" + url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls/{pr_number}/comments' response = self.session.get(url) response.raise_for_status() return response.json() -- 2.45.1 From f2532358415a667bf895c54276401cd173d2ef31 Mon Sep 17 00:00:00 2001 From: "Jon Michael Aanes (aider)" Date: Mon, 21 Apr 2025 16:19:00 +0200 Subject: [PATCH 3/4] feat: add handle_pr_comments to resolve and push PR review comments automatically --- aider_gitea/__init__.py | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index 7da08c6..974d64c 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -521,6 +521,7 @@ def solve_issues_in_repository( handle_pr_comments( repository_config, issue_resolution.pull_request_id, + branch_name, Path(repository_path), client, seen_issues_db, @@ -537,3 +538,45 @@ def solve_issues_in_repository( issue_resolution.pull_request_id, issue_number, ) + +def handle_pr_comments( + repository_config, + pr_number, + branch_name, + repository_path, + client, + seen_issues_db, + issue_url, +): + """Fetch unresolved PR comments and resolve them via aider.""" + comments = client.get_pull_request_comments( + repository_config.owner, + repository_config.repo, + pr_number, + ) + for comment in comments: + path = comment.get('path') + line = comment.get('line') or comment.get('position') or 0 + file_path = repository_path / path + try: + lines = file_path.read_text().splitlines() + start = max(0, line - 3) + end = min(len(lines), line + 2) + context = '\n'.join(lines[start:end]) + except Exception: + context = '' + body = comment.get('body', '') + issue = ( + f"Resolve the following reviewer comment:\n{body}\n\n" + f"File: {path}\n\nContext:\n{context}" + ) + # invoke aider on the comment context + issue_solution_round(repository_path, issue) + # commit and push changes for this comment + run_cmd(['git', 'add', path], repository_path, check=False) + run_cmd( + ['git', 'commit', '-m', f"Resolve comment {comment.get('id')}"], + repository_path, + check=False, + ) + run_cmd(['git', 'push', 'origin', branch_name], repository_path, check=False) -- 2.45.1 From e56463d20710dcc85761fb8dbe620db7eb65121b Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 21 Apr 2025 16:19:04 +0200 Subject: [PATCH 4/4] Ruff after aider --- aider_gitea/__init__.py | 7 ++++--- aider_gitea/gitea_client.py | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index 974d64c..4f356a5 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -539,6 +539,7 @@ def solve_issues_in_repository( issue_number, ) + def handle_pr_comments( repository_config, pr_number, @@ -567,15 +568,15 @@ def handle_pr_comments( context = '' body = comment.get('body', '') issue = ( - f"Resolve the following reviewer comment:\n{body}\n\n" - f"File: {path}\n\nContext:\n{context}" + f'Resolve the following reviewer comment:\n{body}\n\n' + f'File: {path}\n\nContext:\n{context}' ) # invoke aider on the comment context issue_solution_round(repository_path, issue) # commit and push changes for this comment run_cmd(['git', 'add', path], repository_path, check=False) run_cmd( - ['git', 'commit', '-m', f"Resolve comment {comment.get('id')}"], + ['git', 'commit', '-m', f'Resolve comment {comment.get("id")}'], repository_path, check=False, ) diff --git a/aider_gitea/gitea_client.py b/aider_gitea/gitea_client.py index e486d13..e239af1 100644 --- a/aider_gitea/gitea_client.py +++ b/aider_gitea/gitea_client.py @@ -170,7 +170,10 @@ class GiteaClient: return response.json() def get_pull_request_comments( - self, owner: str, repo: str, pr_number: str, + self, + owner: str, + repo: str, + pr_number: str, ) -> list[dict]: """ Fetch comments for a pull request. -- 2.45.1