Compare commits
3 Commits
main
...
issue-41-a
Author | SHA1 | Date | |
---|---|---|---|
1371343ff4 | |||
44104215a2 | |||
0db5ea7214 |
|
@ -70,7 +70,6 @@ The tool uses environment variables for sensitive information:
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import dataclasses
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -84,27 +83,6 @@ from ._version import __version__ # noqa: F401
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
|
||||||
class RepositoryConfig:
|
|
||||||
gitea_url: str
|
|
||||||
owner: str
|
|
||||||
repo: str
|
|
||||||
base_branch: str
|
|
||||||
|
|
||||||
def repo_url(self) -> str:
|
|
||||||
return f'{self.gitea_url}:{self.owner}/{self.repo}.git'.replace(
|
|
||||||
'https://',
|
|
||||||
'git@',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
|
||||||
class IssueResolution:
|
|
||||||
success: bool
|
|
||||||
pull_request_url: str | None = None
|
|
||||||
pull_request_id: str | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def generate_branch_name(issue_number: str, issue_title: str) -> str:
|
def generate_branch_name(issue_number: str, issue_title: str) -> str:
|
||||||
"""Create a branch name by sanitizing the issue title.
|
"""Create a branch name by sanitizing the issue title.
|
||||||
|
|
||||||
|
@ -219,29 +197,29 @@ def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> lis
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
)
|
)
|
||||||
return list(reversed(result.stdout.strip().split('\n')))
|
return reversed(result.stdout.strip().split('\n'))
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
logger.exception(f'Failed to get commit messages on branch {current_branch}')
|
logger.exception(f'Failed to get commit messages on branch {current_branch}')
|
||||||
return []
|
return ''
|
||||||
|
|
||||||
|
|
||||||
def push_changes(
|
def push_changes(
|
||||||
repository_config: RepositoryConfig,
|
|
||||||
cwd: Path,
|
cwd: Path,
|
||||||
branch_name: str,
|
branch_name: str,
|
||||||
issue_number: str,
|
issue_number: str,
|
||||||
issue_title: str,
|
issue_title: str,
|
||||||
|
base_branch: str,
|
||||||
gitea_client,
|
gitea_client,
|
||||||
) -> IssueResolution:
|
owner: str,
|
||||||
|
repo: str,
|
||||||
|
) -> bool:
|
||||||
# Check if there are any commits on the branch before pushing
|
# Check if there are any commits on the branch before pushing
|
||||||
if not has_commits_on_branch(cwd, repository_config.base_branch, branch_name):
|
if not has_commits_on_branch(cwd, base_branch, branch_name):
|
||||||
logger.info('No commits made on branch %s, skipping push', branch_name)
|
logger.info('No commits made on branch %s, skipping push', branch_name)
|
||||||
return IssueResolution(False)
|
return False
|
||||||
|
|
||||||
# Get commit messages for PR description
|
# Get commit messages for PR description
|
||||||
commit_messages = get_commit_messages(
|
commit_messages = get_commit_messages(cwd, base_branch, branch_name)
|
||||||
cwd, repository_config.base_branch, branch_name,
|
|
||||||
)
|
|
||||||
description = f'This pull request resolves #{issue_number}\n\n'
|
description = f'This pull request resolves #{issue_number}\n\n'
|
||||||
|
|
||||||
if commit_messages:
|
if commit_messages:
|
||||||
|
@ -254,20 +232,16 @@ def push_changes(
|
||||||
run_cmd(cmd, cwd)
|
run_cmd(cmd, cwd)
|
||||||
|
|
||||||
# Then create the PR with the aider label
|
# Then create the PR with the aider label
|
||||||
pr_response = gitea_client.create_pull_request(
|
gitea_client.create_pull_request(
|
||||||
owner=repository_config.owner,
|
owner=owner,
|
||||||
repo=repository_config.repo,
|
repo=repo,
|
||||||
title=issue_title,
|
title=issue_title,
|
||||||
body=description,
|
body=description,
|
||||||
head=branch_name,
|
head=branch_name,
|
||||||
base=repository_config.base_branch,
|
base=base_branch,
|
||||||
labels=['aider'],
|
labels=['aider'],
|
||||||
)
|
)
|
||||||
|
return True
|
||||||
# Extract PR number and URL if available
|
|
||||||
return IssueResolution(
|
|
||||||
True, str(pr_response.get('number')), pr_response.get('html_url'),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> bool:
|
def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> bool:
|
||||||
|
@ -282,9 +256,15 @@ def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> b
|
||||||
True if there are commits on the current branch not in the base branch, False otherwise.
|
True if there are commits on the current branch not in the base branch, False otherwise.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
commit_messages = get_commit_messages(cwd, base_branch, current_branch)
|
result = subprocess.run(
|
||||||
return bool(list(commit_messages))
|
['git', 'log', f'{base_branch}..{current_branch}', '--oneline'],
|
||||||
except Exception:
|
check=True,
|
||||||
|
cwd=cwd,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
return bool(result.stdout.strip())
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
logger.exception('Failed to check commits on branch %s', current_branch)
|
logger.exception('Failed to check commits on branch %s', current_branch)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -308,20 +288,25 @@ SKIP_AIDER = False
|
||||||
|
|
||||||
|
|
||||||
def solve_issue_in_repository(
|
def solve_issue_in_repository(
|
||||||
repository_config: RepositoryConfig,
|
args,
|
||||||
tmpdirname: Path,
|
tmpdirname: Path,
|
||||||
branch_name: str,
|
branch_name: str,
|
||||||
issue_title: str,
|
issue_title: str,
|
||||||
issue_description: str,
|
issue_description: str,
|
||||||
issue_number: str,
|
issue_number: str,
|
||||||
gitea_client,
|
gitea_client=None,
|
||||||
) -> IssueResolution:
|
) -> bool:
|
||||||
logger.info('### %s #####', issue_title)
|
logger.info('### %s #####', issue_title)
|
||||||
|
|
||||||
|
repo_url = f'{args.gitea_url}:{args.owner}/{args.repo}.git'.replace(
|
||||||
|
'https://',
|
||||||
|
'git@',
|
||||||
|
)
|
||||||
|
|
||||||
# Setup repository
|
# Setup repository
|
||||||
run_cmd(['git', 'clone', repository_config.repo_url(), tmpdirname])
|
run_cmd(['git', 'clone', repo_url, tmpdirname])
|
||||||
run_cmd(['bash', '-c', AIDER_TEST], tmpdirname)
|
run_cmd(['bash', '-c', AIDER_TEST], tmpdirname)
|
||||||
run_cmd(['git', 'checkout', repository_config.base_branch], tmpdirname)
|
run_cmd(['git', 'checkout', args.base_branch], tmpdirname)
|
||||||
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
||||||
|
|
||||||
# Run initial ruff pass before aider
|
# Run initial ruff pass before aider
|
||||||
|
@ -352,7 +337,7 @@ def solve_issue_in_repository(
|
||||||
succeeded = True
|
succeeded = True
|
||||||
if not succeeded:
|
if not succeeded:
|
||||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||||
return IssueResolution(False)
|
return False
|
||||||
|
|
||||||
# Auto-fix standard code quality stuff after aider
|
# Auto-fix standard code quality stuff after aider
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
||||||
|
@ -374,69 +359,61 @@ def solve_issue_in_repository(
|
||||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||||
issue_number,
|
issue_number,
|
||||||
)
|
)
|
||||||
return IssueResolution(False)
|
return False
|
||||||
|
|
||||||
# Push changes
|
# Push changes
|
||||||
return push_changes(
|
return push_changes(
|
||||||
repository_config,
|
|
||||||
tmpdirname,
|
tmpdirname,
|
||||||
branch_name,
|
branch_name,
|
||||||
issue_number,
|
issue_number,
|
||||||
issue_title,
|
issue_title,
|
||||||
|
args.base_branch,
|
||||||
gitea_client,
|
gitea_client,
|
||||||
|
args.owner,
|
||||||
|
args.repo,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def solve_issues_in_repository(
|
def handle_issues(args, client, seen_issues_db):
|
||||||
repository_config: RepositoryConfig, client, seen_issues_db,
|
|
||||||
):
|
|
||||||
"""Process all open issues with the 'aider' label.
|
"""Process all open issues with the 'aider' label.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
repository_config: Command line arguments.
|
args: Command line arguments.
|
||||||
client: The Gitea client instance.
|
client: The Gitea client instance.
|
||||||
seen_issues_db: Database of previously processed issues.
|
seen_issues_db: Database of previously processed issues.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
issues = client.get_issues(repository_config.owner, repository_config.repo)
|
issues = client.get_issues(args.owner, args.repo)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Failed to retrieve issues')
|
logger.exception('Failed to retrieve issues')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not issues:
|
if not issues:
|
||||||
logger.info('No issues found for %s', repository_config.repo)
|
logger.info('No issues found for %s', args.repo)
|
||||||
return
|
return
|
||||||
|
|
||||||
for issue in issues:
|
for issue in issues:
|
||||||
issue_url = issue.get('web_url')
|
|
||||||
issue_number = issue.get('number')
|
issue_number = issue.get('number')
|
||||||
issue_description = issue.get('body', '')
|
issue_description = issue.get('body', '')
|
||||||
title = issue.get('title', f'Issue {issue_number}')
|
title = issue.get('title', f'Issue {issue_number}')
|
||||||
if seen_issues_db.has_seen(issue_url):
|
issue_text = f'{title}\n{issue_description}'
|
||||||
|
if seen_issues_db.has_seen(issue_text):
|
||||||
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
|
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
branch_name = generate_branch_name(issue_number, title)
|
branch_name = generate_branch_name(issue_number, title)
|
||||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
issue_resolution = solve_issue_in_repository(
|
solved = solve_issue_in_repository(
|
||||||
repository_config,
|
args,
|
||||||
Path(tmpdirname),
|
Path(tmpdirname),
|
||||||
branch_name,
|
branch_name,
|
||||||
title,
|
title,
|
||||||
issue_description,
|
issue_description,
|
||||||
issue_number,
|
issue_number,
|
||||||
client,
|
client,
|
||||||
|
reviewers=args.reviewers,
|
||||||
|
assignees=args.assignees,
|
||||||
)
|
)
|
||||||
|
|
||||||
if issue_resolution.success:
|
if solved:
|
||||||
seen_issues_db.mark_as_seen(issue_url, str(issue_number))
|
seen_issues_db.mark_as_seen(issue_text)
|
||||||
seen_issues_db.update_pr_info(
|
|
||||||
issue_url,
|
|
||||||
issue_resolution.pull_request_id,
|
|
||||||
issue_resolution.pull_request_url,
|
|
||||||
)
|
|
||||||
logger.info(
|
|
||||||
'Stored PR #%s information for issue #%s',
|
|
||||||
issue_resolution.pull_request_id,
|
|
||||||
issue_number,
|
|
||||||
)
|
|
||||||
|
|
|
@ -7,14 +7,25 @@ It assumes that the default branch (default "main") exists and that you have a v
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from . import RepositoryConfig, secrets, solve_issues_in_repository
|
from . import handle_issues, secrets
|
||||||
from .gitea_client import GiteaClient
|
from .gitea_client import GiteaClient
|
||||||
from .seen_issues_db import SeenIssuesDB
|
from .seen_issues_db import SeenIssuesDB
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AiderArgs:
|
||||||
|
gitea_url: str
|
||||||
|
owner: str
|
||||||
|
repo: str
|
||||||
|
base_branch: str
|
||||||
|
reviewers: list[str] = None
|
||||||
|
assignees: list[str] = None
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Download issues and create pull requests for a Gitea repository.',
|
description='Download issues and create pull requests for a Gitea repository.',
|
||||||
|
@ -45,6 +56,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(
|
||||||
|
'--reviewers',
|
||||||
|
type=str,
|
||||||
|
help='Comma-separated list of usernames to assign as reviewers for pull requests',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--assignees',
|
||||||
|
type=str,
|
||||||
|
help='Comma-separated list of usernames to assign as assignees for pull requests',
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,13 +84,19 @@ def main():
|
||||||
while True:
|
while True:
|
||||||
logger.info('Checking for new issues...')
|
logger.info('Checking for new issues...')
|
||||||
for repo in repositories:
|
for repo in repositories:
|
||||||
repository_config = RepositoryConfig(
|
# Parse reviewers and assignees from comma-separated strings to lists
|
||||||
|
reviewers = args.reviewers.split(',') if args.reviewers else None
|
||||||
|
assignees = args.assignees.split(',') if args.assignees else None
|
||||||
|
|
||||||
|
aider_args = AiderArgs(
|
||||||
gitea_url=args.gitea_url,
|
gitea_url=args.gitea_url,
|
||||||
owner=args.owner,
|
owner=args.owner,
|
||||||
repo=repo,
|
repo=repo,
|
||||||
base_branch=args.base_branch,
|
base_branch=args.base_branch,
|
||||||
|
reviewers=reviewers,
|
||||||
|
assignees=assignees,
|
||||||
)
|
)
|
||||||
solve_issues_in_repository(repository_config, client, seen_issues_db)
|
handle_issues(aider_args, client, seen_issues_db)
|
||||||
del repo
|
del repo
|
||||||
if not args.daemon:
|
if not args.daemon:
|
||||||
break
|
break
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
"""Database module for tracking previously processed issues and pull requests.
|
"""Database module for tracking previously processed issues.
|
||||||
|
|
||||||
This module provides functionality to track which issues have already been processed
|
This module provides functionality to track which issues have already been processed
|
||||||
by the system to avoid duplicate processing. It uses a simple SQLite database to
|
by the system to avoid duplicate processing. It uses a simple SQLite database to
|
||||||
store information about seen issues and their associated pull requests for efficient lookup.
|
store hashes of seen issues for efficient lookup.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from hashlib import sha256
|
||||||
|
|
||||||
DEFAULT_DB_PATH = 'output/seen_issues.db'
|
DEFAULT_DB_PATH = 'output/seen_issues.db'
|
||||||
|
|
||||||
|
|
||||||
class SeenIssuesDB:
|
class SeenIssuesDB:
|
||||||
"""Database handler for tracking processed issues and pull requests.
|
"""Database handler for tracking processed issues.
|
||||||
|
|
||||||
This class manages a SQLite database that stores information about issues that have
|
This class manages a SQLite database that stores hashes of issues that have
|
||||||
already been processed and their associated pull requests. It provides methods to mark
|
already been processed. It provides methods to mark issues as seen and check
|
||||||
issues as seen, check if an issue has been seen before, and retrieve pull request
|
if an issue has been seen before, helping to prevent duplicate processing.
|
||||||
information for an issue.
|
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
conn: SQLite database connection
|
conn: SQLite database connection
|
||||||
|
@ -34,90 +34,56 @@ class SeenIssuesDB:
|
||||||
def _create_table(self):
|
def _create_table(self):
|
||||||
"""Create the seen_issues table if it doesn't exist.
|
"""Create the seen_issues table if it doesn't exist.
|
||||||
|
|
||||||
Creates a table with columns for storing issue hashes and associated pull request information.
|
Creates a table with a single column for storing issue hashes.
|
||||||
"""
|
"""
|
||||||
with self.conn:
|
with self.conn:
|
||||||
self.conn.execute("""
|
self.conn.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS seen_issues (
|
CREATE TABLE IF NOT EXISTS seen_issues (
|
||||||
issue_url TEXT PRIMARY KEY,
|
issue_hash TEXT PRIMARY KEY
|
||||||
issue_number TEXT,
|
|
||||||
pr_number TEXT,
|
|
||||||
pr_url TEXT,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def mark_as_seen(
|
def mark_as_seen(self, issue_text: str):
|
||||||
self,
|
|
||||||
issue_url: str,
|
|
||||||
issue_number: str | None = None,
|
|
||||||
pr_number: str | None = None,
|
|
||||||
pr_url: str | None = None,
|
|
||||||
):
|
|
||||||
"""Mark an issue as seen in the database.
|
"""Mark an issue as seen in the database.
|
||||||
|
|
||||||
Computes a hash of the issue text and stores it in the database along with pull request information.
|
Computes a hash of the issue text and stores it in the database.
|
||||||
If the issue has already been marked as seen, this operation has no effect.
|
If the issue has already been marked as seen, this operation has no effect.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
issue_url: The text content of the issue to mark as seen.
|
issue_text: The text content of the issue to mark as seen.
|
||||||
issue_number: The issue number.
|
|
||||||
pr_number: The pull request number associated with this issue.
|
|
||||||
pr_url: The URL of the pull request associated with this issue.
|
|
||||||
"""
|
"""
|
||||||
|
issue_hash = self._compute_hash(issue_text)
|
||||||
with self.conn:
|
with self.conn:
|
||||||
self.conn.execute(
|
self.conn.execute(
|
||||||
'INSERT OR IGNORE INTO seen_issues (issue_url, issue_number, pr_number, pr_url) VALUES (?, ?, ?, ?)',
|
'INSERT OR IGNORE INTO seen_issues (issue_hash) VALUES (?)',
|
||||||
(issue_url, issue_number, pr_number, pr_url),
|
(issue_hash,),
|
||||||
)
|
)
|
||||||
|
|
||||||
def has_seen(self, issue_url: str) -> bool:
|
def has_seen(self, issue_text: str) -> bool:
|
||||||
"""Check if an issue has been seen before.
|
"""Check if an issue has been seen before.
|
||||||
|
|
||||||
Computes a hash of the issue text and checks if it exists in the database.
|
Computes a hash of the issue text and checks if it exists in the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
issue_url: The text content of the issue to check.
|
issue_text: The text content of the issue to check.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True if the issue has been seen before, False otherwise.
|
True if the issue has been seen before, False otherwise.
|
||||||
"""
|
"""
|
||||||
|
issue_hash = self._compute_hash(issue_text)
|
||||||
cursor = self.conn.execute(
|
cursor = self.conn.execute(
|
||||||
'SELECT 1 FROM seen_issues WHERE issue_url = ?',
|
'SELECT 1 FROM seen_issues WHERE issue_hash = ?',
|
||||||
(issue_url,),
|
(issue_hash,),
|
||||||
)
|
)
|
||||||
return cursor.fetchone() is not None
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
def get_pr_info(self, issue_url: str) -> tuple[str, str] | None:
|
def _compute_hash(self, text: str) -> str:
|
||||||
"""Get pull request information for an issue.
|
"""Compute a SHA-256 hash of the given text.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
issue_url: The text content of the issue to check.
|
text: The text to hash.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A tuple containing (pr_number, pr_url) if found, None otherwise.
|
A hexadecimal string representation of the hash.
|
||||||
"""
|
"""
|
||||||
cursor = self.conn.execute(
|
return sha256(text.encode('utf-8')).hexdigest()
|
||||||
'SELECT pr_number, pr_url FROM seen_issues WHERE issue_url = ?',
|
|
||||||
(issue_url,),
|
|
||||||
)
|
|
||||||
result = cursor.fetchone()
|
|
||||||
return result if result else None
|
|
||||||
|
|
||||||
def update_pr_info(self, issue_url: str, pr_number: str, pr_url: str) -> bool:
|
|
||||||
"""Update pull request information for an existing issue.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
issue_url: The text content of the issue to update.
|
|
||||||
pr_number: The pull request number.
|
|
||||||
pr_url: The URL of the pull request.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the update was successful, False if the issue wasn't found.
|
|
||||||
"""
|
|
||||||
with self.conn:
|
|
||||||
cursor = self.conn.execute(
|
|
||||||
'UPDATE seen_issues SET pr_number = ?, pr_url = ? WHERE issue_url = ?',
|
|
||||||
(pr_number, pr_url, issue_url),
|
|
||||||
)
|
|
||||||
return cursor.rowcount > 0
|
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from aider_gitea import has_commits_on_branch
|
|
||||||
|
|
||||||
|
|
||||||
class TestHasCommitsOnBranch:
|
|
||||||
def setup_method(self):
|
|
||||||
self.cwd = Path('/tmp/test-repo')
|
|
||||||
self.base_branch = 'main'
|
|
||||||
self.current_branch = 'feature-branch'
|
|
||||||
|
|
||||||
@patch('aider_gitea.get_commit_messages')
|
|
||||||
def test_has_commits_true(self, mock_get_commit_messages):
|
|
||||||
# Setup mock to return some commit messages
|
|
||||||
mock_get_commit_messages.return_value = ['Commit 1', 'Commit 2']
|
|
||||||
|
|
||||||
# Test function returns True when there are commits
|
|
||||||
assert (
|
|
||||||
has_commits_on_branch(self.cwd, self.base_branch, self.current_branch)
|
|
||||||
is True
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify get_commit_messages was called with correct arguments
|
|
||||||
mock_get_commit_messages.assert_called_once_with(
|
|
||||||
self.cwd, self.base_branch, self.current_branch,
|
|
||||||
)
|
|
||||||
|
|
||||||
@patch('aider_gitea.get_commit_messages')
|
|
||||||
def test_has_commits_false(self, mock_get_commit_messages):
|
|
||||||
# Setup mock to return empty list
|
|
||||||
mock_get_commit_messages.return_value = []
|
|
||||||
|
|
||||||
# Test function returns False when there are no commits
|
|
||||||
assert (
|
|
||||||
has_commits_on_branch(self.cwd, self.base_branch, self.current_branch)
|
|
||||||
is False
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify get_commit_messages was called with correct arguments
|
|
||||||
mock_get_commit_messages.assert_called_once_with(
|
|
||||||
self.cwd, self.base_branch, self.current_branch,
|
|
||||||
)
|
|
||||||
|
|
||||||
@patch('aider_gitea.get_commit_messages')
|
|
||||||
def test_has_commits_exception(self, mock_get_commit_messages):
|
|
||||||
# Setup mock to raise an exception
|
|
||||||
mock_get_commit_messages.side_effect = Exception('Git command failed')
|
|
||||||
|
|
||||||
# Test function returns False when an exception occurs
|
|
||||||
assert (
|
|
||||||
has_commits_on_branch(self.cwd, self.base_branch, self.current_branch)
|
|
||||||
is False
|
|
||||||
)
|
|
49
test/test_main_args.py
Normal file
49
test/test_main_args.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from aider_gitea.__main__ import parse_args
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_args_with_reviewers_and_assignees():
|
||||||
|
"""Test that reviewers and assignees arguments are correctly parsed."""
|
||||||
|
test_args = [
|
||||||
|
'--gitea-url',
|
||||||
|
'https://gitea.example.com',
|
||||||
|
'--owner',
|
||||||
|
'test-owner',
|
||||||
|
'--repo',
|
||||||
|
'test-repo',
|
||||||
|
'--reviewers',
|
||||||
|
'user1,user2',
|
||||||
|
'--assignees',
|
||||||
|
'user3,user4',
|
||||||
|
]
|
||||||
|
|
||||||
|
with patch('sys.argv', ['aider_gitea'] + test_args):
|
||||||
|
args = parse_args()
|
||||||
|
|
||||||
|
assert args.gitea_url == 'https://gitea.example.com'
|
||||||
|
assert args.owner == 'test-owner'
|
||||||
|
assert args.repo == 'test-repo'
|
||||||
|
assert args.reviewers == 'user1,user2'
|
||||||
|
assert args.assignees == 'user3,user4'
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_args_without_reviewers_and_assignees():
|
||||||
|
"""Test that the parser works without reviewers and assignees."""
|
||||||
|
test_args = [
|
||||||
|
'--gitea-url',
|
||||||
|
'https://gitea.example.com',
|
||||||
|
'--owner',
|
||||||
|
'test-owner',
|
||||||
|
'--repo',
|
||||||
|
'test-repo',
|
||||||
|
]
|
||||||
|
|
||||||
|
with patch('sys.argv', ['aider_gitea'] + test_args):
|
||||||
|
args = parse_args()
|
||||||
|
|
||||||
|
assert args.gitea_url == 'https://gitea.example.com'
|
||||||
|
assert args.owner == 'test-owner'
|
||||||
|
assert args.repo == 'test-repo'
|
||||||
|
assert args.reviewers is None
|
||||||
|
assert args.assignees is None
|
|
@ -1,77 +0,0 @@
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from aider_gitea.seen_issues_db import SeenIssuesDB
|
|
||||||
|
|
||||||
|
|
||||||
class TestSeenIssuesDBPRInfo:
|
|
||||||
def setup_method(self):
|
|
||||||
# Create a temporary database file
|
|
||||||
self.db_fd, self.db_path = tempfile.mkstemp()
|
|
||||||
self.db = SeenIssuesDB(self.db_path)
|
|
||||||
|
|
||||||
# Test data
|
|
||||||
self.issue_text = 'Test issue title\nTest issue description'
|
|
||||||
self.issue_number = '123'
|
|
||||||
self.pr_number = '456'
|
|
||||||
self.pr_url = 'https://gitea.example.com/owner/repo/pulls/456'
|
|
||||||
|
|
||||||
def teardown_method(self):
|
|
||||||
# Close and remove the temporary database
|
|
||||||
self.db.conn.close()
|
|
||||||
os.close(self.db_fd)
|
|
||||||
os.unlink(self.db_path)
|
|
||||||
|
|
||||||
def test_mark_as_seen_with_pr_info(self):
|
|
||||||
# Mark an issue as seen with PR info
|
|
||||||
self.db.mark_as_seen(
|
|
||||||
self.issue_text,
|
|
||||||
issue_number=self.issue_number,
|
|
||||||
pr_number=self.pr_number,
|
|
||||||
pr_url=self.pr_url,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify the issue is marked as seen
|
|
||||||
assert self.db.has_seen(self.issue_text)
|
|
||||||
|
|
||||||
# Verify PR info is stored correctly
|
|
||||||
pr_info = self.db.get_pr_info(self.issue_text)
|
|
||||||
assert pr_info is not None
|
|
||||||
assert pr_info[0] == self.pr_number
|
|
||||||
assert pr_info[1] == self.pr_url
|
|
||||||
|
|
||||||
def test_update_pr_info(self):
|
|
||||||
# First mark the issue as seen without PR info
|
|
||||||
self.db.mark_as_seen(self.issue_text, issue_number=self.issue_number)
|
|
||||||
|
|
||||||
# Verify no PR info is available
|
|
||||||
assert self.db.get_pr_info(self.issue_text) == (None, None)
|
|
||||||
|
|
||||||
# Update with PR info
|
|
||||||
updated = self.db.update_pr_info(self.issue_text, self.pr_number, self.pr_url)
|
|
||||||
|
|
||||||
# Verify update was successful
|
|
||||||
assert updated
|
|
||||||
|
|
||||||
# Verify PR info is now available
|
|
||||||
pr_info = self.db.get_pr_info(self.issue_text)
|
|
||||||
assert pr_info[0] == self.pr_number
|
|
||||||
assert pr_info[1] == self.pr_url
|
|
||||||
|
|
||||||
def test_update_nonexistent_issue(self):
|
|
||||||
# Try to update PR info for an issue that doesn't exist
|
|
||||||
updated = self.db.update_pr_info(
|
|
||||||
'Nonexistent issue',
|
|
||||||
self.pr_number,
|
|
||||||
self.pr_url,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify update failed
|
|
||||||
assert not updated
|
|
||||||
|
|
||||||
def test_get_pr_info_nonexistent(self):
|
|
||||||
# Try to get PR info for an issue that doesn't exist
|
|
||||||
pr_info = self.db.get_pr_info('Nonexistent issue')
|
|
||||||
|
|
||||||
# Verify no PR info is available
|
|
||||||
assert pr_info is None
|
|
|
@ -1,18 +1,17 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from aider_gitea import IssueResolution, RepositoryConfig, solve_issue_in_repository
|
from aider_gitea import solve_issue_in_repository
|
||||||
|
|
||||||
REPOSITORY_CONFIG = RepositoryConfig(
|
|
||||||
gitea_url='https://gitea.example.com',
|
|
||||||
owner='test-owner',
|
|
||||||
repo='test-repo',
|
|
||||||
base_branch='main',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestSolveIssueInRepository:
|
class TestSolveIssueInRepository:
|
||||||
def setup_method(self):
|
def setup_method(self):
|
||||||
|
self.args = MagicMock()
|
||||||
|
self.args.gitea_url = 'https://gitea.example.com'
|
||||||
|
self.args.owner = 'test-owner'
|
||||||
|
self.args.repo = 'test-repo'
|
||||||
|
self.args.base_branch = 'main'
|
||||||
|
|
||||||
self.gitea_client = MagicMock()
|
self.gitea_client = MagicMock()
|
||||||
self.tmpdirname = Path('/tmp/test-repo')
|
self.tmpdirname = Path('/tmp/test-repo')
|
||||||
self.branch_name = 'issue-123-test-branch'
|
self.branch_name = 'issue-123-test-branch'
|
||||||
|
@ -33,11 +32,7 @@ class TestSolveIssueInRepository:
|
||||||
):
|
):
|
||||||
# Setup mocks
|
# Setup mocks
|
||||||
mock_run_cmd.return_value = True
|
mock_run_cmd.return_value = True
|
||||||
mock_push_changes.return_value = IssueResolution(
|
mock_push_changes.return_value = True
|
||||||
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 to return different commit hashes and file changes
|
||||||
mock_subprocess_run.side_effect = [
|
mock_subprocess_run.side_effect = [
|
||||||
|
@ -50,7 +45,7 @@ class TestSolveIssueInRepository:
|
||||||
|
|
||||||
# Call the function
|
# Call the function
|
||||||
result = solve_issue_in_repository(
|
result = solve_issue_in_repository(
|
||||||
REPOSITORY_CONFIG,
|
self.args,
|
||||||
self.tmpdirname,
|
self.tmpdirname,
|
||||||
self.branch_name,
|
self.branch_name,
|
||||||
self.issue_title,
|
self.issue_title,
|
||||||
|
@ -60,7 +55,7 @@ class TestSolveIssueInRepository:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify results
|
# Verify results
|
||||||
assert result.success is True
|
assert result is True
|
||||||
assert mock_run_cmd.call_count >= 8 # Verify all expected commands were run
|
assert mock_run_cmd.call_count >= 8 # Verify all expected commands were run
|
||||||
mock_push_changes.assert_called_once()
|
mock_push_changes.assert_called_once()
|
||||||
|
|
||||||
|
@ -77,7 +72,6 @@ class TestSolveIssueInRepository:
|
||||||
):
|
):
|
||||||
# Setup mocks
|
# Setup mocks
|
||||||
mock_run_cmd.return_value = True
|
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 to return same commit hash and no file changes
|
||||||
mock_subprocess_run.side_effect = [
|
mock_subprocess_run.side_effect = [
|
||||||
|
@ -87,7 +81,7 @@ class TestSolveIssueInRepository:
|
||||||
|
|
||||||
# Call the function
|
# Call the function
|
||||||
result = solve_issue_in_repository(
|
result = solve_issue_in_repository(
|
||||||
REPOSITORY_CONFIG,
|
self.args,
|
||||||
self.tmpdirname,
|
self.tmpdirname,
|
||||||
self.branch_name,
|
self.branch_name,
|
||||||
self.issue_title,
|
self.issue_title,
|
||||||
|
@ -97,5 +91,5 @@ class TestSolveIssueInRepository:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify results
|
# Verify results
|
||||||
assert result.success is False
|
assert result is False
|
||||||
assert mock_push_changes.call_count == 0 # push_changes should not be called
|
assert mock_push_changes.call_count == 0 # push_changes should not be called
|
||||||
|
|
Loading…
Reference in New Issue
Block a user