diff --git a/services/check_run_handler.py b/services/check_run_handler.py index 1681bcf3..d0104080 100644 --- a/services/check_run_handler.py +++ b/services/check_run_handler.py @@ -25,6 +25,7 @@ Repository, ) from services.github.pulls_manager import get_pull_request, get_pull_request_files +from services.github.utils import create_permission_url from services.openai.commit_changes import chat_with_agent from services.openai.chat import chat_with_ai from services.openai.instructions.identify_cause import IDENTIFY_CAUSE @@ -144,6 +145,12 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: workflow_path = get_workflow_run_path( owner=owner_name, repo=repo_name, run_id=workflow_run_id, token=token ) + permission_url = create_permission_url( + owner_type=owner_type, owner_name=owner_name, installation_id=installation_id + ) + if workflow_path == 404: + comment_body = f"Approve permission(s) to allow GitAuto to access the check run logs here: {permission_url}" + return update_comment(body=comment_body, base_args=base_args) workflow_content = get_remote_file_content( file_path=workflow_path, base_args=base_args ) @@ -160,11 +167,6 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: owner=owner_name, repo=repo_name, run_id=workflow_run_id, token=token ) if error_log == 404: - permission_url = ( - f"https://github.com/organizations/{owner_name}/settings/installations/{installation_id}/permissions/update" - if owner_type == "Organization" - else f"https://github.com/settings/installations/{installation_id}/permissions/update" - ) comment_body = f"Approve permission(s) to allow GitAuto to access the check run logs here: {permission_url}" return update_comment(body=comment_body, base_args=base_args) if error_log is None: diff --git a/services/github/actions_manager.py b/services/github/actions_manager.py index 39af2383..59dd3e4f 100644 --- a/services/github/actions_manager.py +++ b/services/github/actions_manager.py @@ -11,6 +11,8 @@ def get_failed_step_log_file_name(owner: str, repo: str, run_id: int, token: str url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/actions/runs/{run_id}/jobs" headers = create_headers(token=token) response = requests.get(url=url, headers=headers, timeout=TIMEOUT) + if response.status_code == 404 and "Not Found" in response.text: + return response.status_code response.raise_for_status() data = response.json() @@ -33,6 +35,8 @@ def get_workflow_run_path(owner: str, repo: str, run_id: int, token: str): url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/actions/runs/{run_id}" headers = create_headers(token=token) response = requests.get(url=url, headers=headers, timeout=TIMEOUT) + if response.status_code == 404 and "Not Found" in response.text: + return response.status_code response.raise_for_status() json = response.json() path: str = json["path"] @@ -53,6 +57,8 @@ def get_workflow_run_logs(owner: str, repo: str, run_id: int, token: str): failed_step_fname = get_failed_step_log_file_name( owner=owner, repo=repo, run_id=run_id, token=token ) + if failed_step_fname == 404: + return failed_step_fname # Read the content of the zip file with zipfile.ZipFile(io.BytesIO(response.content)) as zf: diff --git a/services/github/utils.py b/services/github/utils.py new file mode 100644 index 00000000..50424fd0 --- /dev/null +++ b/services/github/utils.py @@ -0,0 +1,11 @@ +from typing import Literal + + +def create_permission_url( + owner_type: Literal["Organization", "User"], owner_name: str, installation_id: int +): + url_base = "https://github.com" + url_part = f"settings/installations/{installation_id}/permissions/update" + if owner_type == "Organization": + return f"{url_base}/organizations/{owner_name}/{url_part}" + return f"{url_base}/{url_part}"