Skip to content

Commit 48d4330

Browse files
committed
Make --disable-security-issue and --disable-overview suppress comments
Both flags were checked only after testing whether a comment of that type was already on the pull request, so they suppressed the first post and then updated that comment on every later run. --disable-security-issue in particular kept refreshing an existing comment with the full alerts table. The flags now mean the CLI does not manage that comment at all. An existing comment is left untouched rather than rewritten, since a body claiming no alerts would be inaccurate when reporting is merely switched off. Moves the decision into should_write_comment() so it is covered by tests directly; main_code() had no harness for this block. Bumps to 2.7.0 rather than a patch, since these flags change behavior.
1 parent 4baec9b commit 48d4330

3 files changed

Lines changed: 92 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
once per ignore command, and an ignore-all comment produces none, so no rows
4040
were removed.
4141

42+
### Fixed: `--disable-security-issue` and `--disable-overview` now suppress the comment entirely
43+
44+
- Both flags were checked only after testing whether a comment of that type was
45+
already on the pull request, so they suppressed the first post and then
46+
updated that comment on every later run. `--disable-security-issue` in
47+
particular kept refreshing an existing comment with the full alerts table.
48+
- The flags now mean the CLI does not manage that comment at all. An existing
49+
comment is left untouched rather than being rewritten, since a body claiming
50+
no alerts would be inaccurate when reporting is merely switched off.
51+
- The decision moved into `should_write_comment()` so it is covered directly by
52+
tests.
53+
4254
## 2.6.11
4355

4456
### Changed: bump pinned @coana-tech/cli to 15.10.32

socketsecurity/socketcli.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ def get_api_request_timeout(config: CliConfig) -> int:
107107
return config.timeout if config.timeout is not None else DEFAULT_API_TIMEOUT
108108

109109

110+
def should_write_comment(disabled: bool, has_findings: bool, update_existing: bool) -> bool:
111+
"""
112+
Decides whether a Socket comment should be written for this run.
113+
114+
:param disabled: bool - The comment type is switched off by flag.
115+
:param has_findings: bool - There is something to report this run.
116+
:param update_existing: bool - A comment of this type is already on the PR.
117+
:return: bool - True when the comment should be posted or updated.
118+
"""
119+
if disabled:
120+
# The flag means the CLI does not manage this comment at all. Checking
121+
# update_existing first only suppressed the very first post, so any
122+
# comment already on the pull request kept being updated.
123+
return False
124+
if not has_findings:
125+
# Nothing to report: refresh an existing comment so a resolved alerts
126+
# table is cleared, but do not open a new one.
127+
return update_existing
128+
return True
129+
130+
110131
def build_socket_sdk(config: CliConfig) -> socketdev:
111132
cli_user_agent_string = f"SocketPythonCLI/{config.version}"
112133
return socketdev(
@@ -780,9 +801,6 @@ def _is_unprocessed(c):
780801

781802
security_comment = Messages.security_comment_template(diff, config)
782803

783-
new_security_comment = True
784-
new_overview_comment = True
785-
786804
update_old_security_comment = (
787805
security_comment is None or
788806
security_comment == "" or
@@ -795,20 +813,22 @@ def _is_unprocessed(c):
795813
(len(comments) != 0 and comments.get("overview") is not None)
796814
)
797815

798-
if len(diff.new_alerts) == 0 or config.disable_security_issue:
799-
if not update_old_security_comment:
800-
new_security_comment = False
801-
log.debug("No new alerts or security issue comment disabled")
802-
else:
803-
log.debug("Updated security comment with no new alerts")
804-
816+
new_security_comment = should_write_comment(
817+
config.disable_security_issue,
818+
len(diff.new_alerts) > 0,
819+
update_old_security_comment,
820+
)
821+
if not new_security_comment:
822+
log.debug("Security issue comment disabled, or no alerts and none to update")
823+
805824
# FIXME: diff.new_packages is never populated, neither is removed_packages
806-
if (len(diff.new_packages) == 0) or config.disable_overview:
807-
if not update_old_overview_comment:
808-
new_overview_comment = False
809-
log.debug("No new/removed packages or Dependency Overview comment disabled")
810-
else:
811-
log.debug("Updated overview comment with no dependencies")
825+
new_overview_comment = should_write_comment(
826+
config.disable_overview,
827+
len(diff.new_packages) > 0,
828+
update_old_overview_comment,
829+
)
830+
if not new_overview_comment:
831+
log.debug("Dependency Overview comment disabled, or no packages and none to update")
812832

813833
log.debug(f"Adding comments for {config.scm}")
814834
scm.add_socket_comments(

tests/unit/test_socketcli.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from socketsecurity.core.classes import Diff, Package
66
from socketsecurity import socketcli
7-
from socketsecurity.socketcli import build_license_artifact_payload
7+
from socketsecurity.socketcli import build_license_artifact_payload, should_write_comment
88

99

1010
# ---------------------------------------------------------------------------
@@ -228,3 +228,46 @@ class Config:
228228
assert payload["deepDependencies"] == []
229229
assert payload["copyrightsByLicense"] == {}
230230
assert payload["licenses"] == {}
231+
232+
233+
# ---------------------------------------------------------------------------
234+
# Comment write decision.
235+
#
236+
# --disable-security-issue used to be checked only after the "is there already
237+
# a comment" test, so it suppressed the first post on a pull request and then
238+
# updated that comment with the full alerts table on every later run.
239+
# ---------------------------------------------------------------------------
240+
241+
242+
class TestShouldWriteComment:
243+
def test_disabled_never_writes_even_when_a_comment_exists(self):
244+
assert should_write_comment(
245+
disabled=True, has_findings=True, update_existing=True
246+
) is False
247+
248+
def test_disabled_never_writes_with_no_existing_comment(self):
249+
assert should_write_comment(
250+
disabled=True, has_findings=True, update_existing=False
251+
) is False
252+
253+
def test_disabled_wins_over_findings(self):
254+
"""The flag is not conditional on there being nothing to report."""
255+
assert should_write_comment(
256+
disabled=True, has_findings=False, update_existing=True
257+
) is False
258+
259+
def test_findings_are_written(self):
260+
assert should_write_comment(
261+
disabled=False, has_findings=True, update_existing=False
262+
) is True
263+
264+
def test_no_findings_refreshes_an_existing_comment(self):
265+
"""So a resolved alerts table gets cleared rather than left stale."""
266+
assert should_write_comment(
267+
disabled=False, has_findings=False, update_existing=True
268+
) is True
269+
270+
def test_no_findings_does_not_open_a_new_comment(self):
271+
assert should_write_comment(
272+
disabled=False, has_findings=False, update_existing=False
273+
) is False

0 commit comments

Comments
 (0)