diff --git a/src/hooks/validate_security_scan.py b/src/hooks/validate_security_scan.py index b8b4f301..8afbe2a3 100644 --- a/src/hooks/validate_security_scan.py +++ b/src/hooks/validate_security_scan.py @@ -63,7 +63,15 @@ async def run(self) -> ValidateSecurityScanResult: return ValidateSecurityScanResult(False, "No commit message provided") regex = re.compile(r"Signed-off-by", flags=re.DOTALL) - filtered_contents = [i for i in contents if not regex.match(i)] + + filtered_contents = [] + for i in contents: + if i == "# ------------------------ >8 ------------------------\n": + break + + if not regex.match(i): + filtered_contents.append(i) + filtered_contents.append(f"\n{SIGNED_OFF_BY_TRAILER}") logger.debug("New commit message is %s", "".join(filtered_contents)) diff --git a/tests/unit/hooks/test_validate_security_scan.py b/tests/unit/hooks/test_validate_security_scan.py index f9f22c3c..ee1399a8 100644 --- a/tests/unit/hooks/test_validate_security_scan.py +++ b/tests/unit/hooks/test_validate_security_scan.py @@ -1,8 +1,8 @@ -from anyio import NamedTemporaryFile -import src.hooks.config - from unittest.mock import patch +from anyio import NamedTemporaryFile + +import src.hooks.config from src.hooks.validate_security_scan import ValidateSecurityScan @@ -112,3 +112,50 @@ async def test_run_with_file_with_existing_signed_off_header_is_replaced(self): assert (await tf.read()).decode( "UTF-8" ) == f"A helpful commit message\n\n{src.hooks.config.SIGNED_OFF_BY_TRAILER}" + + async def test_run_with_file_with_commit_verbose_set_to_true(self): + async with NamedTemporaryFile() as tf: + with patch.object(ValidateSecurityScan, "validate_hook_settings", return_value=True): + await tf.writelines( + line + b"\n" + for line in [ + b"A helpful commit message", + b"# ------------------------ >8 ------------------------", + b"# Do not modify or remove the line above.", + b"# Everything below it will be ignored.", + ] + ) + await tf.seek(0) + + result = await ValidateSecurityScan(paths=[tf.name]).run() + + assert result.success is True + + assert (await tf.read()).decode( + "UTF-8" + ) == f"A helpful commit message\n\n{src.hooks.config.SIGNED_OFF_BY_TRAILER}" + + async def test_run_with_file_with_commit_verbose_set_to_true_with_body(self): + async with NamedTemporaryFile() as tf: + with patch.object(ValidateSecurityScan, "validate_hook_settings", return_value=True): + await tf.writelines( + line + b"\n" + for line in [ + b"A", + b"helpful", + b"commit", + b" message", + b"# ------------------------ >8 ------------------------", + b"# Do not modify or remove the line above.", + b"# Everything below it will be ignored.", + ] + ) + await tf.seek(0) + + result = await ValidateSecurityScan(paths=[tf.name]).run() + + assert result.success is True + + assert (await tf.read()).decode( + "UTF-8" + ) == f"A\nhelpful\ncommit\n message\n\n{src.hooks.config.SIGNED_OFF_BY_TRAILER}"