diff --git a/.github/workflows/agent.yml b/.github/workflows/agent.yml index 167386b..782dd65 100644 --- a/.github/workflows/agent.yml +++ b/.github/workflows/agent.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest permissions: pull-requests: write - contents : read + contents : write steps: - name: Checkout repo uses : actions/checkout@v3 diff --git a/agent/github/committer.py b/agent/github/committer.py new file mode 100644 index 0000000..8614518 --- /dev/null +++ b/agent/github/committer.py @@ -0,0 +1,19 @@ +import os +import subprocess + +def commit_tests(): + try: + branch = os.getenv("GITHUB_HEAD_REF") # PR branch + + subprocess.run(["git", "config", "user.name", "github-actions"], check=True) + subprocess.run(["git", "config", "user.email", "actions@github.com"], check=True) + + subprocess.run(["git", "add", "tests/"], check=True) + subprocess.run(["git", "commit", "-m", "Add AI-generated tests"], check=True) + + subprocess.run(["git", "push", "origin", f"HEAD:{branch}"], check=True) + + print("Tests pushed to PR branch") + + except Exception as e: + print("Commit Failed", e) \ No newline at end of file diff --git a/agent/llm/test_generator.py b/agent/llm/test_generator.py index 7382c9c..733bba7 100644 --- a/agent/llm/test_generator.py +++ b/agent/llm/test_generator.py @@ -34,7 +34,12 @@ def generate_tests(diff:str,context:list =None)->str: } ], ) - return response.choices[0].message.content.strip() + content =response.choices[0].message.content.strip() + if content.startswith("```"): + content = content.split("```")[1] # Extract code from markdown + if content.startswith("python"): + content = content[len("python"):] # Remove language specifier + return content.strip() except Exception as e: return f"Error generating tests:{str(e)}" \ No newline at end of file diff --git a/agent/main.py b/agent/main.py index 144e4eb..33fb385 100644 --- a/agent/main.py +++ b/agent/main.py @@ -3,8 +3,8 @@ from agent.indexing.embedder import get_embeddings from agent.indexing.vector_store import store_embeddings, query_embeddings from agent.llm.test_generator import generate_tests - -import subprocess +from agent.github.committer import commit_tests +import subprocess , os from agent.llm.groq_client import generate_review from agent.github.commenter import post_comment def get_pr_diff(): @@ -72,6 +72,10 @@ def main(): # 8. Generate tests print("Generating tests...") tests = generate_tests(diff, context=relevant_chunks) + os.makedirs("tests", exist_ok=True) + with open("tests/test_generated.py", "w", encoding="utf-8") as f: + f.write(tests) # Save generated tests to a file for potential commit + print("Generated tests saved to tests/test_generated.py") # 9. Combine output final_output = f"{review}\n\n---\n\n### Suggested Tests\n{tests}" @@ -83,6 +87,10 @@ def main(): print("Posting comment...") post_comment(final_output) + # 11. Commit tests + print("Committing tests...") + commit_tests() + print("Done") if __name__ == "__main__": diff --git a/tests/test_generated.py b/tests/test_generated.py new file mode 100644 index 0000000..ff04abe --- /dev/null +++ b/tests/test_generated.py @@ -0,0 +1,26 @@ +import pytest +import os +from agent.github.committer import commit_tests +from agent.main import get_pr_diff + +def test_commit_tests(): + commit_tests() + +def test_get_pr_diff_empty(): + with pytest.raises(subprocess.CalledProcessError): + get_pr_diff() + +def test_commit_tests_exception(): + try: + commit_tests() + except Exception as e: + assert str(e) + +def test_get_pr_diff_no_diff(): + diff = get_pr_diff() + assert diff.strip() == "" + +def test_commit_tests_push(): + subprocess.run(["git", "config", "user.name", "github-actions"], check=True) + subprocess.run(["git", "config", "user.email", "actions@github.com"], check=True) + commit_tests() \ No newline at end of file