forked from SocketDev/socket-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_pr_notifier.py
More file actions
349 lines (287 loc) · 11.4 KB
/
Copy pathtest_github_pr_notifier.py
File metadata and controls
349 lines (287 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from socket_basics.core.notification.github_pr_notifier import GithubPRNotifier
def _notification(summary: str) -> dict:
return {'title': 'Socket SAST JavaScript', 'content': summary}
def test_determine_pr_labels_prefers_highest_current_severity():
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_label_critical': 'security: critical',
'pr_label_high': 'security: high',
'pr_label_medium': 'security: medium',
'pr_label_low': 'security: low',
}
)
labels = notifier._determine_pr_labels(
[_notification('Critical: 0 | High: 1 | Medium: 2 | Low: 3')]
)
assert labels == ['security: high']
def test_determine_pr_labels_supports_low_severity():
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_label_low': 'security: low',
}
)
labels = notifier._determine_pr_labels(
[_notification('Critical: 0 | High: 0 | Medium: 0 | Low: 2')]
)
assert labels == ['security: low']
def test_reconcile_pr_labels_replaces_stale_managed_severity(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_label_critical': 'security: critical',
'pr_label_high': 'security: high',
'pr_label_medium': 'security: medium',
'pr_label_low': 'security: low',
}
)
removed: list[str] = []
added: list[str] = []
ensured: list[str] = []
monkeypatch.setattr(notifier, '_get_current_pr_label_names', lambda pr_number: ['security: critical', 'team: backend'])
monkeypatch.setattr(notifier, '_remove_pr_label', lambda pr_number, label: removed.append(label) or True)
monkeypatch.setattr(notifier, '_ensure_pr_labels_exist', lambda labels: ensured.extend(labels))
monkeypatch.setattr(notifier, '_add_pr_labels', lambda pr_number, labels: added.extend(labels) or True)
success = notifier._reconcile_pr_labels(123, ['security: medium'])
assert success is True
assert removed == ['security: critical']
assert ensured == ['security: medium']
assert added == ['security: medium']
def test_reconcile_pr_labels_clears_managed_labels_when_none_desired(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_label_critical': 'security: critical',
'pr_label_high': 'security: high',
'pr_label_medium': 'security: medium',
'pr_label_low': 'security: low',
}
)
removed: list[str] = []
monkeypatch.setattr(notifier, '_get_current_pr_label_names', lambda pr_number: ['security: high', 'docs'])
monkeypatch.setattr(notifier, '_remove_pr_label', lambda pr_number, label: removed.append(label) or True)
monkeypatch.setattr(notifier, '_ensure_pr_labels_exist', lambda labels: (_ for _ in ()).throw(AssertionError('should not ensure labels')))
monkeypatch.setattr(notifier, '_add_pr_labels', lambda pr_number, labels: (_ for _ in ()).throw(AssertionError('should not add labels')))
success = notifier._reconcile_pr_labels(123, [])
assert success is True
assert removed == ['security: high']
def test_notify_reconciles_labels_even_when_notifications_are_empty(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': True,
}
)
reconciled: list[tuple[int, list[str]]] = []
all_clear_calls: list[tuple[int, set[str]]] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: reconciled.append((pr_number, labels)) or True)
monkeypatch.setattr(
notifier,
'_replace_existing_sections_with_all_clear',
lambda pr_number, section_types=None: all_clear_calls.append((pr_number, section_types)) or None,
)
notifier.notify({'notifications': []})
assert reconciled == [(123, [])]
assert all_clear_calls == [(123, set())]
def test_notify_rewrites_existing_section_to_all_clear_when_notifications_are_empty(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': True,
}
)
comment_body = """<!-- sast-javascript start -->
## <img src="https://example.test/logo.png" width="24" height="24"> Socket SAST JavaScript
### Summary
🟡 Medium: 1
<!-- sast-javascript end -->"""
updated_bodies: list[str] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: True)
monkeypatch.setattr(notifier, '_get_pr_comments', lambda pr_number: [{'id': 99, 'body': comment_body}])
monkeypatch.setattr(
notifier,
'_update_comment',
lambda pr_number, comment_id, body: updated_bodies.append(body) or True,
)
notifier.notify(
{
'notifications': [],
'components': [
{
'alerts': [
{
'generatedBy': 'opengrep-javascript',
'subType': 'sast-javascript',
'action': 'ignore',
}
]
}
],
}
)
assert len(updated_bodies) == 1
assert 'Socket Basics found no active findings in the latest run.' in updated_bodies[0]
assert '<!-- sast-javascript start -->' in updated_bodies[0]
def test_notify_empty_sast_notifications_do_not_rewrite_unrelated_sections(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': True,
}
)
sast_comment = """<!-- sast-javascript start -->
## <img src="https://example.test/logo.png" width="24" height="24"> Socket SAST JavaScript
### Summary
🟡 Medium: 1
<!-- sast-javascript end -->"""
tier1_comment = """<!-- socket-tier1 start -->
## <img src="https://example.test/logo.png" width="24" height="24"> Socket Security Tier 1
### Summary
🟠 High: 2
<!-- socket-tier1 end -->"""
updated_comments: list[tuple[int, str]] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: True)
monkeypatch.setattr(
notifier,
'_get_pr_comments',
lambda pr_number: [
{'id': 99, 'body': sast_comment},
{'id': 100, 'body': tier1_comment},
],
)
monkeypatch.setattr(
notifier,
'_update_comment',
lambda pr_number, comment_id, body: updated_comments.append((comment_id, body)) or True,
)
notifier.notify(
{
'notifications': [],
'components': [
{
'alerts': [
{
'generatedBy': 'opengrep-javascript',
'subType': 'sast-javascript',
'action': 'ignore',
}
]
}
],
}
)
assert len(updated_comments) == 1
assert updated_comments[0][0] == 99
assert '<!-- sast-javascript start -->' in updated_comments[0][1]
assert 'Socket Basics found no active findings in the latest run.' in updated_comments[0][1]
assert '<!-- socket-tier1 start -->' not in updated_comments[0][1]
def test_notify_rewrites_all_clear_even_when_pr_labels_are_disabled(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': False,
}
)
comment_body = """<!-- sast-javascript start -->
## Socket SAST JavaScript
### Summary
🟠 High: 1
<!-- sast-javascript end -->"""
updated_bodies: list[str] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(
notifier,
'_reconcile_pr_labels',
lambda pr_number, labels: (_ for _ in ()).throw(AssertionError('labels are disabled')),
)
monkeypatch.setattr(notifier, '_get_pr_comments', lambda pr_number: [{'id': 99, 'body': comment_body}])
monkeypatch.setattr(
notifier,
'_update_comment',
lambda pr_number, comment_id, body: updated_bodies.append(body) or True,
)
notifier.notify(
{
'notifications': [],
'components': [
{
'alerts': [
{
'generatedBy': 'opengrep-javascript',
'subType': 'sast-javascript',
'action': 'ignore',
}
]
}
],
}
)
assert len(updated_bodies) == 1
assert 'Socket Basics found no active findings in the latest run.' in updated_bodies[0]
assert '<!-- sast-javascript start -->' in updated_bodies[0]
def test_notify_zero_alert_component_metadata_rewrites_matching_section(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': True,
}
)
comment_body = """<!-- sast-javascript start -->
## Socket SAST JavaScript
### Summary
🟠 High: 1
<!-- sast-javascript end -->"""
updated_bodies: list[str] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: True)
monkeypatch.setattr(notifier, '_get_pr_comments', lambda pr_number: [{'id': 99, 'body': comment_body}])
monkeypatch.setattr(
notifier,
'_update_comment',
lambda pr_number, comment_id, body: updated_bodies.append(body) or True,
)
notifier.notify(
{
'notifications': [],
'components': [
{
'id': 'src/index.js',
'subPath': 'sast-javascript',
'alerts': [],
}
],
}
)
assert len(updated_bodies) == 1
assert 'Socket Basics found no active findings in the latest run.' in updated_bodies[0]
assert '<!-- sast-javascript start -->' in updated_bodies[0]
def test_notify_zero_alert_enabled_sast_config_rewrites_matching_section(monkeypatch):
notifier = GithubPRNotifier(
{
'repository': 'SocketDev/socket-basics',
'pr_labels_enabled': True,
}
)
notifier.app_config = {'javascript_sast_enabled': True}
comment_body = """<!-- sast-javascript start -->
## Socket SAST JavaScript
### Summary
🟠 High: 1
<!-- sast-javascript end -->"""
updated_bodies: list[str] = []
monkeypatch.setattr(notifier, '_get_pr_number', lambda: 123)
monkeypatch.setattr(notifier, '_reconcile_pr_labels', lambda pr_number, labels: True)
monkeypatch.setattr(notifier, '_get_pr_comments', lambda pr_number: [{'id': 99, 'body': comment_body}])
monkeypatch.setattr(
notifier,
'_update_comment',
lambda pr_number, comment_id, body: updated_bodies.append(body) or True,
)
notifier.notify({'notifications': [], 'components': []})
assert len(updated_bodies) == 1
assert 'Socket Basics found no active findings in the latest run.' in updated_bodies[0]
assert '<!-- sast-javascript start -->' in updated_bodies[0]