From 8e1f58f6eb9e5fa0672c06d6cb296ca94bae36a7 Mon Sep 17 00:00:00 2001 From: Carly Gundy <47304080+cgundy@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:35:10 +0100 Subject: [PATCH] feat(IDX): switch to plaintext file (#74) * feat(IDX): switch to plaintext file * fix * allow config file to be empty --- .../bot_checks/check_bot_approved_files.py | 21 ++------ .../tests/test_data/BOT_APPROVED_FILES | 3 ++ .../tests/test_repo_policies.py | 50 ++++++------------- 3 files changed, 23 insertions(+), 51 deletions(-) create mode 100644 reusable_workflows/tests/test_data/BOT_APPROVED_FILES diff --git a/reusable_workflows/repo_policies/bot_checks/check_bot_approved_files.py b/reusable_workflows/repo_policies/bot_checks/check_bot_approved_files.py index 2ade101..aca0690 100644 --- a/reusable_workflows/repo_policies/bot_checks/check_bot_approved_files.py +++ b/reusable_workflows/repo_policies/bot_checks/check_bot_approved_files.py @@ -1,4 +1,3 @@ -import json import subprocess import github3 @@ -6,7 +5,7 @@ from check_membership.check_membership import is_approved_bot from shared.utils import download_gh_file, load_env_vars -BOT_APPROVED_FILES_PATH = ".github/repo_policies/bot_approved_files.json" +BOT_APPROVED_FILES_PATH = ".github/repo_policies/BOT_APPROVED_FILES" REQUIRED_ENV_VARS = [ "USER", "GH_TOKEN", @@ -46,17 +45,9 @@ def get_approved_files(config_file: str) -> list[str]: """ Extracts the list of approved files from the config file. """ - try: - config = json.loads(config_file) - except json.JSONDecodeError: - raise Exception("Config file is not a valid JSON file") - try: - approved_files = config["approved_files"] - except KeyError: - raise Exception("No approved_files key found in config file") - - if len(approved_files) == 0: - raise Exception("No approved files found in config file") + approved_files = [ + line for line in config_file.splitlines() if not line.strip().startswith("#") + ] return approved_files @@ -92,9 +83,7 @@ def main() -> None: block_pr = pr_is_blocked(env_vars) else: - print( - f"{user} is not a bot. Letting CLA check handle contribution decision." - ) + print(f"{user} is not a bot. Letting CLA check handle contribution decision.") block_pr = False subprocess.run(f"""echo 'block_pr={block_pr}' >> $GITHUB_OUTPUT""", shell=True) diff --git a/reusable_workflows/tests/test_data/BOT_APPROVED_FILES b/reusable_workflows/tests/test_data/BOT_APPROVED_FILES new file mode 100644 index 0000000..702da11 --- /dev/null +++ b/reusable_workflows/tests/test_data/BOT_APPROVED_FILES @@ -0,0 +1,3 @@ +# Comment +file1 +file2 diff --git a/reusable_workflows/tests/test_repo_policies.py b/reusable_workflows/tests/test_repo_policies.py index 6636517..a835012 100644 --- a/reusable_workflows/tests/test_repo_policies.py +++ b/reusable_workflows/tests/test_repo_policies.py @@ -55,37 +55,12 @@ def test_get_approved_files_config_fails(download_gh_file): def test_get_approved_files(): - config_file = '{"approved_files": ["file1.py", "file2.py"]}' + config_file = open( + "reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r" + ).read() approved_files = get_approved_files(config_file) - assert approved_files == ["file1.py", "file2.py"] - - -def test_get_approved_files_not_json(): - config_file = "not a json file" - - with pytest.raises(Exception) as exc: - get_approved_files(config_file) - - assert str(exc.value) == "Config file is not a valid JSON file" - - -def test_get_approved_files_no_approved_files(): - config_file = '{"another_key": ["file1.py", "file2.py"]}' - - with pytest.raises(Exception) as exc: - get_approved_files(config_file) - - assert str(exc.value) == "No approved_files key found in config file" - - -def test_get_approved_files_no_files(): - config_file = '{"approved_files": []}' - - with pytest.raises(Exception) as exc: - get_approved_files(config_file) - - assert str(exc.value) == "No approved files found in config file" + assert approved_files == ["file1", "file2"] @mock.patch("repo_policies.bot_checks.check_bot_approved_files.get_changed_files") @@ -105,10 +80,11 @@ def test_pr_is_blocked_false(gh_login, get_approved_files_config, get_changed_fi gh_login.return_value = gh repo = mock.Mock() gh.repository.return_value = repo - get_changed_files.return_value = ["file1.py", "file2.py"] - get_approved_files_config.return_value = ( - '{"approved_files": ["file1.py", "file2.py", "file3.py"]}' - ) + get_changed_files.return_value = ["file1", "file2"] + config_file = open( + "reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r" + ).read() + get_approved_files_config.return_value = config_file blocked = pr_is_blocked(env_vars) @@ -134,8 +110,11 @@ def test_pr_is_blocked_true(gh_login, get_approved_files_config, get_changed_fil gh_login.return_value = gh repo = mock.Mock() gh.repository.return_value = repo - get_changed_files.return_value = ["file1.py", "file2.py"] - get_approved_files_config.return_value = '{"approved_files": ["file1.py"]}' + get_changed_files.return_value = ["file1", "file2", "file3"] + config_file = open( + "reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r" + ).read() + get_approved_files_config.return_value = config_file blocked = pr_is_blocked(env_vars) @@ -160,6 +139,7 @@ def test_main_succeeds(subprocess_run, pr_is_blocked, is_approved_bot, load_env_ "echo 'block_pr=False' >> $GITHUB_OUTPUT", shell=True ) + @mock.patch("repo_policies.bot_checks.check_bot_approved_files.load_env_vars") @mock.patch("repo_policies.bot_checks.check_bot_approved_files.is_approved_bot") @mock.patch("repo_policies.bot_checks.check_bot_approved_files.pr_is_blocked")