Skip to content

Commit bbe170f

Browse files
committed
fix: clear stale labels when findings are downgraded, ignored, or resolved
Signed-off-by: lelia <2418071+lelia@users.noreply.github.com>
1 parent 6e63e27 commit bbe170f

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

socket_basics/core/notification/github_pr_notifier.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ def __init__(self, params: Dict[str, Any] | None = None):
3636

3737
def notify(self, facts: Dict[str, Any]) -> None:
3838
notifications = facts.get('notifications', []) or []
39+
labels_enabled = self.config.get('pr_labels_enabled', True)
3940

4041
if not isinstance(notifications, list):
4142
logger.error('GithubPRNotifier: only supports new format - list of dicts with title/content')
4243
return
43-
44-
if not notifications:
45-
logger.info('GithubPRNotifier: no notifications present; skipping')
46-
return
4744

4845
# Get full scan URL if available and store it for use in truncation
4946
self.full_scan_url = facts.get('full_scan_html_url')
@@ -58,6 +55,13 @@ def notify(self, facts: Dict[str, Any]) -> None:
5855
logger.warning('GithubPRNotifier: skipping invalid notification item: %s', type(item))
5956

6057
if not valid_notifications:
58+
if labels_enabled:
59+
pr_number = self._get_pr_number()
60+
if pr_number:
61+
self._reconcile_pr_labels(pr_number, [])
62+
else:
63+
logger.warning('GithubPRNotifier: unable to determine PR number for label reconciliation')
64+
logger.info('GithubPRNotifier: no notifications present; skipping comments')
6165
return
6266

6367
# Get PR number for current branch
@@ -117,7 +121,7 @@ def notify(self, facts: Dict[str, Any]) -> None:
117121
logger.error('GithubPRNotifier: failed to post individual comment')
118122

119123
# Add labels to PR if enabled
120-
if self.config.get('pr_labels_enabled', True) and pr_number:
124+
if labels_enabled and pr_number:
121125
labels = self._determine_pr_labels(valid_notifications)
122126
self._reconcile_pr_labels(pr_number, labels)
123127
def _managed_pr_label_config(self) -> Dict[str, str]:

tests/test_github_pr_notifier.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,20 @@ def test_reconcile_pr_labels_clears_managed_labels_when_none_desired(monkeypatch
8787

8888
assert success is True
8989
assert removed == ['security: high']
90+
91+
92+
def test_notify_reconciles_labels_even_when_notifications_are_empty(monkeypatch):
93+
notifier = GithubPRNotifier(
94+
{
95+
'repository': 'SocketDev/socket-basics',
96+
'pr_labels_enabled': True,
97+
}
98+
)
99+
100+
reconciled: list[tuple[int, list[str]]] = []
101+
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
102+
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: reconciled.append((pr_number, labels)) or True)
103+
104+
notifier.notify({'notifications': []})
105+
106+
assert reconciled == [(123, [])]

0 commit comments

Comments
 (0)