diff --git a/constants/messages.py b/constants/messages.py new file mode 100644 index 00000000..2d736ac3 --- /dev/null +++ b/constants/messages.py @@ -0,0 +1 @@ +CLICK_THE_CHECKBOX = "Click the checkbox below to generate a PR!" diff --git a/config/settings.py b/constants/settings.py similarity index 100% rename from config/settings.py rename to constants/settings.py diff --git a/services/gitauto_handler.py b/services/gitauto_handler.py index f4bdda0f..b687b13e 100644 --- a/services/gitauto_handler.py +++ b/services/gitauto_handler.py @@ -13,6 +13,7 @@ SUPABASE_SERVICE_ROLE_KEY, PR_BODY_STARTS_WITH, ) +from services.github.comment_manager import delete_my_comments from services.github.github_manager import ( create_pull_request, create_remote_branch, @@ -85,6 +86,9 @@ async def handle_gitauto( ) ) + # Delete all comments made by GitAuto except the one with the checkbox to clean up the issue + delete_my_comments(base_args=base_args) + # Notify the user if the request limit is reached and early return if requests_left <= 0 and IS_PRD and owner_name not in EXCEPTION_OWNERS: body = request_limit_reached( diff --git a/services/github/comment_manager.py b/services/github/comment_manager.py new file mode 100644 index 00000000..3b4bb4f5 --- /dev/null +++ b/services/github/comment_manager.py @@ -0,0 +1,50 @@ +from requests import delete, get +from config import GITHUB_API_URL, TIMEOUT +from constants.messages import CLICK_THE_CHECKBOX +from services.github.create_headers import create_headers +from services.github.github_types import BaseArgs +from utils.handle_exceptions import handle_exceptions + + +@handle_exceptions(default_return_value=None, raise_on_error=False) +def delete_a_comment(base_args: BaseArgs, comment_id: str): + """https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#delete-an-issue-comment""" + owner, repo, token = (base_args["owner"], base_args["repo"], base_args["token"]) + url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/issues/comments/{comment_id}" + headers: dict[str, str] = create_headers(token=token) + response = delete(url=url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + + +@handle_exceptions(default_return_value=None, raise_on_error=False) +def get_all_comments(base_args: BaseArgs): + """https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#list-issue-comments""" + owner, repo, issue_number, token = ( + base_args["owner"], + base_args["repo"], + base_args["issue_number"], + base_args["token"], + ) + url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/issues/{issue_number}/comments" + headers: dict[str, str] = create_headers(token=token) + response = get(url=url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + comments: list[dict] = response.json() + # print(f"All comments: {dumps(obj=comments, indent=2)}") + return comments + + +def filter_my_comments(comments: list[dict]): + """Filter out comments made by GitAuto except the one with the checkbox""" + return [ + comment for comment in comments if CLICK_THE_CHECKBOX not in comment["body"] + ] + + +def delete_my_comments(base_args: BaseArgs): + """Delete all comments made by GitAuto except the one with the checkbox""" + comments = get_all_comments(base_args) + my_comments = filter_my_comments(comments) + # print(f"My comments: {dumps(obj=my_comments, indent=2)}") + for comment in my_comments: + delete_a_comment(base_args=base_args, comment_id=comment["id"]) diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 6207a6ac..bbe28fe9 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -38,6 +38,7 @@ SUPABASE_SERVICE_ROLE_KEY, UTF8, ) +from constants.messages import CLICK_THE_CHECKBOX from services.github.create_headers import create_headers from services.github.github_types import ( BaseArgs, @@ -269,7 +270,7 @@ def create_comment_on_issue_with_gitauto_button(payload: GitHubLabeledPayload) - ) ) - body = "Click the checkbox below to generate a PR!\n- [ ] Generate PR" + body = f"{CLICK_THE_CHECKBOX}\n- [ ] Generate PR" if PRODUCT_ID != "gitauto": body += " - " + PRODUCT_ID diff --git a/services/github/utilities.py b/services/github/github_utils.py similarity index 92% rename from services/github/utilities.py rename to services/github/github_utils.py index 04a7a0b2..25c088ce 100644 --- a/services/github/utilities.py +++ b/services/github/github_utils.py @@ -14,7 +14,7 @@ get_installation_access_token, get_user_public_email, ) -from utils.extract_urls import extract_urls +from utils.extract_urls import extract_image_urls, extract_urls from utils.handle_exceptions import handle_exceptions @@ -65,6 +65,10 @@ def deconstruct_github_payload(payload: GitHubLabeledPayload): # Extract other information github_urls, other_urls = extract_urls(text=issue_body) + # print(f"github_urls: {github_urls}") + # print(f"other_urls: {other_urls}") + image_urls = extract_image_urls(text=issue_body) + # print(f"image_urls: {image_urls}") installation_id: int = payload["installation"]["id"] token: str = get_installation_access_token(installation_id=installation_id) sender_email: str = get_user_public_email(username=sender_name, token=token) @@ -91,6 +95,7 @@ def deconstruct_github_payload(payload: GitHubLabeledPayload): "is_automation": is_automation, "reviewers": reviewers, "github_urls": github_urls, + "image_urls": image_urls, "other_urls": other_urls, }