Skip to content

Commit

Permalink
feat(IDX): switch to plaintext file (#74)
Browse files Browse the repository at this point in the history
* feat(IDX): switch to plaintext file

* fix

* allow config file to be empty
  • Loading branch information
cgundy authored Dec 5, 2024
1 parent 79c273c commit 8e1f58f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 51 deletions.
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,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


Expand Down Expand Up @@ -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)
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
50 changes: 15 additions & 35 deletions reusable_workflows/tests/test_repo_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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")
Expand Down

0 comments on commit 8e1f58f

Please sign in to comment.