-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicPRReporter.py
60 lines (46 loc) · 1.92 KB
/
DynamicPRReporter.py
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
from Reporters import PullRequestReporter, GitService
import boto3
import os
import json
class ZAPPRReporter(PullRequestReporter):
def __init__(self, git_service: GitService, pr_id: int):
super(ZAPPRReporter, self).__init__(git_service, pr_id)
self._exit_status = 0
self._bucket_name = os.getenv('S3_BUCKET_NAME')
self._s3_client = boto3.client('s3')
self._s3_bucket = boto3.resource('s3')
@property
def exit_status(self):
return self._exit_status
def _get_report(self):
report_url = self._s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': self._bucket_name, 'Key': 'report.html'},
ExpiresIn=86400)
return report_url
def _get_metrics(self):
obj = self._s3_bucket.Object(self._bucket_name, 'report.json')
json_obj = json.loads(obj.get()['Body'].read().decode('utf-8'))
alerts = {'0': 0, '1': 0, '2': 0, '3': 0}
for site in json_obj['site']:
for alert in site['alerts']:
alerts[alert['riskcode']] += 1
# If high risk vuln > 0
if alerts['3'] > 0:
self._exit_status = 1
return alerts
def _create_message(self):
alerts = self._get_metrics()
msg = "# OWASP ZAP Fuzzing Results\n" \
"| Risk Level | Amount of Vulnerabilities |\n" \
"| -------------- | ------------------------- |\n" \
f"| High | {alerts['3']} |\n" \
f"| Medium | {alerts['2']} |\n" \
f"| Low | {alerts['1']} |\n" \
f"| Informational | {alerts['0']} |\n\n"
report_url = self._get_report()
msg += f"View more results:\n{report_url}"
return msg
def process_pull_review(self):
msg = self._create_message()
self.pull_review.body = msg