Skip to content

Commit

Permalink
feat(IDX): allow wildcards for bot files (#95)
Browse files Browse the repository at this point in the history
* chore(IDX): exclude blank lines

* feat(IDX): enable wildcards for bot approved files
  • Loading branch information
cgundy authored Dec 19, 2024
1 parent 9db6140 commit 8f0c9b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fnmatch
import subprocess
from typing import Optional

Expand Down Expand Up @@ -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:
"""
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions reusable_workflows/tests/test_data/BOT_APPROVED_FILES
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

file1
file2
folder/*.txt
28 changes: 27 additions & 1 deletion reusable_workflows/tests/test_repo_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 8f0c9b3

Please sign in to comment.