Compare commits

..

1 Commits

10 changed files with 70 additions and 218 deletions

View File

@ -1,7 +1,3 @@
# WARNING!
# THIS IS AN AUTOGENERATED FILE!
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN!
name: Build Python Container
on:
push:

View File

@ -1,7 +1,3 @@
# WARNING!
# THIS IS AN AUTOGENERATED FILE!
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN!
name: Package Python
on:
push:

View File

@ -1,7 +1,3 @@
# WARNING!
# THIS IS AN AUTOGENERATED FILE!
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN!
name: Run Python tests (through Pytest)
on:

View File

@ -1,7 +1,3 @@
# WARNING!
# THIS IS AN AUTOGENERATED FILE!
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN!
name: Verify Python project can be installed, loaded and have version checked
on:

View File

@ -1,7 +1,3 @@
<!-- WARNING! -->
<!-- THIS IS AN AUTOGENERATED FILE! -->
<!-- MANUAL CHANGES CAN AND WILL BE OVERWRITTEN! -->
# Conventions
When contributing code to this project, you MUST follow the requirements

View File

@ -1,6 +1,8 @@
<!-- WARNING! -->
<!-- THIS IS AN AUTOGENERATED FILE! -->
<!-- MANUAL CHANGES CAN AND WILL BE OVERWRITTEN! -->
<!--- WARNING --->
<!--- THIS IS AN AUTO-GENERATED FILE --->
<!--- MANUAL CHANGES CAN AND WILL BE OVERWRITTEN --->
# Aider Gitea
@ -16,9 +18,6 @@ When such an issue is found, it:
3. Runs tests and code quality checks.
4. Creates a pull request with the solution.
Inspired by [the AI workflows](https://github.com/oscoreio/ai-workflows/)
project.
## Usage
An application token must be supplied for the `gitea_token` secret. This must
@ -88,10 +87,12 @@ pip install -r requirements.txt
Full list of requirements:
- [secret_loader](https://gitfub.space/Jmaa/secret_loader)
## Contributing
Feel free to submit pull requests. Please follow the [Code Conventions](CONVENTIONS.md) when doing so.
### Testing
Testing requires the [pytest](https://docs.pytest.org/en/stable/) library.
@ -108,6 +109,7 @@ Test coverage can be run using the [`pytest-cov`](https://pypi.org/project/pytes
pytest --cov=aider_gitea test
```
## License
```

View File

@ -103,11 +103,7 @@ class RepositoryConfig:
class IssueResolution:
success: bool
pull_request_url: 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)
pull_request_id: str | None = None
def generate_branch_name(issue_number: str, issue_title: str) -> str:
@ -134,21 +130,17 @@ def bash_cmd(*commands: str) -> str:
AIDER_TEST = bash_cmd(
'echo "Setting up virtual environment"'
'virtualenv venv',
'echo "Activating virtual environment"'
'source venv/bin/activate',
'echo "Installing package"'
'pip install -e .',
'echo "Testing package"'
'pytest test',
)
RUFF_FORMAT_AND_AUTO_FIX = bash_cmd(
'ruff format --silent',
'ruff check --fix --ignore RUF022 --ignore PGH004 --silent',
'ruff format --silent',
'ruff check --fix --ignore RUF022 --ignore PGH004 --silent',
'ruff format',
'ruff check --fix --ignore RUF022 --ignore PGH004',
'ruff format',
'ruff check --fix --ignore RUF022 --ignore PGH004',
)
AIDER_LINT = bash_cmd(
@ -158,21 +150,14 @@ AIDER_LINT = bash_cmd(
)
LLM_MESSAGE_FORMAT = """{issue}
LLM_MESSAGE_FORMAT = (
"""{issue}\nDo not wait for explicit approval before working on code changes."""
)
Go ahead with the changes you deem appropriate without waiting for explicit approval.
Do not draft changes beforehand; produce changes only once prompted for a specific file.
"""
CODE_MODEL = None
# CODE_MODEL = 'ollama/gemma3:4b'
CODE_MODEL = 'o4-mini'
EVALUATOR_MODEL = 'ollama/gemma3:27b'
MODEL_EDIT_MODES = {
'ollama/qwen3:32b': 'diff',
'ollama/hf.co/unsloth/Qwen3-30B-A3B-GGUF:Q4_K_M': 'diff',
}
def create_aider_command(issue: str) -> list[str]:
l = [
@ -189,16 +174,8 @@ def create_aider_command(issue: str) -> list[str]:
'--auto-test',
'--no-auto-lint',
'--yes',
'--disable-playwright',
'--timeout',
str(10_000),
]
if edit_format := MODEL_EDIT_MODES.get(CODE_MODEL):
l.append('--edit-format')
l.append(edit_format)
del edit_format
for key in secrets.llm_api_keys():
l += ['--api-key', key]
@ -216,7 +193,7 @@ def create_aider_command(issue: str) -> list[str]:
l.append('--model')
l.append(CODE_MODEL)
if CODE_MODEL.startswith('ollama/') and False:
if CODE_MODEL.startswith('ollama/'):
l.append('--auto-lint')
if True:
@ -306,8 +283,8 @@ def push_changes(
# Extract PR number and URL if available
return IssueResolution(
True,
str(pr_response.get('number')),
pr_response.get('html_url'),
int(pr_response.get('number')),
)
@ -348,6 +325,7 @@ def run_cmd(cmd: list[str], cwd: Path | None = None, check=True) -> bool:
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,
@ -363,17 +341,10 @@ def issue_solution_round(repository_path, issue_content):
return True
def remove_thinking_tokens(text: str) -> str:
text = re.sub(r'^\s*<think>.*?</think>', '', text, flags=re.MULTILINE | re.DOTALL)
text = text.strip()
return text
assert remove_thinking_tokens('<think>Hello</think>\nWorld\n') == 'World'
assert remove_thinking_tokens('<think>\nHello\n</think>\nWorld\n') == 'World'
assert remove_thinking_tokens('\n<think>\nHello\n</think>\nWorld\n') == 'World'
def run_ollama(cwd: Path, texts: list[str]) -> str:
cmd = ['ollama', 'run', EVALUATOR_MODEL.removeprefix('ollama/')]
print(cmd)
process = subprocess.Popen(
cmd,
cwd=cwd,
@ -383,22 +354,20 @@ def run_ollama(cwd: Path, texts: list[str]) -> str:
text=True,
)
stdout, stderr = process.communicate('\n'.join(texts))
stdout = remove_thinking_tokens(stdout)
print(stdout)
return stdout
def parse_yes_no_answer(text: str) -> bool | None:
interword = '\n \t.,?-'
text = text.lower().strip(interword)
words = text.split(interword)
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
assert parse_yes_no_answer('Yes.') == True
assert parse_yes_no_answer('no') == False
def run_ollama_and_get_yes_or_no(cwd, initial_texts: list[str]) -> bool:
texts = list(initial_texts)
@ -414,9 +383,6 @@ def run_ollama_and_get_yes_or_no(cwd, initial_texts: list[str]) -> bool:
def verify_solution(repository_path: Path, issue_content: str) -> bool:
if not EVALUATOR_MODEL:
return True
summary = run_ollama(
repository_path,
[
@ -536,18 +502,31 @@ def solve_issues_in_repository(
title = issue.get('title', f'Issue {issue_number}')
if seen_issues_db.has_seen(issue_url):
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
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,
)
continue
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,
)
if issue_resolution.success:
# Handle unresolved pull request comments
handle_pr_comments(
repository_config,
issue_resolution.pull_request_id,
branch_name,
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,
@ -560,32 +539,10 @@ def solve_issues_in_repository(
issue_number,
)
# TODO: PR comment handling disabled for now due to missing functionality
if False:
# Handle unresolved pull request comments
handle_pr_comments(
repository_config,
issue_resolution.pull_request_id,
branch_name,
Path(repository_path),
client,
seen_issues_db,
issue_url,
)
# Handle failing pipelines
handle_failing_pipelines(
repository_config,
issue_resolution.pull_request_id,
branch_name,
Path(repository_path),
client,
)
def handle_pr_comments(
repository_config,
pr_number: int,
pr_number,
branch_name,
repository_path,
client,
@ -624,42 +581,3 @@ def handle_pr_comments(
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,
)

View File

@ -42,19 +42,9 @@ def parse_args():
parser.add_argument(
'--interval',
type=int,
default=30,
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)',
required=True,
)
parser.add_argument(
'--evaluator-model',
help='Model to use for evaluating code (overrides default)',
default=None,
)
return parser.parse_args()
@ -62,12 +52,6 @@ def main():
logging.basicConfig(level='INFO')
args = parse_args()
# Override default models if provided
import aider_gitea as core
core.CODE_MODEL = args.aider_model
core.EVALUATOR_MODEL = args.evaluator_model
seen_issues_db = SeenIssuesDB()
client = GiteaClient(args.gitea_url, secrets.gitea_token())

View File

@ -166,56 +166,21 @@ class GiteaClient:
}
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 cant 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(
def get_pull_request_comments(
self,
owner: str,
repo: str,
state: str = 'open',
pr_number: str,
) -> list[dict]:
"""Fetch pull requests for a repository."""
url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls?state={state}'
"""
Fetch comments for a pull request.
"""
# sanitize pr_number to ensure it's just the numeric ID, not a full URL
pr_number = pr_number.rstrip('/').split('/')[-1]
url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls/{pr_number}/comments'
response = self.session.get(url)
response.raise_for_status()
return response.json()

View File

@ -1,6 +1,8 @@
# WARNING!
# THIS IS AN AUTOGENERATED FILE!
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN!
# WARNING
#
# THIS IS AN AUTOGENERATED FILE.
#
# MANUAL CHANGES CAN AND WILL BE OVERWRITTEN.
import re
@ -21,9 +23,6 @@ When such an issue is found, it:
3. Runs tests and code quality checks.
4. Creates a pull request with the solution.
Inspired by [the AI workflows](https://github.com/oscoreio/ai-workflows/)
project.
## Usage
An application token must be supplied for the `gitea_token` secret. This must
@ -84,6 +83,7 @@ The tool uses environment variables for sensitive information:
PACKAGE_DESCRIPTION_SHORT = """
A code automation tool that integrates Gitea with Aider to automatically solve issues.""".strip()
def parse_version_file(text: str) -> str:
match = re.match(r'^__version__\s*=\s*(["\'])([\d\.]+)\1$', text)
if match is None:
@ -91,14 +91,17 @@ def parse_version_file(text: str) -> str:
raise Exception(msg)
return match.group(2)
with open(PACKAGE_NAME + '/_version.py') as f:
version = parse_version_file(f.read())
REQUIREMENTS_MAIN = [
'secret_loader @ git+https://gitfub.space/Jmaa/secret_loader.git',
]
REQUIREMENTS_TEST = []
setup(
name=PACKAGE_NAME,
version=version,