Skip to content

Commit 7a3f7ca

Browse files
committedApr 16, 2024··
refc: basic refactoring of code files
1 parent 8ad26b6 commit 7a3f7ca

File tree

13 files changed

+66
-111
lines changed

13 files changed

+66
-111
lines changed
 

‎api/github_helper/pull_requests.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from api.github_helper.utils import get_diff_text
2-
from api.github_helper.installation import get_installation_access_token
3-
from cloudcode.actions.reviews import CodeReviewer
41
import requests
52
import logging
6-
from api.github_helper.permissions import PULL_REQUEST_PERMISSION
73
import os
4+
from api.github_helper.utils import get_diff_text
5+
from api.github_helper.installation import get_installation_access_token
6+
from api.github_helper.permissions import PULL_REQUEST_PERMISSION
7+
from cloudcode.reviewer.code_review import CodeReviewer
8+
89

910
logger = logging.getLogger(__name__)
1011

‎api/main.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from api.github_helper.utils import is_github_signature_valid
1010
from cloudcode.utils.config import CONFIG_DATA
1111
import logging
12-
from cloudcode.actions.ui_tests import UITester
12+
13+
# from cloudcode.generator.ui import UITester
1314

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

4849

49-
@app.post("/generate-ui-tests")
50-
async def generate_ui_tests(request: Request):
51-
ui_tester = UITester()
52-
data = await request.json()
53-
web_url = data.get('web_url')
54-
tests = ui_tester.generate_ui_tests(web_url=web_url)
55-
return JSONResponse(content={"ui_tests": tests})
50+
# @app.post("/generate-ui-tests")
51+
# async def generate_ui_tests(request: Request):
52+
# ui_tester = UITester()
53+
# data = await request.json()
54+
# web_url = data.get('web_url')
55+
# tests = ui_tester.generate_ui_tests(web_url=web_url)
56+
# return JSONResponse(content={"ui_tests": tests})
5657

5758

58-
@app.post("/run-ui-tests")
59-
async def run_ui_tests(request: Request):
60-
ui_tester = UITester()
61-
data = await request.json()
62-
ui_tests = data.get('ui_tests')
63-
test_result = ui_tester.run_tests(ui_tests)
64-
return JSONResponse(content={"test_result": test_result})
59+
# @app.post("/run-ui-tests")
60+
# async def run_ui_tests(request: Request):
61+
# ui_tester = UITester()
62+
# data = await request.json()
63+
# ui_tests = data.get('ui_tests')
64+
# test_result = ui_tester.run_tests(ui_tests)
65+
# return JSONResponse(content={"test_result": test_result})
6566

6667

6768
@app.get("/")

‎cloudcode/actors/__init__.py

Whitespace-only changes.

‎cloudcode/generator/__init__.py

Whitespace-only changes.

‎cloudcode/actions/ui_tests.py ‎cloudcode/generator/ui.py

+23-45
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from cloudcode.llms.prompts import (
66
UI_MODULES_PROMPT,
77
UI_TESTS_SYSTEM_PROMPT,
8-
PLAYWRIGHT_CODE_PROMPT
8+
PLAYWRIGHT_CODE_PROMPT,
99
)
1010
import logging
1111
import subprocess
@@ -14,14 +14,9 @@
1414
class UITester:
1515
def __init__(self):
1616
self.logger = logging.getLogger(__name__)
17-
self.provider = LLMProvider(
18-
system_prompt=UI_TESTS_SYSTEM_PROMPT
19-
)
17+
self.provider = LLMProvider(system_prompt=UI_TESTS_SYSTEM_PROMPT)
2018

21-
def generate_ui_tests(
22-
self,
23-
web_url: str
24-
):
19+
def generate_ui_tests(self, web_url: str):
2520
"""
2621
This method generates UI tests with cypress code for a given web URL.
2722
"""
@@ -31,86 +26,69 @@ def generate_ui_tests(
3126

3227
return ui_tests
3328

34-
def extract_webpage(
35-
self,
36-
web_url: str
37-
):
29+
def extract_webpage(self, web_url: str):
3830
"""
3931
This method extracts the code for a given web URL.
4032
"""
4133

4234
html = output.get_web_html(web_url)
4335
return html
4436

45-
def identify_modules(
46-
self,
47-
web_content: str,
48-
user: Optional[str] = None
49-
):
37+
def identify_modules(self, web_content: str, user: Optional[str] = None):
5038
"""
5139
This method identifies the different UI modules from a webpage.
5240
"""
5341

54-
prompt = UI_MODULES_PROMPT.format(
55-
WEB_CONTENT=web_content
56-
)
57-
42+
prompt = UI_MODULES_PROMPT.format(WEB_CONTENT=web_content)
43+
5844
resp = self.provider.chat_completion(prompt, user=user)
5945

6046
modules = parser.extract_multi_json(resp)
6147

6248
return modules
63-
49+
6450
def generate_playwright_code(
65-
self,
66-
web_content: str,
67-
test_description: str,
68-
web_url: str,
69-
user: Optional[str] = None
51+
self,
52+
web_content: str,
53+
test_description: str,
54+
web_url: str,
55+
user: Optional[str] = None,
7056
):
7157
"""
7258
This method generates playwright code for a particular UI test.
7359
"""
7460
prompt = PLAYWRIGHT_CODE_PROMPT.format(
75-
WEB_CONTENT=web_content,
76-
TEST_DESCRIPTION=test_description,
77-
URL=web_url
61+
WEB_CONTENT=web_content, TEST_DESCRIPTION=test_description, URL=web_url
7862
)
79-
63+
8064
resp = self.provider.chat_completion(prompt, user=user)
8165

8266
return resp
8367

84-
def generate_module_tests(
85-
self,
86-
web_content: str,
87-
test_modules: dict,
88-
web_url: str
89-
):
68+
def generate_module_tests(self, web_content: str, test_modules: dict, web_url: str):
9069
"""
9170
This method generates UI testing points for all modules.
9271
"""
9372
ui_tests = test_modules
9473
for module in ui_tests:
9574
for test in module["tests"]:
9675
test_description = test["test_description"]
97-
playwright_code = self.generate_playwright_code(web_content, test_description, web_url)
76+
playwright_code = self.generate_playwright_code(
77+
web_content, test_description, web_url
78+
)
9879
test["code"] = playwright_code
9980
test["status"] = "Not run"
10081

10182
return ui_tests
102-
103-
def run_tests(
104-
self,
105-
ui_tests: dict
106-
):
83+
84+
def run_tests(self, ui_tests: dict):
10785
"""
10886
This method runs playwright tests and updates logs and status accordingly.
10987
"""
110-
subprocess.run(['playwright', 'install', '--with-deps'], check=True)
88+
subprocess.run(["playwright", "install", "--with-deps"], check=True)
11189
test_result = ui_tests
11290
for module in test_result:
11391
for test in module["tests"]:
11492
test["logs"], test["status"] = helper.run_test(test["code"])
115-
93+
11694
return test_result

‎cloudcode/helpers/output.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def create_pr_description(data, original_desc):
6666

6767
async def get_html(url):
6868
async with async_playwright() as p:
69-
subprocess.run(['playwright', 'install', '--with-deps'], check=True)
69+
subprocess.run(["playwright", "install", "--with-deps"], check=True)
7070
browser = await p.chromium.launch(headless=True)
7171
page = await browser.new_page()
7272
await page.goto(url)
@@ -78,10 +78,10 @@ async def get_html(url):
7878
def get_web_html(url):
7979
nest_asyncio.apply()
8080
html = asyncio.run(get_html(url))
81-
soup = BeautifulSoup(html, 'html.parser')
81+
soup = BeautifulSoup(html, "html.parser")
8282

83-
for svg in soup.find_all('svg'):
83+
for svg in soup.find_all("svg"):
8484
svg.decompose()
85-
85+
8686
pretty_html = soup.prettify()
8787
return pretty_html

‎cloudcode/integrations/__init__.py

Whitespace-only changes.

‎cloudcode/playwright/helper.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def run_test(code):
8-
temp_file_path = 'cloudcode/playwright/tests/temp.spec.js'
8+
temp_file_path = "cloudcode/playwright/tests/temp.spec.js"
99
create_test_spec(code, temp_file_path)
1010
result_json = run_test_script()
1111
logs, test_result = extract_result(result_json)
@@ -15,20 +15,22 @@ def run_test(code):
1515

1616

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

2121
if match:
2222
test_code = match.group(1)
23-
with open(path, 'w+') as test_file:
23+
with open(path, "w+") as test_file:
2424
test_file.write(test_code)
2525

2626

2727
def run_test_script():
28-
output = subprocess.run(['npx', 'playwright', 'test'],
29-
cwd="cloudcode/playwright",
30-
capture_output=True,
31-
text=True)
28+
output = subprocess.run(
29+
["npx", "playwright", "test"],
30+
cwd="cloudcode/playwright",
31+
capture_output=True,
32+
text=True,
33+
)
3234
result = json.loads(output.stdout)
3335
return result
3436

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

4042
for suite in result_json["suites"]:
41-
if suite["file"] == 'temp.spec.js':
43+
if suite["file"] == "temp.spec.js":
4244
for _, spec in enumerate(suite["specs"]):
4345
if spec["ok"] is False:
4446
test_status = "Failed"
4547
test_result = "Failed"
4648
else:
4749
test_status = "Success"
48-
49-
logs.append(f'Test for {spec["tests"][0]["projectName"]} - {test_status}')
50-
50+
51+
logs.append(
52+
f'Test for {spec["tests"][0]["projectName"]} - {test_status}'
53+
)
54+
5155
return logs, test_result
5256

5357

‎cloudcode/reviewer/__init__.py

Whitespace-only changes.
File renamed without changes.

‎cloudcode/templates/pull_request_review.md

-30
This file was deleted.

‎examples/basic/main.py

Whitespace-only changes.

‎tests/helpers/test_output.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def test_json_to_markdown(test_data, capfd):
5959
comment="The code could be optimized for better performance.",
6060
reasoning="There are some inefficient loops and data structures used.",
6161
confidence="Medium",
62-
) + "\n"
62+
)
63+
+ "\n"
6364
)
6465
expected_output += "### Security\n\n"
6566
expected_output += (

0 commit comments

Comments
 (0)
Please sign in to comment.