Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(IDX): allow wildcards for bot files #95

Merged
merged 3 commits into from
Dec 19, 2024
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
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
Loading