Skip to content

Commit

Permalink
using the new utils modules
Browse files Browse the repository at this point in the history
  • Loading branch information
htrgouvea committed Nov 29, 2024
1 parent bbbe48b commit 3a94817
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 125 deletions.
62 changes: 15 additions & 47 deletions lib/Sentra/Engine/DependabotMetrics.pm
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
package Sentra::Engine::DependabotMetrics {
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(decode_json);
use JSON;
use Sentra::Utils::UserAgent;
use Sentra::Utils::Repositories_List;

sub new {
my ($class, $org, $token, $per_page) = @_;

my $userAgent = Mojo::UserAgent -> new();
my $userAgent = Sentra::Utils::UserAgent -> new($token);
my @repositories_list = Sentra::Utils::Repositories_List -> new($org, $token);

my $headers = {
'X-GitHub-Api-Version' => '2022-11-28',
'Accept' => 'application/vnd.github+json',
'User-Agent' => 'Sentra 0.0.3',
'Authorization' => "Bearer $token"
};

my @repos;
my $repo_page = 1;

while (1) {
my $repo_url = "https://api.github.com/orgs/$org/repos?per_page=$per_page&page=$repo_page";
my $repo_tx = $userAgent -> get($repo_url => $headers);
my $res = $repo_tx -> result or return "Error fetching repositories: " . $repo_tx->error->{message} . "\n";

$res->is_success or return "Error fetching repositories: " . $res->message . "\n";

my $repo_data = $res -> json;

last unless @$repo_data;

push @repos, map { "$org/$_->{name}" } grep { !$_->{archived} } @$repo_data;

$repo_page++;
}

return "Error when trying to request information from GitHub, please review the parameters provided." unless @repos;

my $output = "";
my $total_alerts = 0;

my %severity_count = (
Expand All @@ -46,32 +21,25 @@ package Sentra::Engine::DependabotMetrics {
critical => 0
);

for my $repo (@repos) {
foreach my $repository (@repositories_list) {
my $alert_page = 1;

while (1) {
my $alert_url = "https://api.github.com/repos/$repo/dependabot/alerts?state=open&per_page=$per_page&page=$alert_page";
my $alert_tx = $userAgent -> get($alert_url => $headers);
my $res = $alert_tx->result or return "Error fetching alerts for $repo: " . $alert_tx->error->{message} . "\n";
my $alert_url = "https://api.github.com/repos/$repository/dependabot/alerts?state=open&per_page=$per_page&page=$alert_page";
my $request = $userAgent -> get($alert_url);

$res->is_success or return "Error fetching alerts for $repo: " . $res->message . "\n";

my $alert_data = $res -> json;
if ($request -> code() == 200) {
my $alert_data = decode_json($request -> content());

last unless @$alert_data;

$total_alerts += scalar @$alert_data;

for my $alert (@$alert_data) {
my $severity = $alert -> {security_vulnerability}{severity} || 'unknown';

$severity_count{$severity}++ if exists $severity_count{$severity};
}

$alert_page++;
}
}
}

my $output = "";

$output .= "Severity $_: $severity_count{$_}\n" for keys %severity_count;
$output .= "Total DependaBot Alerts: $total_alerts\n";
Expand Down
53 changes: 18 additions & 35 deletions lib/Sentra/Engine/Maintained.pm
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
package Sentra::Engine::Maintained {
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(decode_json);
use JSON;
use DateTime;
use DateTime::Format::ISO8601;
use Sentra::Utils::UserAgent;
use Sentra::Utils::Repositories_List;

sub new {
my ($class, $org, $token, $per_page) = @_;

my $userAgent = Mojo::UserAgent -> new();
my $output = '';
my $userAgent = Sentra::Utils::UserAgent -> new($token);
my @repositories_list = Sentra::Utils::Repositories_List -> new($org, $token);

my $headers = {
'Authorization' => "Bearer $token",
'Accept' => 'application/vnd.github+json',
'X-GitHub-Api-Version' => '2022-11-28'
};
foreach my $repository (@repositories_list) {
my $get_commits = $userAgent -> get("https://api.github.com/repos/$repository/commits");

my $output = '';
my $repo_url = "https://api.github.com/orgs/$org/repos?per_page=$per_page";
my $repo_tx = $userAgent -> get($repo_url => $headers);
my $res = $repo_tx -> result();

if ($res -> is_success) {
my $repos = $res->json;

for my $repo (@$repos) {
next if $repo -> {archived};

my $full_name = "$org/$repo->{name}";
my $commits_url = "https://api.github.com/repos/$full_name/commits";
my $commits_tx = $userAgent -> get($commits_url => $headers);
my $commits_res = $commits_tx -> result;

if ($commits_res && $commits_res->is_success) {
my $commits = $commits_res->json;
if ($get_commits -> code() == 200) {
my $commits = decode_json($get_commits -> content());

if (@$commits) {
my $last_commit_date_str = $commits->[0]{commit}{committer}{date};
my $last_commit_date = DateTime::Format::ISO8601 -> parse_datetime($last_commit_date_str);
if (@$commits) {
my $last_commit_date_str = $commits->[0]{commit}{committer}{date};
my $last_commit_date = DateTime::Format::ISO8601 -> parse_datetime($last_commit_date_str);

if (DateTime -> now -> subtract(days => 90) > $last_commit_date) {
$output .= "The repository https://github.com/$full_name has not been updated for more than 90 days.\n";
}
if (DateTime -> now -> subtract(days => 90) > $last_commit_date) {
$output .= "The repository https://github.com/$repository has not been updated for more than 90 days.\n";
}
}
}
}
}
}

return $output || "No issues found.";
return $output;
}
}

Expand Down
43 changes: 13 additions & 30 deletions lib/Sentra/Engine/SearchFiles.pm
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
package Sentra::Engine::SearchFiles {
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(decode_json);
use JSON;
use Sentra::Utils::UserAgent;
use Sentra::Utils::Repositories_List;

sub new {
my ($class, $org, $token, $per_page) = @_;

my $userAgent = Mojo::UserAgent -> new();
my $output = '';
my $userAgent = Sentra::Utils::UserAgent -> new($token);
my @repositories_list = Sentra::Utils::Repositories_List -> new($org, $token);

my $headers = {
'Authorization' => "Bearer $token",
'Accept' => 'application/vnd.github+json',
'X-GitHub-Api-Version' => '2022-11-28'
};

my $output = '';
my $repo_url = "https://api.github.com/orgs/$org/repos?per_page=$per_page";
my $repo_tx = $userAgent -> get($repo_url => $headers);

my $res = $repo_tx -> result();

if ($res -> is_success) {
my $repos = $res -> json();

for my $repo (@$repos) {
next if $repo -> {archived};

my $full_name = "$org/$repo->{name}";

my $dependabot_url = "https://api.github.com/repos/$full_name/contents/.github/dependabot.yaml";
my $dependabot_tx = $userAgent -> get($dependabot_url => $headers);
foreach my $repository (@repositories_list) {
my $dependabot_url = "https://api.github.com/repos/$repository/contents/.github/dependabot.yaml";
my $request = $userAgent -> get($dependabot_url);

if ($dependabot_tx -> result -> code == 404) {
$output .= "The dependabot.yml file was not found in this repository: https://github.com/$full_name\n";
}
}
if ($request -> code == 404) {
$output .= "The dependabot.yml file was not found in this repository: https://github.com/$repository\n";
}
}

return $output || "No issues found.";
return $output;
}
}

Expand Down
10 changes: 0 additions & 10 deletions lib/Sentra/Utils/Github.pm

This file was deleted.

6 changes: 3 additions & 3 deletions sentra.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ sub main {
);

my %dispatch_table = (
'metrics' => sub { Sentra::Engine::DependabotMetrics->new($org, $token, $per_page) },
'dependency' => sub { Sentra::Engine::SearchFiles->new($org, $token, $per_page) },
'maintained' => sub { Sentra::Engine::Maintained->new($org, $token, $per_page) },
'metrics' => sub { Sentra::Engine::DependabotMetrics -> new($org, $token, $per_page) },
'dependency' => sub { Sentra::Engine::SearchFiles -> new($org, $token, $per_page) },
'maintained' => sub { Sentra::Engine::Maintained -> new($org, $token, $per_page) },
);

for my $option (keys %options) {
Expand Down

0 comments on commit 3a94817

Please sign in to comment.