Skip to content

Commit

Permalink
feat: init pytest workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Aug 21, 2024
1 parent 4964aee commit a4df0aa
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 80 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/aws-preview.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: Deploy Backend to Preview ECS

on:
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/aws-preview.yml
- server/**
- petercat_utils/**
- subscriber/**
workflow_run:
workflows: ["Build And Test"]
types:
- completed

env:
AWS_REGION: ap-northeast-1
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/server-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build And Test

on:
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/aws-preview.yml
- server/**
- petercat_utils/**
- subscriber/**

jobs:
build:
runs-on: ubuntu-latest
environment: production
strategy:
fail-fast: true

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
run: |
cd server
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Test with pytest
run: |
pip install pytest pytest-cov
pytest
13 changes: 0 additions & 13 deletions client/tests/github/pull_request_test.py

This file was deleted.

7 changes: 4 additions & 3 deletions server/agent/bot_builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import AsyncIterator, Optional
from petercat_utils.data_class import ChatData
from agent.base import AgentBuilder
from prompts.bot_builder import generate_prompt_by_user_id
from tools import bot_builder

from ..agent.base import AgentBuilder
from ..prompts.bot_builder import generate_prompt_by_user_id
from ..tools import bot_builder


TOOL_MAPPING = {
Expand Down
7 changes: 4 additions & 3 deletions server/agent/qa_chat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import AsyncIterator, Optional
from agent.base import AgentBuilder
from prompts.bot_template import generate_prompt_by_repo_name
from petercat_utils import get_client
from petercat_utils.data_class import ChatData
from tools import issue, sourcecode, knowledge, git_info

from ..agent.base import AgentBuilder
from ..prompts.bot_template import generate_prompt_by_repo_name
from ..tools import issue, sourcecode, knowledge, git_info


def get_tools(bot_id: str, token: Optional[str]):
Expand Down
3 changes: 2 additions & 1 deletion server/bot/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from petercat_utils import get_client
from petercat_utils.data_class import RAGGitDocConfig
from petercat_utils import git_doc_task
from prompts.bot_template import generate_prompt_by_repo_name

from ..prompts.bot_template import generate_prompt_by_repo_name

g = Github()

Expand Down
9 changes: 4 additions & 5 deletions server/dao/authorizationDAO.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

import json
from dao.BaseDAO import BaseDAO
from models.authorization import Authorization
from supabase.client import Client, create_client
from supabase.client import Client

from petercat_utils.db.client.supabase import get_client

from ..dao.BaseDAO import BaseDAO
from ..models.authorization import Authorization

class AuthorizationDAO(BaseDAO):
client: Client

Expand Down
9 changes: 3 additions & 6 deletions server/dao/repositoryConfigDAO.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@

import json
from dao.BaseDAO import BaseDAO
from models.repository import RepositoryConfig
from supabase.client import Client, create_client

from supabase.client import Client
from petercat_utils.db.client.supabase import get_client

from ..dao.BaseDAO import BaseDAO
from ..models.repository import RepositoryConfig
class RepositoryConfigDAO(BaseDAO):
client: Client

Expand Down
3 changes: 2 additions & 1 deletion server/event_handler/discussion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from typing import Any
from github import Github, Auth
from github import GithubException
from agent.qa_chat import agent_chat

from petercat_utils.data_class import ChatData, Message, TextContentBlock

from ..agent.qa_chat import agent_chat


class DiscussionEventHandler:
event: Any
Expand Down
3 changes: 2 additions & 1 deletion server/event_handler/issue.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Any
from github import Github, Auth
from github import GithubException
from agent.qa_chat import agent_chat

from petercat_utils.data_class import ChatData, Message, TextContentBlock

from ..agent.qa_chat import agent_chat


class IssueEventHandler:
event: Any
Expand Down
13 changes: 7 additions & 6 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


# Import fastapi routers
from routers import bot, health_checker, github, rag, auth, chat, task
from .routers import bot, health_checker, github, rag, auth, chat, task

AUTH0_DOMAIN = get_env_variable("AUTH0_DOMAIN")
API_AUDIENCE = get_env_variable("API_IDENTIFIER")
CLIENT_ID = get_env_variable("AUTH0_CLIENT_ID")
Expand Down Expand Up @@ -50,8 +51,8 @@
app.include_router(task.router)


if __name__ == "__main__":
if is_dev:
uvicorn.run("main:app", host="0.0.0.0", port=int(os.environ.get("PORT", "8080")), reload=True)
else:
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "8080")))
# if __name__ == "__main__":
# if is_dev:
# uvicorn.run("main:app", host="0.0.0.0", port=int(os.environ.get("PORT", "8080")), reload=True)
# else:
# uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "8080")))
6 changes: 4 additions & 2 deletions server/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[pytest]
testpaths = tests
python_files = test_*.py
testpaths = .
python_files = test_*.py
cov=com
cov-report=xml,html
5 changes: 2 additions & 3 deletions server/routers/auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Annotated
from fastapi import APIRouter, Cookie, Request, HTTPException, status, Response

from fastapi.responses import RedirectResponse
import httpx

from petercat_utils import get_client, get_env_variable
from auth.get_user_info import generateAnonymousUser, getAnonymousUserInfoByToken, getUserAccessToken, getUserInfoByToken

from ..auth.get_user_info import generateAnonymousUser, getAnonymousUserInfoByToken, getUserInfoByToken

AUTH0_DOMAIN = get_env_variable("AUTH0_DOMAIN")

Expand Down
7 changes: 4 additions & 3 deletions server/routers/bot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter, Depends, status, Query, Path
from fastapi.responses import JSONResponse
from auth.get_user_info import get_user_id
from petercat_utils import get_client
from bot.builder import bot_builder, bot_info_generator
from type_class.bot import BotUpdateRequest, BotCreateRequest
from typing import Annotated, Optional

from ..auth.get_user_info import get_user_id
from ..bot.builder import bot_builder, bot_info_generator
from ..type_class.bot import BotUpdateRequest, BotCreateRequest

router = APIRouter(
prefix="/api/bot",
tags=["bot"],
Expand Down
10 changes: 5 additions & 5 deletions server/routers/chat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
from typing import Annotated, Optional
from fastapi import APIRouter, Cookie, Depends
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from auth.get_user_info import get_user_access_token, get_user_id
from petercat_utils.data_class import ChatData
from agent import qa_chat, bot_builder
from verify.rate_limit import verify_rate_limit

from ..agent import qa_chat, bot_builder
from ..verify.rate_limit import verify_rate_limit
from ..auth.get_user_info import get_user_access_token, get_user_id


router = APIRouter(
Expand Down
16 changes: 9 additions & 7 deletions server/routers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
from fastapi.responses import RedirectResponse
import requests
import time
from github import Auth, Github, Organization
from auth.get_user_info import get_user_access_token
from dao.authorizationDAO import AuthorizationDAO
from dao.repositoryConfigDAO import RepositoryConfigDAO
from models.repository import RepositoryConfig
from models.authorization import Authorization
from utils.github import get_handler, get_private_key
from github import Auth, Github

from petercat_utils import get_env_variable
from jwt import JWT, jwk_from_pem

from ..auth.get_user_info import get_user_access_token
from ..dao.authorizationDAO import AuthorizationDAO
from ..dao.repositoryConfigDAO import RepositoryConfigDAO
from ..models.repository import RepositoryConfig
from ..models.authorization import Authorization
from ..utils.github import get_handler, get_private_key

APP_ID = get_env_variable("X_GITHUB_APP_ID")
WEB_URL = get_env_variable("WEB_URL")

Expand Down
8 changes: 1 addition & 7 deletions server/routers/health_checker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from fastapi import APIRouter, Depends

from verify.rate_limit import verify_rate_limit
from fastapi import APIRouter

router = APIRouter(
prefix="/api",
Expand All @@ -11,7 +9,3 @@
@router.get("/health_checker")
def health_checker():
return { "Hello": "World" }

@router.get("/login_checker", dependencies=[Depends(verify_rate_limit)])
def login_checker():
return { "Hello": "World" }
4 changes: 3 additions & 1 deletion server/routers/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from fastapi import APIRouter, Depends
from petercat_utils.db.client.supabase import get_client
from verify.rate_limit import verify_rate_limit

from petercat_utils.data_class import RAGGitDocConfig, RAGGitIssueConfig, TaskType
from petercat_utils.rag_helper import (
Expand All @@ -14,6 +13,9 @@
git_issue_task,
)

from ..verify.rate_limit import verify_rate_limit


router = APIRouter(
prefix="/api",
tags=["rag"],
Expand Down
9 changes: 9 additions & 0 deletions server/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi.testclient import TestClient
from .main import app

client = TestClient(app)

def test_health_checker():
response = client.get("/api/health_checker")
assert response.status_code == 200
assert response.json() == { "Hello": "World" }
3 changes: 2 additions & 1 deletion server/tools/bot_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from langchain.tools import tool
from github import Github
from petercat_utils import get_client
from bot.builder import bot_builder

from ..bot.builder import bot_builder

g = Github()

Expand Down
2 changes: 1 addition & 1 deletion server/tools/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from github import Auth, Github
from langchain.tools import tool

from tools.helper import need_github_login
from ..tools.helper import need_github_login

DEFAULT_REPO_NAME = "ant-design/ant-design"

Expand Down
6 changes: 3 additions & 3 deletions server/utils/github.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Union
import boto3
from botocore.exceptions import ClientError
from event_handler.pull_request import PullRequestEventHandler
from event_handler.discussion import DiscussionEventHandler
from event_handler.issue import IssueEventHandler
from petercat_utils import get_env_variable
from github import Auth

from ..event_handler.pull_request import PullRequestEventHandler
from ..event_handler.discussion import DiscussionEventHandler
from ..event_handler.issue import IssueEventHandler

APP_ID = get_env_variable("X_GITHUB_APP_ID")

Expand Down
3 changes: 2 additions & 1 deletion server/verify/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from fastapi import Cookie, HTTPException
from datetime import datetime, timedelta

from auth.get_user_info import getUserInfoByToken
from petercat_utils import get_client, get_env_variable

from ..auth.get_user_info import getUserInfoByToken

RATE_LIMIT_ENABLED = get_env_variable("RATE_LIMIT_ENABLED", "False") == 'True'
RATE_LIMIT_REQUESTS = get_env_variable("RATE_LIMIT_REQUESTS") or 100
RATE_LIMIT_DURATION = timedelta(minutes=int(get_env_variable("RATE_LIMIT_DURATION") or 1))
Expand Down

0 comments on commit a4df0aa

Please sign in to comment.