Skip to content

Commit

Permalink
Clean up all previous GitAuto comments when starting except its trigg…
Browse files Browse the repository at this point in the history
…er comment
  • Loading branch information
hiroshinishio committed Dec 13, 2024
1 parent a63171b commit c229aa9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions constants/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CLICK_THE_CHECKBOX = "Click the checkbox below to generate a PR!"
File renamed without changes.
4 changes: 4 additions & 0 deletions services/gitauto_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
50 changes: 50 additions & 0 deletions services/github/comment_manager.py
Original file line number Diff line number Diff line change
@@ -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"])
3 changes: 2 additions & 1 deletion services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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,
}

Expand Down

0 comments on commit c229aa9

Please sign in to comment.