From 48ace8416dbfca4d6b2200b0240bd5891d93216e Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 15:21:50 -0600 Subject: [PATCH 1/6] Add multiply function, tests, and CI workflow --- .github/workflows/ci.yml | 26 ++++++++++++++++++++++++++ app.py | 4 ++++ tests/test_app.py | 15 ++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..d0863fdd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Run tests + run: pytest -v diff --git a/app.py b/app.py index 8f2f7ae1..86919caf 100644 --- a/app.py +++ b/app.py @@ -14,3 +14,7 @@ def is_even(n: int) -> bool: def reverse_string(s: str) -> str: """Reverse a string.""" return s[::-1] + +def multiply(a: int, b: int) -> int: + """Multiply two numbers together.""" + return a * b \ No newline at end of file diff --git a/tests/test_app.py b/tests/test_app.py index 79c3e093..32cf991b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,6 +1,6 @@ """Tests for app.py - you'll add more!""" -from app import add, is_even, reverse_string +from app import add, is_even, reverse_string, multiply class TestMath: @@ -22,3 +22,16 @@ def test_reverse(self): def test_is_even(self): assert is_even(4) is True assert is_even(3) is False + +class TestMultiply: + """Tests for multiply function.""" + + def test_multiply_positive(self): + assert multiply(2, 3) == 6 + + def test_multiply_by_zero(self): + assert multiply(0, 10) == 0 + + def test_multiply_by_negative(self): + assert multiply(-1, -1) == 1 + From 7801bbb66048bb2dd0b8e8ee39c6aaa3d33a574b Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 15:24:10 -0600 Subject: [PATCH 2/6] Break a test --- tests/test_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_app.py b/tests/test_app.py index 32cf991b..7fae9a3e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -27,7 +27,7 @@ class TestMultiply: """Tests for multiply function.""" def test_multiply_positive(self): - assert multiply(2, 3) == 6 + assert multiply(2, 3) == 9 def test_multiply_by_zero(self): assert multiply(0, 10) == 0 From 18681281a4baec984cec22acf5da280ce9ba880c Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 15:26:04 -0600 Subject: [PATCH 3/6] Fix the test --- tests/test_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_app.py b/tests/test_app.py index 7fae9a3e..32cf991b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -27,7 +27,7 @@ class TestMultiply: """Tests for multiply function.""" def test_multiply_positive(self): - assert multiply(2, 3) == 9 + assert multiply(2, 3) == 6 def test_multiply_by_zero(self): assert multiply(0, 10) == 0 From 931cbf4868d7f4b5a55b90fd346e9206ec571eaa Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 16:37:12 -0600 Subject: [PATCH 4/6] Add AI code review script and PR workflow --- .github/workflows/pr-review.yml | 56 +++++++++++++++++++++++++++++++++ requirements.txt | 1 + scripts/ai_review.py | 41 ++++++++++++++++++++++++ scripts/sample_diff.txt | 17 ++++++++++ 4 files changed, 115 insertions(+) create mode 100644 .github/workflows/pr-review.yml create mode 100644 scripts/ai_review.py create mode 100644 scripts/sample_diff.txt diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml new file mode 100644 index 00000000..1bbebca3 --- /dev/null +++ b/.github/workflows/pr-review.yml @@ -0,0 +1,56 @@ +name: AI Code Review + +on: + pull_request: + branches: [main] + +permissions: + contents: read + pull-requests: write + +jobs: + review: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Get PR diff + run: | + git diff origin/main...HEAD > pr_diff.txt + + - name: Run AI review + id: ai-review + env: + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} + run: | + REVIEW=$(python scripts/ai_review.py pr_diff.txt) + echo "review<> $GITHUB_OUTPUT + echo "$REVIEW" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Post review comment + uses: actions/github-script@v7 + env: + REVIEW: ${{ steps.ai-review.outputs.review }} + with: + script: | + const review = process.env.REVIEW; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: `## 🤖 AI Code Review\n\n${review}\n\n---\n*Powered by Gemini AI*` + }); diff --git a/requirements.txt b/requirements.txt index 5f456517..389d026e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ # Production dependencies (none for this simple app) +google-genai>=1.0.0 # Development/testing dependencies pytest>=7.0.0 diff --git a/scripts/ai_review.py b/scripts/ai_review.py new file mode 100644 index 00000000..078a0fc5 --- /dev/null +++ b/scripts/ai_review.py @@ -0,0 +1,41 @@ +from google import genai +import sys + +client = genai.Client() + +# Define a function that takes a code diff as input +def review_code(diff_text): + prompt = f"""Act as a code reviewer. Here is a code diff: + +{diff_text} + +Review for: +- Security issues +- Bugs +- Performance issues +- Code style issues +- Code readability issues +- Code maintainability issues +""" + + response = client.models.generate_content( + model="gemini-2.5-flash", + contents=prompt, + ) + + return response.text + +# Only run this code when the script is executed directly +if __name__ == "__main__": + # Check if a filename was passed as a command-line argument + if len(sys.argv) > 1: + diff_file = sys.argv[1] + with open(diff_file, "r", encoding="utf-8") as f: + diff_content = f.read() + else: + # If no filename was passed, read from standard input + diff_content = sys.stdin.read() + + # Call the review function and print the result + review = review_code(diff_content) # fixed name + print(review) diff --git a/scripts/sample_diff.txt b/scripts/sample_diff.txt new file mode 100644 index 00000000..d4b5984b --- /dev/null +++ b/scripts/sample_diff.txt @@ -0,0 +1,17 @@ +diff --git a/app.py b/app.py +index 1234567..abcdefg 100644 +--- a/app.py ++++ b/app.py +@@ -1,5 +1,12 @@ + """Simple utility functions""" + ++import sqlite3 ++ ++def get_user(username): ++ conn = sqlite3.connect("users.db") ++ query = f"SELECT * FROM users WHERE name = '{username}'" ++ return conn.execute(query).fetchone() ++ + def add(a: int, b: int) -> int: + """Add two numbers together.""" + return a + b From dfb8a77132f2de295690060853195cfb839b69d7 Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 16:41:36 -0600 Subject: [PATCH 5/6] Add command execution feature --- dangerous.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 dangerous.py diff --git a/dangerous.py b/dangerous.py new file mode 100644 index 00000000..5d477bcb --- /dev/null +++ b/dangerous.py @@ -0,0 +1,7 @@ +import subprocess + +def run_command(user_input): + """Run a shell command from user input.""" + subprocess.call(user_input, shell=True) + +API_KEY = "sk-live-abc123def456" From c9c0d702666ad49192fb26b441c4b95095bab1f4 Mon Sep 17 00:00:00 2001 From: Amar Date: Mon, 9 Feb 2026 16:51:23 -0600 Subject: [PATCH 6/6] Fix security issues --- dangerous.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dangerous.py b/dangerous.py index 5d477bcb..86d316f3 100644 --- a/dangerous.py +++ b/dangerous.py @@ -1,7 +1,10 @@ import subprocess +import shlex +import os def run_command(user_input): - """Run a shell command from user input.""" - subprocess.call(user_input, shell=True) + """Run a shell command safely.""" + args = shlex.split(user_input) + subprocess.call(args, shell=False) -API_KEY = "sk-live-abc123def456" +API_KEY = os.environ.get("API_KEY")