Compare commits
26 Commits
issue-43-a
...
main
Author | SHA1 | Date | |
---|---|---|---|
bd5788ecae | |||
a8ce6102d2 | |||
236d1c0a10 | |||
7a35029a18 | |||
7a73a1e3fc | |||
54cddfde0b | |||
b9013b7b2a | |||
6db1cccaf8 | |||
04b3baaba2 | |||
cd9e32e4b0 | |||
bff022b806 | |||
c524891168 | |||
e56463d207 | |||
f253235841 | |||
a4f7caf125 | |||
7b500c3f2e | |||
232622309f | |||
8b35ea9cad | |||
948ab5a382 | |||
890dada71c | |||
3458826f54 | |||
3e3e6591d7 | |||
d10218f0c8 | |||
e9a0719eb2 | |||
f306faab16 | |||
727b788d01 |
|
@ -80,6 +80,7 @@ from pathlib import Path
|
||||||
|
|
||||||
from . import secrets
|
from . import secrets
|
||||||
from ._version import __version__ # noqa: F401
|
from ._version import __version__ # noqa: F401
|
||||||
|
from .seen_issues_db import SeenIssuesDB
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -102,7 +103,11 @@ class RepositoryConfig:
|
||||||
class IssueResolution:
|
class IssueResolution:
|
||||||
success: bool
|
success: bool
|
||||||
pull_request_url: str | None = None
|
pull_request_url: str | None = None
|
||||||
pull_request_id: str | None = None
|
pull_request_id: int | None = None
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
assert self.pull_request_id is None or isinstance(self.pull_request_id, int)
|
||||||
|
assert self.pull_request_url is None or isinstance(self.pull_request_url, str)
|
||||||
|
|
||||||
|
|
||||||
def generate_branch_name(issue_number: str, issue_title: str) -> str:
|
def generate_branch_name(issue_number: str, issue_title: str) -> str:
|
||||||
|
@ -149,19 +154,13 @@ AIDER_LINT = bash_cmd(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
LLM_MESSAGE_FORMAT = """
|
LLM_MESSAGE_FORMAT = (
|
||||||
{issue}
|
"""{issue}\nDo not wait for explicit approval before working on code changes."""
|
||||||
|
)
|
||||||
|
|
||||||
# Solution Details
|
# CODE_MODEL = 'ollama/gemma3:4b'
|
||||||
|
CODE_MODEL = 'o4-mini'
|
||||||
For code tasks:
|
EVALUATOR_MODEL = 'ollama/gemma3:27b'
|
||||||
|
|
||||||
1. Create a plan for how to solve the issue.
|
|
||||||
2. Write unit tests that proves that your solution works.
|
|
||||||
3. Then, solve the issue by writing the required code.
|
|
||||||
"""
|
|
||||||
|
|
||||||
MODEL = None
|
|
||||||
|
|
||||||
|
|
||||||
def create_aider_command(issue: str) -> list[str]:
|
def create_aider_command(issue: str) -> list[str]:
|
||||||
|
@ -171,31 +170,39 @@ def create_aider_command(issue: str) -> list[str]:
|
||||||
'english',
|
'english',
|
||||||
'--no-stream',
|
'--no-stream',
|
||||||
'--no-analytics',
|
'--no-analytics',
|
||||||
|
#'--no-check-update',
|
||||||
'--test-cmd',
|
'--test-cmd',
|
||||||
AIDER_TEST,
|
AIDER_TEST,
|
||||||
'--lint-cmd',
|
'--lint-cmd',
|
||||||
AIDER_LINT,
|
AIDER_LINT,
|
||||||
'--auto-test',
|
'--auto-test',
|
||||||
'--no-auto-lint',
|
'--no-auto-lint',
|
||||||
'--read',
|
|
||||||
'CONVENTIONS.md',
|
|
||||||
'--message',
|
|
||||||
LLM_MESSAGE_FORMAT.format(issue=issue),
|
|
||||||
'--yes',
|
'--yes',
|
||||||
]
|
]
|
||||||
|
|
||||||
for key in secrets.llm_api_keys():
|
for key in secrets.llm_api_keys():
|
||||||
l += ['--api-key', key]
|
l += ['--api-key', key]
|
||||||
|
|
||||||
|
if False:
|
||||||
|
l.append('--read')
|
||||||
|
l.append('CONVENTIONS.md')
|
||||||
|
|
||||||
if True:
|
if True:
|
||||||
l.append('--cache-prompts')
|
l.append('--cache-prompts')
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
l.append('--architect')
|
l.append('--architect')
|
||||||
|
|
||||||
if MODEL:
|
if CODE_MODEL:
|
||||||
l.append('--model')
|
l.append('--model')
|
||||||
l.append(MODEL)
|
l.append(CODE_MODEL)
|
||||||
|
|
||||||
|
if CODE_MODEL.startswith('ollama/'):
|
||||||
|
l.append('--auto-lint')
|
||||||
|
|
||||||
|
if True:
|
||||||
|
l.append('--message')
|
||||||
|
l.append(LLM_MESSAGE_FORMAT.format(issue=issue))
|
||||||
|
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
@ -225,6 +232,17 @@ def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> lis
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def get_diff(cwd: Path, base_branch: str, current_branch: str) -> str:
|
||||||
|
result = subprocess.run(
|
||||||
|
['git', 'diff', f'{base_branch}..{current_branch}', '--pretty=format:%s'],
|
||||||
|
check=True,
|
||||||
|
cwd=cwd,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
return result.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
def push_changes(
|
def push_changes(
|
||||||
repository_config: RepositoryConfig,
|
repository_config: RepositoryConfig,
|
||||||
cwd: Path,
|
cwd: Path,
|
||||||
|
@ -269,8 +287,8 @@ def push_changes(
|
||||||
# Extract PR number and URL if available
|
# Extract PR number and URL if available
|
||||||
return IssueResolution(
|
return IssueResolution(
|
||||||
True,
|
True,
|
||||||
str(pr_response.get('number')),
|
|
||||||
pr_response.get('html_url'),
|
pr_response.get('html_url'),
|
||||||
|
int(pr_response.get('number')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,12 +326,103 @@ def run_cmd(cmd: list[str], cwd: Path | None = None, check=True) -> bool:
|
||||||
return result.returncode == 0
|
return result.returncode == 0
|
||||||
|
|
||||||
|
|
||||||
SKIP_AIDER = False
|
def issue_solution_round(repository_path, issue_content):
|
||||||
|
# Primary Aider command
|
||||||
|
aider_command = create_aider_command(issue_content)
|
||||||
|
print(aider_command)
|
||||||
|
aider_did_not_crash = run_cmd(
|
||||||
|
aider_command,
|
||||||
|
repository_path,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
if not aider_did_not_crash:
|
||||||
|
return aider_did_not_crash
|
||||||
|
|
||||||
|
# Auto-fix standard code quality stuff after aider
|
||||||
|
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||||
|
run_cmd(['git', 'add', '.'], repository_path)
|
||||||
|
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], repository_path, check=False)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def run_ollama(cwd: Path, texts: list[str]) -> str:
|
||||||
|
cmd = ['ollama', 'run', EVALUATOR_MODEL.removeprefix('ollama/')]
|
||||||
|
print(cmd)
|
||||||
|
process = subprocess.Popen(
|
||||||
|
cmd,
|
||||||
|
cwd=cwd,
|
||||||
|
stdin=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
stdout, stderr = process.communicate('\n'.join(texts))
|
||||||
|
print(stdout)
|
||||||
|
return stdout
|
||||||
|
|
||||||
|
|
||||||
|
def parse_yes_no_answer(text: str) -> bool | None:
|
||||||
|
text = text.lower().strip()
|
||||||
|
words = text.split('\n \t.,?-')
|
||||||
|
print(words)
|
||||||
|
if words[-1] in {'yes', 'agree'}:
|
||||||
|
return True
|
||||||
|
if words[-1] in {'no', 'disagree'}:
|
||||||
|
return False
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def run_ollama_and_get_yes_or_no(cwd, initial_texts: list[str]) -> bool:
|
||||||
|
texts = list(initial_texts)
|
||||||
|
texts.append('Think through your answer.')
|
||||||
|
while True:
|
||||||
|
response = run_ollama(cwd, texts)
|
||||||
|
yes_or_no = parse_yes_no_answer(response)
|
||||||
|
if yes_or_no is not None:
|
||||||
|
return yes_or_no
|
||||||
|
else:
|
||||||
|
texts.append(response)
|
||||||
|
texts.append('Please answer either "yes" or "no".')
|
||||||
|
|
||||||
|
|
||||||
|
def verify_solution(repository_path: Path, issue_content: str) -> bool:
|
||||||
|
if not EVALUATOR_MODEL:
|
||||||
|
return True
|
||||||
|
|
||||||
|
summary = run_ollama(
|
||||||
|
repository_path,
|
||||||
|
[
|
||||||
|
'Concisely summarize following changeset',
|
||||||
|
get_diff(repository_path, 'main', 'HEAD'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
return run_ollama_and_get_yes_or_no(
|
||||||
|
repository_path,
|
||||||
|
[
|
||||||
|
'Does this changeset accomplish the entire task?',
|
||||||
|
'# Change set',
|
||||||
|
summary,
|
||||||
|
'# Issue',
|
||||||
|
issue_content,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_head_commit_hash(repository_path: Path) -> str:
|
||||||
|
return subprocess.run(
|
||||||
|
['git', 'rev-parse', 'HEAD'],
|
||||||
|
check=True,
|
||||||
|
cwd=repository_path,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
).stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
def solve_issue_in_repository(
|
def solve_issue_in_repository(
|
||||||
repository_config: RepositoryConfig,
|
repository_config: RepositoryConfig,
|
||||||
tmpdirname: Path,
|
repository_path: Path,
|
||||||
branch_name: str,
|
branch_name: str,
|
||||||
issue_title: str,
|
issue_title: str,
|
||||||
issue_description: str,
|
issue_description: str,
|
||||||
|
@ -323,72 +432,52 @@ def solve_issue_in_repository(
|
||||||
logger.info('### %s #####', issue_title)
|
logger.info('### %s #####', issue_title)
|
||||||
|
|
||||||
# Setup repository
|
# Setup repository
|
||||||
run_cmd(['git', 'clone', repository_config.repo_url(), tmpdirname])
|
run_cmd(['git', 'clone', repository_config.repo_url(), repository_path])
|
||||||
run_cmd(['bash', '-c', AIDER_TEST], tmpdirname)
|
run_cmd(['bash', '-c', AIDER_TEST], repository_path)
|
||||||
run_cmd(['git', 'checkout', repository_config.base_branch], tmpdirname)
|
run_cmd(['git', 'checkout', repository_config.base_branch], repository_path)
|
||||||
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
run_cmd(['git', 'checkout', '-b', branch_name], repository_path)
|
||||||
|
|
||||||
# Run initial ruff pass before aider
|
# Run initial ruff pass before aider
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||||
run_cmd(['git', 'add', '.'], tmpdirname)
|
run_cmd(['git', 'add', '.'], repository_path)
|
||||||
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], tmpdirname, check=False)
|
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], repository_path, check=False)
|
||||||
|
|
||||||
# Save the commit hash after ruff but before aider
|
|
||||||
result = subprocess.run(
|
|
||||||
['git', 'rev-parse', 'HEAD'],
|
|
||||||
check=True,
|
|
||||||
cwd=tmpdirname,
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
pre_aider_commit = result.stdout.strip()
|
|
||||||
|
|
||||||
# Run aider
|
# Run aider
|
||||||
issue_content = f'# {issue_title}\n{issue_description}'
|
issue_content = f'# {issue_title}\n{issue_description}'
|
||||||
if not SKIP_AIDER:
|
|
||||||
succeeded = run_cmd(
|
|
||||||
create_aider_command(issue_content),
|
|
||||||
tmpdirname,
|
|
||||||
check=False,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
logger.warning('Skipping aider command (for testing)')
|
|
||||||
succeeded = True
|
|
||||||
if not succeeded:
|
|
||||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
|
||||||
return IssueResolution(False)
|
|
||||||
|
|
||||||
# Auto-fix standard code quality stuff after aider
|
while True:
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
# Save the commit hash after ruff but before aider
|
||||||
run_cmd(['git', 'add', '.'], tmpdirname)
|
pre_aider_commit = get_head_commit_hash(repository_path)
|
||||||
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], tmpdirname, check=False)
|
|
||||||
|
|
||||||
# Check if aider made any changes beyond the initial ruff pass
|
# Run aider
|
||||||
result = subprocess.run(
|
aider_did_not_crash = issue_solution_round(repository_path, issue_content)
|
||||||
['git', 'diff', pre_aider_commit, 'HEAD', '--name-only'],
|
if not aider_did_not_crash:
|
||||||
check=True,
|
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||||
cwd=tmpdirname,
|
return IssueResolution(False)
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
files_changed = result.stdout.strip()
|
|
||||||
|
|
||||||
if not files_changed and not SKIP_AIDER:
|
# Check if aider made any changes beyond the initial ruff pass
|
||||||
logger.info(
|
if not has_commits_on_branch(repository_path, pre_aider_commit, 'HEAD'):
|
||||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
logger.error(
|
||||||
|
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||||
|
issue_number,
|
||||||
|
)
|
||||||
|
return IssueResolution(False)
|
||||||
|
|
||||||
|
# Push changes and create/update the pull request on every iteration
|
||||||
|
resolution = push_changes(
|
||||||
|
repository_config,
|
||||||
|
repository_path,
|
||||||
|
branch_name,
|
||||||
issue_number,
|
issue_number,
|
||||||
|
issue_title,
|
||||||
|
gitea_client,
|
||||||
)
|
)
|
||||||
return IssueResolution(False)
|
if not resolution.success:
|
||||||
|
return resolution
|
||||||
|
|
||||||
# Push changes
|
# Verify whether this is a satisfactory solution
|
||||||
return push_changes(
|
if verify_solution(repository_path, issue_content):
|
||||||
repository_config,
|
return resolution
|
||||||
tmpdirname,
|
|
||||||
branch_name,
|
|
||||||
issue_number,
|
|
||||||
issue_title,
|
|
||||||
gitea_client,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def solve_issues_in_repository(
|
def solve_issues_in_repository(
|
||||||
|
@ -420,21 +509,40 @@ def solve_issues_in_repository(
|
||||||
title = issue.get('title', f'Issue {issue_number}')
|
title = issue.get('title', f'Issue {issue_number}')
|
||||||
if seen_issues_db.has_seen(issue_url):
|
if seen_issues_db.has_seen(issue_url):
|
||||||
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
|
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
|
||||||
continue
|
else:
|
||||||
|
branch_name = generate_branch_name(issue_number, title)
|
||||||
|
with tempfile.TemporaryDirectory() as repository_path:
|
||||||
|
issue_resolution = solve_issue_in_repository(
|
||||||
|
repository_config,
|
||||||
|
Path(repository_path),
|
||||||
|
branch_name,
|
||||||
|
title,
|
||||||
|
issue_description,
|
||||||
|
issue_number,
|
||||||
|
client,
|
||||||
|
)
|
||||||
|
|
||||||
branch_name = generate_branch_name(issue_number, title)
|
# TODO: PR comment handling disabled for now due to missing functionality
|
||||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
if False:
|
||||||
issue_resolution = solve_issue_in_repository(
|
# Handle unresolved pull request comments
|
||||||
|
handle_pr_comments(
|
||||||
repository_config,
|
repository_config,
|
||||||
Path(tmpdirname),
|
issue_resolution.pull_request_id,
|
||||||
branch_name,
|
branch_name,
|
||||||
title,
|
Path(repository_path),
|
||||||
issue_description,
|
|
||||||
issue_number,
|
|
||||||
client,
|
client,
|
||||||
|
seen_issues_db,
|
||||||
|
issue_url,
|
||||||
)
|
)
|
||||||
|
|
||||||
if issue_resolution.success:
|
# Handle failing pipelines
|
||||||
|
handle_failing_pipelines(
|
||||||
|
repository_config,
|
||||||
|
issue_resolution.pull_request_id,
|
||||||
|
branch_name,
|
||||||
|
Path(repository_path),
|
||||||
|
client,
|
||||||
|
)
|
||||||
seen_issues_db.mark_as_seen(issue_url, str(issue_number))
|
seen_issues_db.mark_as_seen(issue_url, str(issue_number))
|
||||||
seen_issues_db.update_pr_info(
|
seen_issues_db.update_pr_info(
|
||||||
issue_url,
|
issue_url,
|
||||||
|
@ -446,3 +554,83 @@ def solve_issues_in_repository(
|
||||||
issue_resolution.pull_request_id,
|
issue_resolution.pull_request_id,
|
||||||
issue_number,
|
issue_number,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_pr_comments(
|
||||||
|
repository_config,
|
||||||
|
pr_number: int,
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_failing_pipelines(
|
||||||
|
repository_config: RepositoryConfig,
|
||||||
|
pr_number: str,
|
||||||
|
branch_name: str,
|
||||||
|
repository_path: Path,
|
||||||
|
client,
|
||||||
|
) -> None:
|
||||||
|
"""Fetch failing pipelines for the given PR and resolve them via aider."""
|
||||||
|
while True:
|
||||||
|
failed_runs = client.get_failed_pipelines(
|
||||||
|
repository_config.owner,
|
||||||
|
repository_config.repo,
|
||||||
|
pr_number,
|
||||||
|
)
|
||||||
|
if not failed_runs:
|
||||||
|
break
|
||||||
|
for run_id in failed_runs:
|
||||||
|
log = client.get_pipeline_log(
|
||||||
|
repository_config.owner,
|
||||||
|
repository_config.repo,
|
||||||
|
run_id,
|
||||||
|
)
|
||||||
|
lines = log.strip().split('\n')
|
||||||
|
context = '\n'.join(lines[-100:])
|
||||||
|
issue = f'Resolve the following failing pipeline run {run_id}:\n\n{context}'
|
||||||
|
issue_solution_round(repository_path, issue)
|
||||||
|
run_cmd(['git', 'add', '.'], repository_path, check=False)
|
||||||
|
run_cmd(
|
||||||
|
['git', 'commit', '-m', f'Resolve pipeline {run_id}'],
|
||||||
|
repository_path,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
run_cmd(
|
||||||
|
['git', 'push', 'origin', branch_name], repository_path, check=False,
|
||||||
|
)
|
||||||
|
|
|
@ -45,6 +45,16 @@ def parse_args():
|
||||||
default=300,
|
default=300,
|
||||||
help='Interval in seconds between checks in daemon mode (default: 300)',
|
help='Interval in seconds between checks in daemon mode (default: 300)',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--aider-model',
|
||||||
|
help='Model to use for generating code (overrides default)',
|
||||||
|
default=None,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--evaluator-model',
|
||||||
|
help='Model to use for evaluating code (overrides default)',
|
||||||
|
default=None,
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,6 +62,14 @@ def main():
|
||||||
logging.basicConfig(level='INFO')
|
logging.basicConfig(level='INFO')
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
|
# Override default models if provided
|
||||||
|
import aider_gitea as core
|
||||||
|
|
||||||
|
if args.aider_model:
|
||||||
|
core.CODE_MODEL = args.aider_model
|
||||||
|
if args.evaluator_model:
|
||||||
|
core.EVALUATOR_MODEL = args.evaluator_model
|
||||||
|
|
||||||
seen_issues_db = SeenIssuesDB()
|
seen_issues_db = SeenIssuesDB()
|
||||||
client = GiteaClient(args.gitea_url, secrets.gitea_token())
|
client = GiteaClient(args.gitea_url, secrets.gitea_token())
|
||||||
|
|
||||||
|
|
|
@ -166,5 +166,56 @@ class GiteaClient:
|
||||||
}
|
}
|
||||||
|
|
||||||
response = self.session.post(url, json=json_data)
|
response = self.session.post(url, json=json_data)
|
||||||
|
# If a pull request for this head/base already exists, return it instead of crashing
|
||||||
|
if response.status_code == 409:
|
||||||
|
logger.warning(
|
||||||
|
'Pull request already exists for head %s and base %s',
|
||||||
|
head,
|
||||||
|
base,
|
||||||
|
)
|
||||||
|
prs = self.get_pull_requests(owner, repo)
|
||||||
|
for pr in prs:
|
||||||
|
if (
|
||||||
|
pr.get('head', {}).get('ref') == head
|
||||||
|
and pr.get('base', {}).get('ref') == base
|
||||||
|
):
|
||||||
|
return pr
|
||||||
|
# fallback to raise if we can’t find it
|
||||||
|
response.raise_for_status()
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def get_failed_pipelines(self, owner: str, repo: str, pr_number: str) -> list[int]:
|
||||||
|
"""Fetch pipeline runs for a PR and return IDs of failed runs."""
|
||||||
|
url = f'{self.gitea_url}/repos/{owner}/{repo}/actions/runs'
|
||||||
|
response = self.session.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
runs = response.json().get('workflow_runs', [])
|
||||||
|
failed = []
|
||||||
|
for run in runs:
|
||||||
|
if any(
|
||||||
|
pr.get('number') == int(pr_number)
|
||||||
|
for pr in run.get('pull_requests', [])
|
||||||
|
):
|
||||||
|
if run.get('conclusion') not in ('success',):
|
||||||
|
failed.append(run.get('id'))
|
||||||
|
return failed
|
||||||
|
|
||||||
|
def get_pipeline_log(self, owner: str, repo: str, run_id: int) -> str:
|
||||||
|
"""Download the logs for a pipeline run."""
|
||||||
|
url = f'{self.gitea_url}/repos/{owner}/{repo}/actions/runs/{run_id}/logs'
|
||||||
|
response = self.session.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.text
|
||||||
|
|
||||||
|
def get_pull_requests(
|
||||||
|
self,
|
||||||
|
owner: str,
|
||||||
|
repo: str,
|
||||||
|
state: str = 'open',
|
||||||
|
) -> list[dict]:
|
||||||
|
"""Fetch pull requests for a repository."""
|
||||||
|
url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls?state={state}'
|
||||||
|
response = self.session.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
|
@ -46,6 +46,13 @@ class SeenIssuesDB:
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
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(
|
def mark_as_seen(
|
||||||
self,
|
self,
|
||||||
|
|
|
@ -1,101 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
|
|
||||||
from aider_gitea import IssueResolution, RepositoryConfig, solve_issue_in_repository
|
|
||||||
|
|
||||||
REPOSITORY_CONFIG = RepositoryConfig(
|
|
||||||
gitea_url='https://gitea.example.com',
|
|
||||||
owner='test-owner',
|
|
||||||
repo='test-repo',
|
|
||||||
base_branch='main',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestSolveIssueInRepository:
|
|
||||||
def setup_method(self):
|
|
||||||
self.gitea_client = MagicMock()
|
|
||||||
self.tmpdirname = Path('/tmp/test-repo')
|
|
||||||
self.branch_name = 'issue-123-test-branch'
|
|
||||||
self.issue_title = 'Test Issue'
|
|
||||||
self.issue_description = 'This is a test issue'
|
|
||||||
self.issue_number = '123'
|
|
||||||
|
|
||||||
@patch('aider_gitea.secrets.llm_api_keys', return_value='fake-api-key')
|
|
||||||
@patch('aider_gitea.run_cmd')
|
|
||||||
@patch('aider_gitea.push_changes')
|
|
||||||
@patch('subprocess.run')
|
|
||||||
def test_solve_issue_with_aider_changes(
|
|
||||||
self,
|
|
||||||
mock_subprocess_run,
|
|
||||||
mock_push_changes,
|
|
||||||
mock_run_cmd,
|
|
||||||
mock_llm_api_key,
|
|
||||||
):
|
|
||||||
# Setup mocks
|
|
||||||
mock_run_cmd.return_value = True
|
|
||||||
mock_push_changes.return_value = IssueResolution(
|
|
||||||
True,
|
|
||||||
'456',
|
|
||||||
'https://gitea.example.com/test-owner/test-repo/pulls/456',
|
|
||||||
)
|
|
||||||
|
|
||||||
# Mock subprocess.run to return different commit hashes and file changes
|
|
||||||
mock_subprocess_run.side_effect = [
|
|
||||||
MagicMock(stdout='abc123\n', returncode=0), # First git rev-parse
|
|
||||||
MagicMock(
|
|
||||||
stdout='file1.py\nfile2.py\n',
|
|
||||||
returncode=0,
|
|
||||||
), # git diff with changes
|
|
||||||
]
|
|
||||||
|
|
||||||
# Call the function
|
|
||||||
result = solve_issue_in_repository(
|
|
||||||
REPOSITORY_CONFIG,
|
|
||||||
self.tmpdirname,
|
|
||||||
self.branch_name,
|
|
||||||
self.issue_title,
|
|
||||||
self.issue_description,
|
|
||||||
self.issue_number,
|
|
||||||
self.gitea_client,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify results
|
|
||||||
assert result.success is True
|
|
||||||
assert mock_run_cmd.call_count >= 8 # Verify all expected commands were run
|
|
||||||
mock_push_changes.assert_called_once()
|
|
||||||
|
|
||||||
@patch('aider_gitea.secrets.llm_api_keys', return_value='fake-api-key')
|
|
||||||
@patch('aider_gitea.run_cmd')
|
|
||||||
@patch('aider_gitea.push_changes')
|
|
||||||
@patch('subprocess.run')
|
|
||||||
def test_solve_issue_without_aider_changes(
|
|
||||||
self,
|
|
||||||
mock_subprocess_run,
|
|
||||||
mock_push_changes,
|
|
||||||
mock_run_cmd,
|
|
||||||
mock_llm_api_key,
|
|
||||||
):
|
|
||||||
# Setup mocks
|
|
||||||
mock_run_cmd.return_value = True
|
|
||||||
mock_push_changes.return_value = IssueResolution(False, None, None)
|
|
||||||
|
|
||||||
# Mock subprocess.run to return same commit hash and no file changes
|
|
||||||
mock_subprocess_run.side_effect = [
|
|
||||||
MagicMock(stdout='abc123\n', returncode=0), # First git rev-parse
|
|
||||||
MagicMock(stdout='', returncode=0), # git diff with no changes
|
|
||||||
]
|
|
||||||
|
|
||||||
# Call the function
|
|
||||||
result = solve_issue_in_repository(
|
|
||||||
REPOSITORY_CONFIG,
|
|
||||||
self.tmpdirname,
|
|
||||||
self.branch_name,
|
|
||||||
self.issue_title,
|
|
||||||
self.issue_description,
|
|
||||||
self.issue_number,
|
|
||||||
self.gitea_client,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify results
|
|
||||||
assert result.success is False
|
|
||||||
assert mock_push_changes.call_count == 0 # push_changes should not be called
|
|
Loading…
Reference in New Issue
Block a user