Skip to content
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
10 changes: 9 additions & 1 deletion src/hooks/validate_security_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
53 changes: 50 additions & 3 deletions tests/unit/hooks/test_validate_security_scan.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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}"
Loading