Skip to content

Commit

Permalink
Merge pull request #27 from CSE2102-Spring25/main
Browse files Browse the repository at this point in the history
Making sure develop branch is up-to-date with main
  • Loading branch information
sed19015 authored Apr 28, 2025
2 parents 1de91e2 + edca506 commit d9e4ee5
Show file tree
Hide file tree
Showing 45 changed files with 1,325 additions and 522 deletions.
112 changes: 76 additions & 36 deletions .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,100 @@
name: Backend CI Pipeline
name: CI Backend + Frontend Workflow

# Trigger workflow on push or pull request to develop and main branches
on:
push:
branches:
- develop
- main
branches: [ "main" ]
pull_request:
branches:
- develop
- main
branches: [ "main" ]

jobs:
lint:
runs-on: ubuntu-latest
backend-ci:
runs-on: self-hosted
defaults:
run:
working-directory: ./backend

services:
postgres:
image: postgres:13
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: testdb
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11' # Adjust Python version if needed
python-version: '3.11'

- name: Install dependencies
- name: Install backend dependencies
run: |
python -m venv venv
source venv/bin/activate
sudo apt-get update
sudo apt-get install -y jq
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pylint pytest requests
- name: Run pylint on Python code
- name: Run pylint on backend
run: |
source venv/bin/activate
pylint backend/main.py backend/db.py backend/init_db.py # Add more files as needed
git fetch origin main
changed_files=$(git diff --name-only origin/main...HEAD | grep '\.py$' || true)
if [ -n "$changed_files" ]; then
pylint $changed_files --exit-zero | tee pylint.log
SCORE=$(tail -n 2 pylint.log | grep -oP '[0-9]+\.[0-9]+(?=/10)')
echo "Pylint score: $SCORE"
python -c "import sys; sys.exit(0 if float('$SCORE') >= 8.0 else 1)"
else
echo "No Python files changed, skipping pylint."
exit 0
fi
test:
runs-on: ubuntu-latest
needs: lint # Ensure this job runs after linting
- name: Start backend server (background)
run: |
python main.py &
for i in {1..10}; do
curl -s http://localhost:5000/api/pets && break || sleep 2
done
- name: Initialize database
run: |
python init_db.py
- name: Run pytest on backend (HTTP API tests)
run: |
pytest tests/
frontend-build:
runs-on: self-hosted
defaults:
run:
working-directory: ./frontend

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
- name: Set up Node.js
uses: actions/setup-node@v4
with:
python-version: '3.11'
node-version: '20'

- name: Install dependencies
- name: Install frontend dependencies
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
npm install
- name: Run tests with pytest
- name: Build frontend
run: |
source venv/bin/activate
pytest backend/tests --maxfail=1 --disable-warnings -q
npm run build
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ https://trello.com/b/1z9zUv2a/cse-2102-project

Figma Prototype
https://www.figma.com/design/ackeSbcEwP2kJQmJknsge5/Milestone3_Figma?node-id=0-1&t=ItY8uyCUknB0nwIz-1
# trigger cleanup
145 changes: 125 additions & 20 deletions backend/.github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,144 @@
name: Backend CI
name: Backend CI Pipeline

on:
push:
paths:
- 'backend/**'
branches: [main, develop]
pull_request:
paths:
- 'backend/**'
branches: [main, develop]

jobs:
backend-ci:
runs-on: ubuntu-latest
lint:
runs-on: self-hosted
defaults:
run:
working-directory: backend

steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Checkout code without submodules
uses: actions/checkout@v2 # Using v2 for actions/checkout
with:
submodules: false # Skips submodule initialization

- name: Set up Python
uses: actions/setup-python@v2 # Using v2 for setup-python
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt # Ensure all dependencies are installed, including requests
- name: Cache Python dependencies
uses: actions/cache@v2 # Using v2 for actions/cache
with:
path: venv
key: ${{ runner.os }}-python-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-python-
- name: Run pylint (backend only on changed files)
run: |
source venv/bin/activate
changed_files=$(git diff --name-only origin/main...HEAD | grep '\.py$' || true)
if [ -n "$changed_files" ]; then
pylint $changed_files --exit-zero | tee pylint.log
SCORE=$(tail -n 2 pylint.log | grep -oP '[0-9]+\.[0-9]+(?=/10)')
echo "Pylint score: $SCORE"
python -c "import sys; sys.exit(0 if float('$SCORE') >= 8.0 else 1)"
else
echo "No Python files changed, skipping pylint."
exit 0 # Exit with status 0 (success) if no Python files changed
fi
test:
runs-on: self-hosted
needs: lint

steps:
- name: Checkout code without submodules
uses: actions/checkout@v2 # Using v2 for actions/checkout
with:
submodules: false # Skips submodule initialization

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v2 # Using v2 for setup-python
with:
python-version: '3.11'

- name: Install dependencies
working-directory: ./backend
run: |
pip install -r requirements.txt
python -m venv venv
source venv/bin/activate
pip install -r backend/requirements.txt # Ensure all dependencies are installed, including requests
pip install pytest-html
- name: Run pylint on backend
working-directory: ./backend
- name: Cache Python dependencies
uses: actions/cache@v2 # Using v2 for actions/cache
with:
path: venv
key: ${{ runner.os }}-python-${{ hashFiles('backend/requirements.txt') }}
restore-keys: |
${{ runner.os }}-python-
- name: Start Flask API server
run: |
nohup python backend/main.py > flask.log 2>&1 &
for i in {1..10}; do
if curl --silent --fail http://127.0.0.1:5000/; then
echo "Flask is ready"
break
fi
echo "Waiting for Flask... Attempt $i/10"
sleep 5
done
- name: Run backend tests with pytest
env:
BASE_URL: http://127.0.0.1:5000
run: |
pylint main.py db.py init_db.py
source venv/bin/activate # Activate the virtual environment before running tests
pytest backend/tests --html=backend/test-report.html --self-contained-html
- name: Run Pytest
working-directory: ./backend
- name: Upload test report
uses: actions/upload-artifact@v2 # Using v2 for actions/upload-artifact
with:
name: backend-test-report
path: backend/test-report.html

- name: Kill Flask server
run: |
python main.py & # Run server
sleep 5 # Give it time to start
pytest tests/
pkill -f "python backend/main.py" || echo "Flask already exited"
build-frontend:
runs-on: self-hosted
needs: [lint, test]

steps:
- name: Checkout repository without submodules
uses: actions/checkout@v2 # Using v2 for actions/checkout
with:
submodules: false # Skips submodule initialization

- name: Set up Node.js
uses: actions/setup-node@v3 # Using the latest stable version for Node.js
with:
node-version: '20'

- name: Cache node modules
uses: actions/cache@v2 # Using v2 for actions/cache
with:
path: frontend/node_modules
key: ${{ runner.os }}-node-${{ hashFiles('frontend/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install frontend dependencies
working-directory: frontend
run: npm install

- name: Build frontend app
working-directory: frontend
run: npm run build
Binary file added backend/__pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file added backend/__pycache__/pets.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file added backend/__pycache__/utils.cpython-311.pyc
Binary file not shown.
40 changes: 13 additions & 27 deletions backend/db.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
"""Database utility functions for managing the pet adoption database."""

import sqlite3
import os
from utils import create_tables


def init_db():
"""Initialize the database, creating tables and inserting initial data."""
db_path = os.path.join(os.path.dirname(__file__), 'animal_shelter.db')
db_path = os.path.join(os.path.dirname(__file__), "animal_shelter.db")

if os.path.exists(db_path):
print("Database already initialized.")
return

# Connect to SQLite database (it will create the file if it doesn't exist)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
create_tables(conn)

# Create tables
cursor.execute('''CREATE TABLE IF NOT EXISTS animals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
species TEXT NOT NULL,
breed TEXT,
age INTEGER,
personality TEXT,
image_path TEXT,
adoption_status TEXT DEFAULT 'Available'
)''')

cursor.execute('''CREATE TABLE IF NOT EXISTS adopters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE,
join_date TEXT DEFAULT CURRENT_TIMESTAMP
)''')

# Insert initial data (e.g., a test animal)
cursor.execute('''INSERT INTO animals (name, species, breed, age, personality, image_path)
VALUES (?, ?, ?, ?, ?, ?)''',
('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg'))
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO animals (name, species, breed, age, personality, image_path)
VALUES (?, ?, ?, ?, ?, ?)""",
("TestDog", "Dog", "Mixed", 2, "Friendly", "/images/test.jpg"),
)

conn.commit()
conn.close()
Expand Down
Binary file added backend/images/bella.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/max.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/mittens.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/images/oldy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d9e4ee5

Please sign in to comment.