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): switch to plaintext file #74

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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,12 +1,11 @@
import json
import subprocess

import github3

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",
Expand Down Expand Up @@ -46,17 +45,11 @@ 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")

approved_files = [
line for line in config_file.splitlines() if not line.strip().startswith("#")
]
if len(approved_files) == 0:
raise Exception("No approved files found in config file")
raise ValueError("No approved files found in config file")
return approved_files
cgundy marked this conversation as resolved.
Show resolved Hide resolved


Expand Down Expand Up @@ -92,9 +85,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)
Expand Down
3 changes: 3 additions & 0 deletions reusable_workflows/tests/test_data/BOT_APPROVED_FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Comment
file1
file2
47 changes: 18 additions & 29 deletions reusable_workflows/tests/test_repo_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,18 @@ 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"]
assert approved_files == ["file1", "file2"]


def test_get_approved_files_not_json():
config_file = "not a json file"
def test_get_approved_files_empty():
config_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:
with pytest.raises(ValueError) as exc:
get_approved_files(config_file)

assert str(exc.value) == "No approved files found in config file"
Expand All @@ -105,10 +89,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)

Expand All @@ -134,8 +119,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)

Expand All @@ -160,6 +148,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")
Expand Down
Loading