From dbc86c85d49d4a4e1c8dd351dde915aff408cf80 Mon Sep 17 00:00:00 2001
From: "Jon Michael Aanes (aider)" <jonjmaa@gmail.com>
Date: Sun, 13 Apr 2025 14:56:55 +0200
Subject: [PATCH] feat: implement new branch creation process using temporary
 clone and aider

---
 aider_gitea/__main__.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/aider_gitea/__main__.py b/aider_gitea/__main__.py
index 4ed24fe..8fc498f 100644
--- a/aider_gitea/__main__.py
+++ b/aider_gitea/__main__.py
@@ -9,6 +9,9 @@ import argparse
 import requests
 import sys
 import dataclasses
+import tempfile
+import subprocess
+import os
 
 logger = logging.getLogger(__name__)
 
@@ -94,11 +97,19 @@ def main():
         title = issue.get("title", f"Issue {issue_number}")
         branch_name = f"issue-{issue_number}"
         try:
-            created = client.create_branch(args.owner, args.repo, branch_name, base_sha)
-            if created:
+            with tempfile.TemporaryDirectory() as tmpdirname:
+                repo_url = f"{args.gitea_url}/repos/{args.owner}/{args.repo}.git"
+                subprocess.run(["git", "clone", repo_url, tmpdirname], check=True)
+                subprocess.run(["git", "-C", tmpdirname, "checkout", args.base_branch], check=True)
+                subprocess.run(["git", "-C", tmpdirname, "checkout", "-b", branch_name], check=True)
+                message = issue.get("body", "")
+                subprocess.run(["aider", message], check=True)
+                subprocess.run(["git", "-C", tmpdirname, "add", "."], check=True)
+                subprocess.run(["git", "-C", tmpdirname, "commit", "-m", f"Apply aider for issue {issue_number}"], check=True)
+                subprocess.run(["git", "-C", tmpdirname, "push", "origin", branch_name], check=True)
                 print(f"Created branch {branch_name} for issue {issue_number}.")
         except Exception as e:
-            print(f"Error creating branch {branch_name}: {e}")
+            print(f"Error processing issue {issue_number}: {e}")
         body = f"Automatically generated pull request for issue: {issue.get('html_url', 'unknown')}"
         try:
             pr = client.create_pull_request(args.owner, args.repo, f"[Issue {issue_number}] {title}", branch_name, args.base_branch, body)