From 82b9dceb154d8bd157df0fb49278956192ddf291 Mon Sep 17 00:00:00 2001 From: Harsh <115716485+Kaos599@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:33:54 +0530 Subject: [PATCH 1/4] Update diff_pr_test.py --- kaizen/tests/actions/diff_pr_test.py | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/kaizen/tests/actions/diff_pr_test.py b/kaizen/tests/actions/diff_pr_test.py index 5591dcea..638a2e93 100644 --- a/kaizen/tests/actions/diff_pr_test.py +++ b/kaizen/tests/actions/diff_pr_test.py @@ -1,3 +1,4 @@ +from pydantic import BaseModel, constr, conint, ValidationError import requests from kaizen.reviewer.code_review import CodeReviewer from kaizen.llms.provider import LLMProvider @@ -13,21 +14,30 @@ "Accept": "application/vnd.github.v3+json", } +# Pydantic model for validating inputs +class PRRequestModel(BaseModel): + owner: constr(regex=r"^[a-zA-Z0-9-]{1,39}$") + repo: constr(regex=r"^[a-zA-Z0-9_.-]{1,100}$") + pr_number: conint(gt=0) + +# Wrapper function to validate inputs +def validate_pr_request(owner, repo, pr_number): + return PRRequestModel(owner=owner, repo=repo, pr_number=pr_number) def get_pr_info(owner, repo, pr_number): - url = f"{GITHUB_API}/repos/{owner}/{repo}/pulls/{pr_number}" + validated_data = validate_pr_request(owner, repo, pr_number) + url = f"{GITHUB_API}/repos/{validated_data.owner}/{validated_data.repo}/pulls/{validated_data.pr_number}" response = requests.get(url, headers=headers) response.raise_for_status() return response.json() - def get_pr_files(owner, repo, pr_number): - url = f"{GITHUB_API}/repos/{owner}/{repo}/pulls/{pr_number}/files" + validated_data = validate_pr_request(owner, repo, pr_number) + url = f"{GITHUB_API}/repos/{validated_data.owner}/{validated_data.repo}/pulls/{validated_data.pr_number}/files" response = requests.get(url, headers=headers) response.raise_for_status() return response.json() - def get_diff(url): headers = { "Accept": "application/vnd.github.v3.diff", @@ -36,7 +46,6 @@ def get_diff(url): response = requests.get(url, headers=headers) return response.text - def main(owner, repo, pr_number): try: pr_info = get_pr_info(owner, repo, pr_number) @@ -44,6 +53,7 @@ def main(owner, repo, pr_number): print(f"\nDiff URL: {pr_info['diff_url']}") diff_text = get_diff(pr_info["diff_url"]) print(f"Diff: \n{diff_text}\n") + code_reviewer = CodeReviewer(llm_provider=LLMProvider()) reviews = code_reviewer.review_pull_request( pull_request_title=pr_info["title"], @@ -52,18 +62,17 @@ def main(owner, repo, pr_number): pull_request_files=pr_files, user="local_test", ) + print(json.dumps(reviews.topics, indent=2)) - print("Processing Reviews ....") + topics = clean_keys(reviews.topics, "moderate") review_desc = create_pr_review_text(topics) comments, topics = create_review_comments(topics) - + print(f"\n Review Desc: \n {review_desc}") - print(f"\nComments: \n{json.dumps(comments)}") - print("################### CODE DESC") desc_generator = PRDescriptionGenerator(llm_provider=LLMProvider()) description = desc_generator.generate_pull_request_desc( pull_request_title=pr_info["title"], @@ -75,12 +84,12 @@ def main(owner, repo, pr_number): print("Description: \n", description.desc) + except ValidationError as e: + print("Input validation error:", e.json()) except requests.exceptions.HTTPError as e: print(f"Error: {e}") - if __name__ == "__main__": main("Cloud-Code-AI", "kaizen", 252) - print("------------------- Multi File -------------------") main("Cloud-Code-AI", "kaizen", 222) From d27494093990b6b63befc0cbca149150c8127680 Mon Sep 17 00:00:00 2001 From: Harsh <115716485+Kaos599@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:46:05 +0530 Subject: [PATCH 2/4] Update diff_pr_test.py to support flake8 and Black --- kaizen/tests/actions/diff_pr_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kaizen/tests/actions/diff_pr_test.py b/kaizen/tests/actions/diff_pr_test.py index 638a2e93..3635fc6a 100644 --- a/kaizen/tests/actions/diff_pr_test.py +++ b/kaizen/tests/actions/diff_pr_test.py @@ -14,16 +14,19 @@ "Accept": "application/vnd.github.v3+json", } + # Pydantic model for validating inputs class PRRequestModel(BaseModel): owner: constr(regex=r"^[a-zA-Z0-9-]{1,39}$") repo: constr(regex=r"^[a-zA-Z0-9_.-]{1,100}$") pr_number: conint(gt=0) + # Wrapper function to validate inputs def validate_pr_request(owner, repo, pr_number): return PRRequestModel(owner=owner, repo=repo, pr_number=pr_number) + def get_pr_info(owner, repo, pr_number): validated_data = validate_pr_request(owner, repo, pr_number) url = f"{GITHUB_API}/repos/{validated_data.owner}/{validated_data.repo}/pulls/{validated_data.pr_number}" @@ -31,6 +34,7 @@ def get_pr_info(owner, repo, pr_number): response.raise_for_status() return response.json() + def get_pr_files(owner, repo, pr_number): validated_data = validate_pr_request(owner, repo, pr_number) url = f"{GITHUB_API}/repos/{validated_data.owner}/{validated_data.repo}/pulls/{validated_data.pr_number}/files" @@ -38,6 +42,7 @@ def get_pr_files(owner, repo, pr_number): response.raise_for_status() return response.json() + def get_diff(url): headers = { "Accept": "application/vnd.github.v3.diff", @@ -46,6 +51,7 @@ def get_diff(url): response = requests.get(url, headers=headers) return response.text + def main(owner, repo, pr_number): try: pr_info = get_pr_info(owner, repo, pr_number) @@ -89,6 +95,7 @@ def main(owner, repo, pr_number): except requests.exceptions.HTTPError as e: print(f"Error: {e}") + if __name__ == "__main__": main("Cloud-Code-AI", "kaizen", 252) print("------------------- Multi File -------------------") From 281e766d555432f7b26ea6af921bec47ea33516d Mon Sep 17 00:00:00 2001 From: Harsh <115716485+Kaos599@users.noreply.github.com> Date: Thu, 31 Oct 2024 06:03:51 +0000 Subject: [PATCH 3/4] Properly updated to fit Flake8 & Black --- kaizen/tests/actions/diff_pr_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/kaizen/tests/actions/diff_pr_test.py b/kaizen/tests/actions/diff_pr_test.py index 3635fc6a..8c291530 100644 --- a/kaizen/tests/actions/diff_pr_test.py +++ b/kaizen/tests/actions/diff_pr_test.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, constr, conint, ValidationError +from pydantic import BaseModel, Field, ValidationError import requests from kaizen.reviewer.code_review import CodeReviewer from kaizen.llms.provider import LLMProvider @@ -17,9 +17,9 @@ # Pydantic model for validating inputs class PRRequestModel(BaseModel): - owner: constr(regex=r"^[a-zA-Z0-9-]{1,39}$") - repo: constr(regex=r"^[a-zA-Z0-9_.-]{1,100}$") - pr_number: conint(gt=0) + owner: str = Field(..., regex=r"^[a-zA-Z0-9-]{1,39}$") + repo: str = Field(..., regex=r"^[a-zA-Z0-9_.-]{1,100}$") + pr_number: int = Field(..., gt=0) # Wrapper function to validate inputs @@ -59,7 +59,7 @@ def main(owner, repo, pr_number): print(f"\nDiff URL: {pr_info['diff_url']}") diff_text = get_diff(pr_info["diff_url"]) print(f"Diff: \n{diff_text}\n") - + code_reviewer = CodeReviewer(llm_provider=LLMProvider()) reviews = code_reviewer.review_pull_request( pull_request_title=pr_info["title"], @@ -68,14 +68,14 @@ def main(owner, repo, pr_number): pull_request_files=pr_files, user="local_test", ) - + print(json.dumps(reviews.topics, indent=2)) print("Processing Reviews ....") - + topics = clean_keys(reviews.topics, "moderate") review_desc = create_pr_review_text(topics) comments, topics = create_review_comments(topics) - + print(f"\n Review Desc: \n {review_desc}") print(f"\nComments: \n{json.dumps(comments)}") From 34ef5c7eada81677a0ba8e95a2a0348d09e3a181 Mon Sep 17 00:00:00 2001 From: Harsh <115716485+Kaos599@users.noreply.github.com> Date: Thu, 31 Oct 2024 06:08:36 +0000 Subject: [PATCH 4/4] Fixed Issues --- kaizen/tests/actions/diff_pr_test.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kaizen/tests/actions/diff_pr_test.py b/kaizen/tests/actions/diff_pr_test.py index 8c291530..d2a818a1 100644 --- a/kaizen/tests/actions/diff_pr_test.py +++ b/kaizen/tests/actions/diff_pr_test.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Field, ValidationError +from pydantic import BaseModel, Field, ValidationError, StrictStr import requests from kaizen.reviewer.code_review import CodeReviewer from kaizen.llms.provider import LLMProvider @@ -17,8 +17,10 @@ # Pydantic model for validating inputs class PRRequestModel(BaseModel): - owner: str = Field(..., regex=r"^[a-zA-Z0-9-]{1,39}$") - repo: str = Field(..., regex=r"^[a-zA-Z0-9_.-]{1,100}$") + owner: StrictStr = Field(..., min_length=1, max_length=39, regex=r"^[a-zA-Z0-9-]+$") + repo: StrictStr = Field( + ..., min_length=1, max_length=100, regex=r"^[a-zA-Z0-9_.-]+$" + ) pr_number: int = Field(..., gt=0)