From 8f0c9b39476dae4532ce463c08ad1e8a043c1b63 Mon Sep 17 00:00:00 2001 From: Carly Gundy <47304080+cgundy@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:28:39 +0100 Subject: [PATCH] feat(IDX): allow wildcards for bot files (#95) * chore(IDX): exclude blank lines * feat(IDX): enable wildcards for bot approved files --- .../bot_checks/check_bot_approved_files.py | 12 +++++++- .../tests/test_data/BOT_APPROVED_FILES | 1 + .../tests/test_repo_policies.py | 28 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) 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 9e158cc..3654513 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,3 +1,4 @@ +import fnmatch import subprocess from typing import Optional @@ -61,6 +62,15 @@ def get_approved_files(config_file: str) -> list[str]: ] return approved_files +def check_files_in_approved_list(changed_files: list[str], approved_files: list[str]) -> bool: + """ + Checks if all the changed files are in the list of approved files. + """ + return all( + any(fnmatch.fnmatch(changed_file, pattern) for pattern in approved_files) + for changed_file in changed_files + ) + def check_if_pr_is_blocked(env_vars: dict) -> None: """ @@ -74,7 +84,7 @@ def check_if_pr_is_blocked(env_vars: dict) -> None: ) config = get_approved_files_config(repo) approved_files = get_approved_files(config) - block_pr = not all(file in approved_files for file in changed_files) + block_pr = not check_files_in_approved_list(changed_files, approved_files) print(f"changed_files: {changed_files}") print(f"approved_files: {approved_files}") if block_pr: diff --git a/reusable_workflows/tests/test_data/BOT_APPROVED_FILES b/reusable_workflows/tests/test_data/BOT_APPROVED_FILES index bb1f594..bd183c1 100644 --- a/reusable_workflows/tests/test_data/BOT_APPROVED_FILES +++ b/reusable_workflows/tests/test_data/BOT_APPROVED_FILES @@ -2,3 +2,4 @@ file1 file2 +folder/*.txt diff --git a/reusable_workflows/tests/test_repo_policies.py b/reusable_workflows/tests/test_repo_policies.py index 1ade7a8..8c2a909 100644 --- a/reusable_workflows/tests/test_repo_policies.py +++ b/reusable_workflows/tests/test_repo_policies.py @@ -5,6 +5,7 @@ from repo_policies.bot_checks.check_bot_approved_files import ( BOT_APPROVED_FILES_PATH, + check_files_in_approved_list, check_if_pr_is_blocked, get_approved_files, get_approved_files_config, @@ -62,7 +63,32 @@ def test_get_approved_files(): ).read() approved_files = get_approved_files(config_file) - assert approved_files == ["file1", "file2"] + assert approved_files == ["file1", "file2", "folder/*.txt"] + + +def get_test_approved_files(): + config_file = open( + "reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r" + ).read() + approved_files = get_approved_files(config_file) + return approved_files + + +def test_check_files_in_approved_list_succeeds(): + changed_files = ["file1", "file2", "folder/file3.txt"] + approved_files = get_test_approved_files() + + assert check_files_in_approved_list(changed_files, approved_files) + + changed_files = ["file1", "file2"] + + assert check_files_in_approved_list(changed_files, approved_files) + +def test_check_files_in_approved_list_fails(): + changed_files = ["file1", "file2", "folder/file3.txt", 'folder/file4.py'] + approved_files = get_test_approved_files() + + assert not check_files_in_approved_list(changed_files, approved_files) @mock.patch("repo_policies.bot_checks.check_bot_approved_files.get_changed_files")