Skip to content

Commit

Permalink
parse codeowners file
Browse files Browse the repository at this point in the history
- moved the parsing of the codeowners file to its own sub
  • Loading branch information
wickedOne committed Feb 17, 2024
1 parent ba014d6 commit 7ef6938
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ WriteMakefile(
"File::Temp" => 0,
},
'test' => {
TESTS => 't/unit/GPH/*.t t/unit/GPH/*/*.t'
TESTS => 't/unit/GPH/*.t t/unit/GPH/*/*.t t/functional/*.t'
}
);

59 changes: 36 additions & 23 deletions lib/GPH/Gitlab.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,48 @@ use warnings FATAL => 'all';
# Returns: reference to Gitlab object
sub new {
my ($class, %args) = @_;
my (%codeowners, %excludeHash, %blacklist);

(exists($args{owner}) and exists($args{codeowners})) or die "$!";

my $self = {
owner => $args{owner},
file => $args{codeowners},
codeowners => undef,
blacklist => undef,
};

bless $self, $class;

return $self->parseCodeowners(%args);
}

#------------------------------------------------------------------------------
# Parse codeowners file
#
# Inputs: codeowners => (string) path to code owners file
# excludes => (array) paths to exclude
#
# Returns: reference to Gitlab object
sub parseCodeowners {
my ($self, %args) = @_;
my ($fh, %codeowners, %excludeHash, %blacklist);

open $fh, '<', $args{codeowners} or die "unable to open codeowners file: $!";
my @lines = <$fh>;
close($fh);

# build excludes hash for quick lookup
if (exists($args{excludes})) {
foreach my $item (@{$args{excludes}}) {
$excludeHash{$item} = 1;
}
}

open(my $fh, '<', $args{codeowners}) or die "unable to open codeowners file, initialization failed $!";

my @lines = <$fh>;

close($fh);

for my $line (@lines) {

# skip section line. default codeowners not yet supported
next if $line =~ /[\[\]]/;
next if $line =~ /[\[\]]/;
# skip if line does not contain @
next unless $line =~ /^.*\s\@[\w]+\/.*$/x;

# replace /**/* with a trailing forward slash
my $pat = quotemeta('/**/* ');
$line =~ s|$pat|/ |;
Expand All @@ -67,25 +85,20 @@ sub new {
}

push(@{$codeowners{$1}}, $class_path);
}

# check whether less specific path is already defined and add it to the blacklist
foreach my $key (keys %codeowners) {
foreach my $defined (@{$codeowners{$key}}) {
if ($class_path =~ $defined and $class_path ne $defined) {
push(@{$blacklist{$key}}, $class_path);
# check whether less specific path is already defined and add it to the blacklist
foreach my $key (keys %codeowners) {
foreach my $defined (@{$codeowners{$key}}) {
if ($class_path =~ $defined and $class_path ne $defined) {
push(@{$blacklist{$key}}, $class_path);
}
}
}
}
}

my $self = {
owner => $args{owner},
codeowners => \%codeowners,
blacklist => \%blacklist,
};

bless $self, $class;
$self->{codeowners} = \%codeowners;
$self->{blacklist} = \%blacklist;

return $self;
}
Expand Down

0 comments on commit 7ef6938

Please sign in to comment.