Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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