From 16912592b1b55cd763abcd1c902408de8a8ac790 Mon Sep 17 00:00:00 2001 From: Hiroshi Nishio <4620828+hiroshinishio@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:14:13 +0900 Subject: [PATCH 1/2] Improve GitAuto self-assignment feature. --- .vscode/settings.json | 2 +- scheduler.py | 41 +++++-------------------------- services/github/github_manager.py | 24 ++++++++++++++++-- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 63ffa0ac..02352ae2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,5 @@ "python.analysis.autoFormatStrings": true, "python.analysis.addImport.heuristics": true, "python.analysis.autoImportCompletions": true, - "python.analysis.typeCheckingMode": "off" + "python.analysis.typeCheckingMode": "off", } \ No newline at end of file diff --git a/scheduler.py b/scheduler.py index fca4016b..403b89c9 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,9 +1,8 @@ """This is scheduled to run by AWS Lambda""" -import asyncio -from config import SUPABASE_SERVICE_ROLE_KEY, SUPABASE_URL -from services.gitauto_handler import handle_gitauto +from config import PRODUCT_ID, SUPABASE_SERVICE_ROLE_KEY, SUPABASE_URL from services.github.github_manager import ( + add_label_to_issue, get_installation_access_token, get_oldest_unassigned_open_issue, ) @@ -24,44 +23,16 @@ def schedule_handler(event, context) -> dict[str, int]: # Identify the oldest open and unassigned issue for each repository. owner_id = 159883862 # gitautoai owner = "gitautoai" - owner_type = "Organization" repo = "gitauto" - default_branch = "main" - user_id = 4620828 - user = "hiroshinishio" installation_id = supabase_manager.get_installation_id(owner_id=owner_id) token: str = get_installation_access_token(installation_id=installation_id) issue: IssueInfo | None = get_oldest_unassigned_open_issue( owner=owner, repo=repo, token=token ) + issue_number = issue["number"] - # Resolve that issue. - payload = ( - { - "issue": { - "number": issue["number"], - "title": issue["title"], - "body": issue["body"], - }, - "installation": { - "id": installation_id, - }, - "repository": { - "owner": { - "type": owner_type, - "id": owner_id, - "login": owner, - }, - "name": repo, - "default_branch": default_branch, - }, - "sender": { - "id": user_id, - "login": user, - }, - }, + # Label the issue with the product ID to trigger GitAuto. + add_label_to_issue( + owner=owner, repo=repo, issue_number=issue_number, label=PRODUCT_ID, token=token ) - - # Use asyncio.run() to run the async function. - asyncio.run(main=handle_gitauto(payload=payload, trigger_type="label")) return {"statusCode": 200} diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 12daa0ce..898e980f 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -29,7 +29,11 @@ SUPABASE_SERVICE_ROLE_KEY, UTF8, ) -from services.github.github_types import GitHubContentInfo, GitHubLabeledPayload, IssueInfo +from services.github.github_types import ( + GitHubContentInfo, + GitHubLabeledPayload, + IssueInfo, +) from services.supabase import SupabaseManager from utils.file_manager import apply_patch, extract_file_name, run_command from utils.handle_exceptions import handle_exceptions @@ -41,6 +45,20 @@ ) +@handle_exceptions(default_return_value=None, raise_on_error=True) +def add_label_to_issue( + owner: str, repo: str, issue_number: int, label: str, token: str +) -> None: + """https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#add-labels-to-an-issue""" + response: requests.Response = requests.post( + url=f"{GITHUB_API_URL}/repos/{owner}/{repo}/issues/{issue_number}/labels", + headers=create_headers(token=token), + json={"labels": [label]}, + timeout=TIMEOUT_IN_SECONDS, + ) + response.raise_for_status() + + @handle_exceptions(default_return_value=None, raise_on_error=False) def add_reaction_to_issue( owner: str, repo: str, issue_number: int, content: str, token: str @@ -398,7 +416,9 @@ def get_latest_remote_commit_sha( @handle_exceptions(default_return_value=None, raise_on_error=False) -def get_oldest_unassigned_open_issue(owner: str, repo: str, token: str) -> IssueInfo | None: +def get_oldest_unassigned_open_issue( + owner: str, repo: str, token: str +) -> IssueInfo | None: """https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues""" response: requests.Response = requests.get( url=f"{GITHUB_API_URL}/repos/{owner}/{repo}/issues", From f6340d03cd29a5c233f67d20829d4cc46866725f Mon Sep 17 00:00:00 2001 From: Hiroshi Nishio <4620828+hiroshinishio@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:15:03 +0900 Subject: [PATCH 2/2] . --- cloudformation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudformation.yml b/cloudformation.yml index 0ad37089..c6bbdd54 100644 --- a/cloudformation.yml +++ b/cloudformation.yml @@ -20,7 +20,7 @@ Resources: Properties: Name: SchedulerEventRule Description: "Schedule Lambda function to run every weekday at 0 AM UTC" - ScheduleExpression: cron(0 10 ? * MON-FRI *) # min hour day month day-of-week year + ScheduleExpression: cron(30 11 ? * MON-FRI *) # min hour day month day-of-week year State: ENABLED Targets: - Arn: !Ref LambdaFunctionArn