Skip to content

Commit

Permalink
Merge pull request #150 from gitautoai/wes
Browse files Browse the repository at this point in the history
Wes
  • Loading branch information
hiroshinishio authored Jul 2, 2024
2 parents 9b58ba3 + f6340d0 commit 269d27e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
2 changes: 1 addition & 1 deletion cloudformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 6 additions & 35 deletions scheduler.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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}
24 changes: 22 additions & 2 deletions services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 269d27e

Please sign in to comment.