Skip to content
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
6 changes: 3 additions & 3 deletions webhook_server_container/tests/test_pull_request_owners.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

import yaml

import pytest
from webhook_server_container.tests.conftest import ContentFile, Tree
from webhook_server_container.utils.constants import APPROVED_BY_LABEL_PREFIX

Expand All @@ -12,7 +12,7 @@ def __init__(self):
def get_git_tree(self, sha: str, recursive: bool):
return Tree("")

def get_contents(self, path: str):
def get_contents(self, path: str, ref):
owners_data = yaml.dump({
"approvers": ["root_approver1", "root_approver2"],
"reviewers": ["root_reviewer1", "root_reviewer2"],
Expand Down
28 changes: 12 additions & 16 deletions webhook_server_container/utils/github_repository_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def get_repo_branch_protection_rules(config_data: dict[str, Any], repo_data: dic
return repo_data


def set_repositories_settings(config_: Config, github_api: Github) -> None:
def set_repositories_settings(config_: Config) -> None:
logger = get_logger_with_params(name="github-repository-settings")

logger.info("Processing repositories")
Expand All @@ -220,7 +220,6 @@ def set_repositories_settings(config_: Config, github_api: Github) -> None:
set_repository,
**{
"data": data,
"github_api": github_api,
"default_status_checks": default_status_checks,
},
)
Expand All @@ -229,15 +228,17 @@ def set_repositories_settings(config_: Config, github_api: Github) -> None:
get_future_results(futures=futures)


def set_repository(
data: Dict[str, Any], github_api: Github, default_status_checks: List[str]
) -> Tuple[bool, str, Callable]:
def set_repository(data: Dict[str, Any], default_status_checks: List[str]) -> Tuple[bool, str, Callable]:
logger = get_logger_with_params(name="github-repository-settings")

repository: str = data["name"]
logger.info(f"Processing repository {repository}")
protected_branches: Dict[str, Any] = data.get("protected-branches", {})
repo_branch_protection_rules: Dict[str, Any] = data["branch_protection"]
github_api, _ = get_api_with_highest_rate_limit(config=config, repository_name=repository)
if not github_api:
return False, f"{repository}: Failed to get github api", logger.error

Comment on lines +231 to +241
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Refactored function with internal API retrieval and improved error handling.

The function now retrieves the GitHub API internally rather than accepting it as a parameter, and includes proper error handling for when API retrieval fails.

However, there's a bug in the implementation:

-    github_api, _ = get_api_with_highest_rate_limit(config=config, repository_name=repository)
+    github_api, _ = get_api_with_highest_rate_limit(config=config_, repository_name=repository)

The variable config is undefined. It should be config_, but this parameter is not available in this function's scope. This will cause a runtime error.

Consider passing the config as a parameter:

-def set_repository(data: Dict[str, Any], default_status_checks: List[str]) -> Tuple[bool, str, Callable]:
+def set_repository(data: Dict[str, Any], default_status_checks: List[str], config_: Config) -> Tuple[bool, str, Callable]:

And update the calling code in set_repositories_settings to pass the config.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def set_repository(data: Dict[str, Any], default_status_checks: List[str]) -> Tuple[bool, str, Callable]:
logger = get_logger_with_params(name="github-repository-settings")
repository: str = data["name"]
logger.info(f"Processing repository {repository}")
protected_branches: Dict[str, Any] = data.get("protected-branches", {})
repo_branch_protection_rules: Dict[str, Any] = data["branch_protection"]
github_api, _ = get_api_with_highest_rate_limit(config=config, repository_name=repository)
if not github_api:
return False, f"{repository}: Failed to get github api", logger.error
def set_repository(data: Dict[str, Any], default_status_checks: List[str], config_: Config) -> Tuple[bool, str, Callable]:
logger = get_logger_with_params(name="github-repository-settings")
repository: str = data["name"]
logger.info(f"Processing repository {repository}")
protected_branches: Dict[str, Any] = data.get("protected-branches", {})
repo_branch_protection_rules: Dict[str, Any] = data["branch_protection"]
github_api, _ = get_api_with_highest_rate_limit(config=config_, repository_name=repository)
if not github_api:
return False, f"{repository}: Failed to get github api", logger.error

repo = _get_github_repo_api(github_api=github_api, repository=repository)
if not repo:
return False, f"{repository}: Failed to get repository", logger.error
Expand Down Expand Up @@ -294,7 +295,7 @@ def set_repository(
return True, f"{repository}: Setting repository settings is done", logger.info


def set_all_in_progress_check_runs_to_queued(config_: Config, github_api: Github) -> None:
def set_all_in_progress_check_runs_to_queued(config_: Config) -> None:
check_runs = (
PYTHON_MODULE_INSTALL_STR,
CAN_BE_MERGED_STR,
Expand All @@ -306,6 +307,7 @@ def set_all_in_progress_check_runs_to_queued(config_: Config, github_api: Github

with ThreadPoolExecutor() as executor:
for _, data in config_.data["repositories"].items():
github_api, _ = get_api_with_highest_rate_limit(config=config, repository_name=data["name"])
futures.append(
executor.submit(
set_repository_check_runs_to_queued,
Expand Down Expand Up @@ -382,13 +384,7 @@ def get_repository_github_app_api(config_: Config, repository_name: str) -> Opti
logger = get_logger_with_params(name="github-repository-settings")

config = Config()
api, _ = get_api_with_highest_rate_limit(config=config)
if api:
set_repositories_settings(config_=config, github_api=api)
set_all_in_progress_check_runs_to_queued(
config_=config,
github_api=api,
)

else:
logger.error("Failed to get GitHub API")
set_repositories_settings(config_=config)
set_all_in_progress_check_runs_to_queued(
config_=config,
)
12 changes: 4 additions & 8 deletions webhook_server_container/utils/webhook.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Callable, Dict, List, Tuple

from github.Hook import Hook
from github import Github
from github.Hook import Hook

from webhook_server_container.libs.config import Config
from webhook_server_container.utils.helpers import (
Expand All @@ -12,7 +12,6 @@
get_logger_with_params,
)


LOGGER = get_logger_with_params(name="webhook")


Expand All @@ -39,13 +38,14 @@ def process_github_webhook(data: Dict[str, Any], github_api: Github, webhook_ip:
return True, f"{repository}: Create webhook is done", LOGGER.info


def create_webhook(config_: Config, github_api: Github) -> None:
def create_webhook(config_: Config) -> None:
LOGGER.info("Preparing webhook configuration")
webhook_ip = config_.data["webhook_ip"]

futures = []
with ThreadPoolExecutor() as executor:
for _, data in config_.data["repositories"].items():
github_api, _ = get_api_with_highest_rate_limit(config=config, repository_name=data["name"])
futures.append(
executor.submit(
process_github_webhook,
Expand All @@ -58,8 +58,4 @@ def create_webhook(config_: Config, github_api: Github) -> None:

if __name__ == "__main__":
config = Config()
api, _ = get_api_with_highest_rate_limit(config=config)
if api:
create_webhook(config_=config, github_api=api)
else:
LOGGER.error("Failed to get GitHub API")
create_webhook(config_=config)