Skip to content

refc: basic refactoring of code files #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions api/github_helper/pull_requests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from api.github_helper.utils import get_diff_text
from api.github_helper.installation import get_installation_access_token
from cloudcode.actions.reviews import CodeReviewer
import requests
import logging
from api.github_helper.permissions import PULL_REQUEST_PERMISSION
import os
from api.github_helper.utils import get_diff_text
from api.github_helper.installation import get_installation_access_token
from api.github_helper.permissions import PULL_REQUEST_PERMISSION
from cloudcode.reviewer.code_review import CodeReviewer


logger = logging.getLogger(__name__)

Expand Down
31 changes: 16 additions & 15 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from api.github_helper.utils import is_github_signature_valid
from cloudcode.utils.config import CONFIG_DATA
import logging
from cloudcode.actions.ui_tests import UITester

# from cloudcode.generator.ui import UITester

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
Expand Down Expand Up @@ -46,22 +47,22 @@ async def handle_webhook(request: Request, background_tasks: BackgroundTasks):
return JSONResponse(content={"message": "Webhook received"})


@app.post("/generate-ui-tests")
async def generate_ui_tests(request: Request):
ui_tester = UITester()
data = await request.json()
web_url = data.get('web_url')
tests = ui_tester.generate_ui_tests(web_url=web_url)
return JSONResponse(content={"ui_tests": tests})
# @app.post("/generate-ui-tests")
# async def generate_ui_tests(request: Request):
# ui_tester = UITester()
# data = await request.json()
# web_url = data.get('web_url')
# tests = ui_tester.generate_ui_tests(web_url=web_url)
# return JSONResponse(content={"ui_tests": tests})


@app.post("/run-ui-tests")
async def run_ui_tests(request: Request):
ui_tester = UITester()
data = await request.json()
ui_tests = data.get('ui_tests')
test_result = ui_tester.run_tests(ui_tests)
return JSONResponse(content={"test_result": test_result})
# @app.post("/run-ui-tests")
# async def run_ui_tests(request: Request):
# ui_tester = UITester()
# data = await request.json()
# ui_tests = data.get('ui_tests')
# test_result = ui_tester.run_tests(ui_tests)
# return JSONResponse(content={"test_result": test_result})


@app.get("/")
Expand Down
Empty file added cloudcode/actors/__init__.py
Empty file.
Empty file added cloudcode/generator/__init__.py
Empty file.
68 changes: 23 additions & 45 deletions cloudcode/actions/ui_tests.py → cloudcode/generator/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cloudcode.llms.prompts import (
UI_MODULES_PROMPT,
UI_TESTS_SYSTEM_PROMPT,
PLAYWRIGHT_CODE_PROMPT
PLAYWRIGHT_CODE_PROMPT,
)
import logging
import subprocess
Expand All @@ -14,14 +14,9 @@
class UITester:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.provider = LLMProvider(
system_prompt=UI_TESTS_SYSTEM_PROMPT
)
self.provider = LLMProvider(system_prompt=UI_TESTS_SYSTEM_PROMPT)

def generate_ui_tests(
self,
web_url: str
):
def generate_ui_tests(self, web_url: str):
"""
This method generates UI tests with cypress code for a given web URL.
"""
Expand All @@ -31,86 +26,69 @@ def generate_ui_tests(

return ui_tests

def extract_webpage(
self,
web_url: str
):
def extract_webpage(self, web_url: str):
"""
This method extracts the code for a given web URL.
"""

html = output.get_web_html(web_url)
return html

def identify_modules(
self,
web_content: str,
user: Optional[str] = None
):
def identify_modules(self, web_content: str, user: Optional[str] = None):
"""
This method identifies the different UI modules from a webpage.
"""

prompt = UI_MODULES_PROMPT.format(
WEB_CONTENT=web_content
)

prompt = UI_MODULES_PROMPT.format(WEB_CONTENT=web_content)

resp = self.provider.chat_completion(prompt, user=user)

modules = parser.extract_multi_json(resp)

return modules

def generate_playwright_code(
self,
web_content: str,
test_description: str,
web_url: str,
user: Optional[str] = None
self,
web_content: str,
test_description: str,
web_url: str,
user: Optional[str] = None,
):
"""
This method generates playwright code for a particular UI test.
"""
prompt = PLAYWRIGHT_CODE_PROMPT.format(
WEB_CONTENT=web_content,
TEST_DESCRIPTION=test_description,
URL=web_url
WEB_CONTENT=web_content, TEST_DESCRIPTION=test_description, URL=web_url
)

resp = self.provider.chat_completion(prompt, user=user)

return resp

def generate_module_tests(
self,
web_content: str,
test_modules: dict,
web_url: str
):
def generate_module_tests(self, web_content: str, test_modules: dict, web_url: str):
"""
This method generates UI testing points for all modules.
"""
ui_tests = test_modules
for module in ui_tests:
for test in module["tests"]:
test_description = test["test_description"]
playwright_code = self.generate_playwright_code(web_content, test_description, web_url)
playwright_code = self.generate_playwright_code(
web_content, test_description, web_url
)
test["code"] = playwright_code
test["status"] = "Not run"

return ui_tests

def run_tests(
self,
ui_tests: dict
):

def run_tests(self, ui_tests: dict):
"""
This method runs playwright tests and updates logs and status accordingly.
"""
subprocess.run(['playwright', 'install', '--with-deps'], check=True)
subprocess.run(["playwright", "install", "--with-deps"], check=True)
test_result = ui_tests
for module in test_result:
for test in module["tests"]:
test["logs"], test["status"] = helper.run_test(test["code"])

return test_result
8 changes: 4 additions & 4 deletions cloudcode/helpers/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def create_pr_description(data, original_desc):

async def get_html(url):
async with async_playwright() as p:
subprocess.run(['playwright', 'install', '--with-deps'], check=True)
subprocess.run(["playwright", "install", "--with-deps"], check=True)
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
await page.goto(url)
Expand All @@ -78,10 +78,10 @@ async def get_html(url):
def get_web_html(url):
nest_asyncio.apply()
html = asyncio.run(get_html(url))
soup = BeautifulSoup(html, 'html.parser')
soup = BeautifulSoup(html, "html.parser")

for svg in soup.find_all('svg'):
for svg in soup.find_all("svg"):
svg.decompose()

pretty_html = soup.prettify()
return pretty_html
Empty file.
28 changes: 16 additions & 12 deletions cloudcode/playwright/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def run_test(code):
temp_file_path = 'cloudcode/playwright/tests/temp.spec.js'
temp_file_path = "cloudcode/playwright/tests/temp.spec.js"
create_test_spec(code, temp_file_path)
result_json = run_test_script()
logs, test_result = extract_result(result_json)
Expand All @@ -15,20 +15,22 @@ def run_test(code):


def create_test_spec(code, path):
with open(path, 'w+') as test_file:
match = re.search(r'```(?:javascript)?\n(.*)\n```', code, re.DOTALL)
with open(path, "w+") as test_file:
match = re.search(r"```(?:javascript)?\n(.*)\n```", code, re.DOTALL)

if match:
test_code = match.group(1)
with open(path, 'w+') as test_file:
with open(path, "w+") as test_file:
test_file.write(test_code)


def run_test_script():
output = subprocess.run(['npx', 'playwright', 'test'],
cwd="cloudcode/playwright",
capture_output=True,
text=True)
output = subprocess.run(
["npx", "playwright", "test"],
cwd="cloudcode/playwright",
capture_output=True,
text=True,
)
result = json.loads(output.stdout)
return result

Expand All @@ -38,16 +40,18 @@ def extract_result(result_json):
test_result = "Success"

for suite in result_json["suites"]:
if suite["file"] == 'temp.spec.js':
if suite["file"] == "temp.spec.js":
for _, spec in enumerate(suite["specs"]):
if spec["ok"] is False:
test_status = "Failed"
test_result = "Failed"
else:
test_status = "Success"

logs.append(f'Test for {spec["tests"][0]["projectName"]} - {test_status}')


logs.append(
f'Test for {spec["tests"][0]["projectName"]} - {test_status}'
)

return logs, test_result


Expand Down
Empty file added cloudcode/reviewer/__init__.py
Empty file.
File renamed without changes.
30 changes: 0 additions & 30 deletions cloudcode/templates/pull_request_review.md

This file was deleted.

Empty file added examples/basic/main.py
Empty file.
3 changes: 2 additions & 1 deletion tests/helpers/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_json_to_markdown(test_data, capfd):
comment="The code could be optimized for better performance.",
reasoning="There are some inefficient loops and data structures used.",
confidence="Medium",
) + "\n"
)
+ "\n"
)
expected_output += "### Security\n\n"
expected_output += (
Expand Down
Loading