Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GitAuto self-assignment feature. #150

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading