Skip to content

Commit

Permalink
fix(code): correct severity counting
Browse files Browse the repository at this point in the history
- fix incorrect severity counts
- removed spacing around arrows
  • Loading branch information
scriptprivate authored Sep 10, 2024
1 parent ea8dd96 commit 59ac038
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions lib/SecurityGate/Engine/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@ package SecurityGate::Engine::Code {
use Mojo::JSON;

sub new {
my ($class, $token, $repository, $severity_limits) = @_;
my $alerts_endpoint = "https://api.github.com/repos/$repository/code-scanning/alerts";

my $userAgent = Mojo::UserAgent -> new();
my $alerts_request = $userAgent -> get($alerts_endpoint, {Authorization => "Bearer $token"}) -> result();

if ($alerts_request -> code() == 200) {
my $alerts_data = $alerts_request -> json();
my $open_alerts = 0;
my %severity_counts = map {$_ => 0} keys %$severity_limits;

foreach my $alert (@$alerts_data) {
if ($alert -> {state} eq "open") {
$open_alerts++;

my $severity = $alert -> {rule} -> {severity};
$severity_counts{$severity}++ if exists $severity_counts{$severity};
}
}
my ($class, $token, $repository, $severity_limits) = @_;
my $alerts_endpoint = "https://api.github.com/repos/$repository/code-scanning/alerts";

print "[!] Total of open code scanning alerts: $open_alerts\n";

foreach my $severity (keys %severity_counts) {
print "[-] $severity: $severity_counts{$severity}\n";
}
my $userAgent = Mojo::UserAgent->new();
my $alerts_request = $userAgent->get($alerts_endpoint, {Authorization => "Bearer $token"})->result();

my $threshold_exceeded = 0;

foreach my $severity (keys %severity_counts) {
if ($severity_counts{$severity} > $severity_limits -> {$severity}) {
print "[+] More than $severity_limits->{$severity} $severity code scanning alerts found.\n";

$threshold_exceeded = 1;
}
}
if ($alerts_request->code() == 200) {
my $alerts_data = $alerts_request->json();
my $open_alerts = 0;
my %severity_counts = map {$_ => 0} keys %$severity_limits;

foreach my $alert (@$alerts_data) {
if ($alert->{state} eq "open") {
$open_alerts++;

my $severity = $alert->{rule}->{security_severity_level} // 'unknown';
$severity_counts{$severity}++ if exists $severity_counts{$severity};
}
}

print "\n[!] Total of open code scanning alerts: $open_alerts\n\n";

if ($threshold_exceeded) {
return 1;
foreach my $severity (keys %severity_counts) {
print "[-] $severity: $severity_counts{$severity}\n";
}

print "\n";

my $threshold_exceeded = 0;

foreach my $severity (keys %severity_counts) {
if ($severity_counts{$severity} > $severity_limits->{$severity}) {
print "[+] More than $severity_limits->{$severity} $severity code scanning alerts found.\n";
$threshold_exceeded = 1;
}
}

if ($threshold_exceeded) {
return 1;
}
}
}

else {
print "Error: Unable to fetch code scanning alerts. HTTP status code: " . $alerts_request -> code() . "\n";

return 1;
}
else {
print "Error: Unable to fetch code scanning alerts. HTTP status code: " . $alerts_request->code() . "\n";
return 1;
}

return 0;
return 0;
}
}

1;
1;

0 comments on commit 59ac038

Please sign in to comment.