Skip to content

Commit

Permalink
integration script fixes
Browse files Browse the repository at this point in the history
- added support for codeowners /**/* notation
- skip section default owners for now
- prevent empty arrays from creating invalid configs
  • Loading branch information
wickedOne committed Feb 16, 2024
1 parent aa37501 commit f1a8f44
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
my %config = (
owner => $owner,
codeowners => './CODEOWNERS',
excludes => @excludes
excludes => \@excludes,
);

my $gitlab = GPH::Gitlab->new(%config);
Expand Down
14 changes: 9 additions & 5 deletions codeowner2phpstan.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
use GPH::Gitlab;
use GPH::PHPStan;

use constant CODEOWNERS_FILE => './CODEOWNERS';

my $owner = $ENV{'DEV_TEAM'} or die "please define owner in DEV_TEAM env var";
my @excludes = split /,/, ($ENV{'EXCLUDE_PATHS'} || '');

my $gitlab = GPH::Gitlab->new((owner => $owner, codeowners => './CODEOWNERS', excludes => @excludes));
my %gitlabConfig = (
owner => $owner,
codeowners => './CODEOWNERS',
excludes => \@excludes,
);

my $gitlab = GPH::Gitlab->new(%gitlabConfig);

my @includes = split /,/, ($ENV{'PHPSTAN_INCLUDES'} || './phpstan.ci.neon');
my @ignored = split /,/, ($ENV{'PHPSTAN_IGNORED_DIRS'} || '');
Expand All @@ -22,9 +26,9 @@

my %config = (
level => $ENV{'PHPSTAN_LEVEL'} || 6,
paths => \$gitlab->getPaths(),
paths => $gitlab->getPaths(),
baseline => $ENV{'PHPSTAN_BASELINE'},
ignoredDirectories => \@ignored,
ignoredDirectories => @ignored,
cacheDir => $ENV{'PHPSTAN_CACHE_DIR'},
includes => \@includes,
threads => $ENV{'PHPSTAN_THREADS'}
Expand Down
18 changes: 11 additions & 7 deletions codeowner2psalm.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
use GPH::Gitlab;
use GPH::Psalm;

use constant PSALM_CONFIG => './psalm.xml';

my $owner = $ENV{'DEV_TEAM'} or die "please define owner in DEV_TEAM env var";
my @excludes = split /,/, ($ENV{'EXCLUDE_PATHS'} || '');

my $gitlab = GPH::Gitlab->new((owner => $owner, codeowners => './CODEOWNERS', excludes => @excludes));
my %gitlabConfig = (
owner => $owner,
codeowners => './CODEOWNERS',
excludes => \@excludes,
);

my $gitlab = GPH::Gitlab->new(%gitlabConfig);

my @ignored = split /,/, ($ENV{'PSALM_IGNORED_DIRS'} || '');
my @plugins = split /,/, ($ENV{'PSALM_PLUGINS'} || '');
my $clone = defined($ENV{'PSALM_CLONE_HANDLERS'}) ? $ENV{'PSALM_CLONE_HANDLERS'} : 1;
my $clone = (defined($ENV{'PSALM_CLONE_HANDLERS'}) ? $ENV{'PSALM_CLONE_HANDLERS'} : 1);

# merge ignored dirs with blacklist
@ignored = (@ignored, $gitlab->getBlacklistPaths());
@ignored = (@ignored, @{$gitlab->getBlacklistPaths()});

my %config = (
level => $ENV{'PSALM_LEVEL'} || 4,
paths => \$gitlab->getPaths(),
paths => $gitlab->getPaths(),
ignoredDirectories => \@ignored,
baseline => $ENV{'PSALM_BASELINE'},
baselineCheck => $ENV{'PSALM_BASELINE_CHECK'},
Expand All @@ -37,7 +41,7 @@
if ($clone eq 1) {
my @blacklist = split /,/, ($ENV{'PSALM_EXCLUDE_HANDLERS'} || '');

print $psalm->getConfigWithIssueHandlers(PSALM_CONFIG, @blacklist);
print $psalm->getConfigWithIssueHandlers('./psalm.xml', @blacklist);
}
else {
print $psalm->getConfig();
Expand Down
4 changes: 2 additions & 2 deletions doc/Infection.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ though configurable in the `stdin2codeowner-filter.pl` file, for now no plans to
> MIN_MSI: '95.00'
> before_script:
> - composer dump-autoload --optimize --ignore-platform-reqs
> - git fetch --depth=1 origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
> - export INFECTION_FILTER=$(git diff origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME --diff-filter=AM --name-only | .stdin2codeowner-filter.pl)
> - git fetch --depth=1 origin $CI_MERGE_REQUEST_DIFF_BASE_SHA
> - export INFECTION_FILTER=$(git diff $CI_MERGE_REQUEST_DIFF_BASE_SHA..$CI_COMMIT_SHA --diff-filter=AMR --name-only -- '***.php' | .stdin2codeowner-filter.pl)
> script:
> - ./vendor/bin/infection -j$(nproc) --filter=$INFECTION_FILTER --min-msi=$MIN_MSI --min-covered-msi=$MIN_COVERED_MSI --coverage=./coverage --skip-initial-tests
> ```
Expand Down
2 changes: 1 addition & 1 deletion infection2escapee-warning.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

my $infection = GPH::Infection->new((msi => $ENV{'MIN_MSI'}, covered => $ENV{'MIN_COVERED_MSI'}));

exit $infection->Parse();
exit $infection->parse();
8 changes: 7 additions & 1 deletion lib/GPH/Gitlab.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sub new {

# build excludes hash for quick lookup
if (exists($args{excludes})) {
foreach my $item ($args{excludes}) {
foreach my $item (@{$args{excludes}}) {
$excludeHash{$item} = 1;
}
}
Expand All @@ -44,9 +44,15 @@ sub new {

for my $line (@lines) {

# skip section line. default codeowners not yet supported
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|/ |;

my ($class_path, $owners) = split(' ', $line, 2);

# skip if path is excluded
Expand Down
5 changes: 4 additions & 1 deletion lib/GPH/PHPStan.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ sub new {

(exists($args{level}) and exists($args{paths})) or die "$!";

# filter out empty arrays
my $excludes = ((exists($args{ignoredDirectories}) and scalar(@{$args{ignoredDirectories}}) != 0) ? $args{ignoredDirectories} : undef);

my $self = {
level => $args{level},
paths => $args{paths},
ignoredDirectories => $args{ignoredDirectories} || undef,
ignoredDirectories => $excludes,
baseline => $args{baseline} || undef,
cacheDir => $args{cacheDir} || 'var',
includes => $args{includes} || undef,
Expand Down
20 changes: 16 additions & 4 deletions lib/GPH/Psalm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ sub new {

(exists($args{level}) and exists($args{paths})) or die "$!";

# prevent empty arrays
my $ignored = ((exists($args{ignoredDirectories}) and scalar(@{$args{ignoredDirectories}}) != 0) ? $args{ignoredDirectories} : undef);
my $plugins = ((exists($args{plugins}) and scalar(@{$args{plugins}}) != 0) ? $args{plugins} : undef);

my $self = {
level => $args{level},
paths => $args{paths},
ignoredDirectories => $args{ignoredDirectories} || undef,
ignoredDirectories => $ignored,
baseline => $args{baseline} || undef,
baselineCheck => $args{baselineCheck} || 'true',
cacheDir => $args{cacheDir} || './psalm',
plugins => $args{plugins} || undef,
plugins => $plugins,
generator => GPH::XMLHelper->new(),
};

Expand All @@ -60,6 +64,7 @@ sub new {
# Returns: psalm.xml config file string
sub getConfig {
my $self = shift;
my $type;

my $psalm = $self->{generator}->buildElement((name => 'psalm', attributes => {
'resolveFromConfigFile' => 'true',
Expand All @@ -76,7 +81,9 @@ sub getConfig {
my $projectFiles = $self->{generator}->buildElement((name => 'projectFiles', parent => $psalm));

foreach my $path (@{$self->{paths}}) {
$self->{generator}->buildElement((name => 'directory', parent => $projectFiles, attributes => {
$type = ($path =~ /.*\.[a-z]+$/) ? 'file' : 'directory';

$self->{generator}->buildElement((name => $type, parent => $projectFiles, attributes => {
'name' => $path,
}));
}
Expand All @@ -85,7 +92,9 @@ sub getConfig {
my $ignoreFiles = $self->{generator}->buildElement((name => 'ignoreFiles', parent => $projectFiles));

foreach my $path (@{$self->{ignoredDirectories}}) {
$self->{generator}->buildElement((name => 'directory', parent => $ignoreFiles, attributes => {
$type = ($path =~ /.*\.[a-z]+$/) ? 'file' : 'directory';

$self->{generator}->buildElement((name => $type, parent => $ignoreFiles, attributes => {
'name' => $path,
}));
}
Expand All @@ -102,6 +111,7 @@ sub getConfig {
}

my $dom = $self->{generator}->getDom();

$dom->setDocumentElement($psalm);

return ($dom->toString(1));
Expand All @@ -121,6 +131,8 @@ sub getConfigWithIssueHandlers {
my ($handlers) = $dom->findnodes('//*[local-name()="issueHandlers"]');

foreach my $exclude ($blacklist) {
next if not defined $exclude;

my ($remove) = $handlers->findnodes("//*[local-name()=\"${exclude}\"]");

if (defined $remove) {
Expand Down
2 changes: 1 addition & 1 deletion stdin2codeowner-filter.pl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
my %config = (
owner => $owner,
codeowners => './CODEOWNERS',
excludes => @excludes
excludes => \@excludes
);

my $gitlab = GPH::Gitlab->new(%config);
Expand Down
8 changes: 4 additions & 4 deletions t/share/Gitlab/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[alpha]
/src/Command @teams/alpha
/src/Service @teams/alpha @#!$%^
/src/Command/**/* @teams/alpha
/src/Service/ @teams/alpha @#!$%^
.gitlab-ci.yml @teams/alpha

[beta]
/src/Command/Config @teams/beta
/src/DependencyInjection @teams/beta
/src/Command/Config/ConfigPhraseKeyCommand.php @teams/beta
/src/DependencyInjection/ @teams/beta
.gitlab-ci.yml @teams/beta
1 change: 1 addition & 0 deletions t/share/Psalm/psalm-issue-handlers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<projectFiles>
<directory name="/src/Command"/>
<directory name="/src/Service"/>
<file name="/src/DependencyInjection/Configuration.php"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
Expand Down
1 change: 1 addition & 0 deletions t/share/Psalm/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<projectFiles>
<directory name="/src/Command"/>
<directory name="/src/Service"/>
<file name="/src/DependencyInjection/Configuration.php"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
Expand Down
9 changes: 4 additions & 5 deletions t/unit/GPH/Composer.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use Test2::V0 -target => 'GPH::Composer';
use Test2::Tools::Spec;

use Data::Dumper;
use Readonly;

local $SIG{__WARN__} = sub {};

Readonly my $CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';
use constant CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';

describe "class `$CLASS`" => sub {
tests 'it can be instantiated' => sub {
Expand All @@ -24,7 +23,7 @@ describe "class `$CLASS`" => sub {

$exception = dies {
$warnings = warns {
$object = $CLASS->new((classmap => $CLASSMAP_FILE));
$object = $CLASS->new((classmap => CLASSMAP_FILE));
};
};

Expand All @@ -41,7 +40,7 @@ describe "class `$CLASS`" => sub {

tests "mandatory config options" => sub {
ok(dies{$CLASS->new(())}, 'died with missing classmap option') or note ($@);
ok(lives{$CLASS->new((classmap => $CLASSMAP_FILE))}, 'lived with mandatory options') or note ($@);
ok(lives{$CLASS->new((classmap => CLASSMAP_FILE))}, 'lived with mandatory options') or note ($@);
};
};

Expand Down Expand Up @@ -74,7 +73,7 @@ describe 'test matching' => sub {

$exception = dies {
$warnings = warns {
$object = $CLASS->new((classmap => $CLASSMAP_FILE));
$object = $CLASS->new((classmap => CLASSMAP_FILE));
$result = $object->match($className, @paths);
};
};
Expand Down
33 changes: 16 additions & 17 deletions t/unit/GPH/Gitlab.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ use Test2::V0 -target => 'GPH::Gitlab';
use Test2::Tools::Spec;

use Data::Dumper;
use Readonly;

Readonly my $CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';
use constant CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';

local $SIG{__WARN__} = sub {};

describe "class `$CLASS`" => sub {
my %config = (
codeowners => $CODEOWNERS_FILE,
codeowners => CODEOWNERS_FILE,
owner =>'@teams/alpha',
excludes => qw{.gitlab-ci.yml}
excludes => ['.gitlab-ci.yml']
);

tests 'it can be instantiated' => sub {
Expand All @@ -31,8 +30,8 @@ describe "class `$CLASS`" => sub {

tests "mandatory config options" => sub {
ok(dies{$CLASS->new((owner =>'@teams/alpha'))}, 'died with missing codeowners option') or note ($@);
ok(dies{$CLASS->new((codeowners => $CODEOWNERS_FILE))}, 'died with missing owner option') or note ($@);
ok(lives{$CLASS->new((owner =>'@teams/alpha', codeowners => $CODEOWNERS_FILE))}, 'lived with mandatory options') or note ($@);
ok(dies{$CLASS->new((codeowners => CODEOWNERS_FILE))}, 'died with missing owner option') or note ($@);
ok(lives{$CLASS->new((owner =>'@teams/alpha', codeowners => CODEOWNERS_FILE))}, 'lived with mandatory options') or note ($@);
};

tests 'owner with blacklist and exclude' => sub {
Expand All @@ -47,7 +46,7 @@ describe "class `$CLASS`" => sub {
is($exception, undef, 'no exception thrown');
is($warnings, 0, 'no warnings generated');

is($object->{blacklist}{'@teams/alpha'}, ['/src/Command/Config'], 'blacklist correct');
is($object->{blacklist}{'@teams/alpha'}, ['/src/Command/Config/ConfigPhraseKeyCommand.php'], 'blacklist correct');
is('.gitlab-ci.yml', not_in_set(@{$object->{codeowners}{'@teams/alpha'}}), 'excluded file not defined');
};

Expand All @@ -65,40 +64,40 @@ describe "class `$CLASS`" => sub {

is($object->getPaths(),
array {
item '/src/Command';
item '/src/Service';
item '/src/Command/';
item '/src/Service/';
end;
},
'GetPaths call correct'
);

is($object->getBlacklistPaths(),
array {
item '/src/Command/Config';
item '/src/Command/Config/ConfigPhraseKeyCommand.php';
end;
},
'GetBlacklistPaths call correct'
);

is($object->getCommaSeparatedPathList(), '/src/Command,/src/Service', 'GetCommaSeparatedPathList call correct');
is($object->getCommaSeparatedPathList(), '/src/Command/,/src/Service/', 'GetCommaSeparatedPathList call correct');

my @arr = qw|/src/Mutator/Unwrap/ /src/Command /src/Command/Config/Processor|;
my @arr = qw|/src/Mutator/Unwrap/ /src/Command/ /src/Command/Config/ConfigPhraseKeyCommand.php|;

is($object->intersectCommaSeparatedPathList(@arr), '/src/Command', 'IntersectToCommaSeparatedPathList call correct');
is($object->intersectCommaSeparatedPathList(@arr), '/src/Command/', 'IntersectToCommaSeparatedPathList call correct');

is([$object->intersect(@arr)],
array {
item '/src/Command';
item '/src/Command/';
end;
},
'Intersect call correct'
);

is($object->match('/src/Service'), 1, 'Match call match correct');
is($object->match('/src/Service/'), 1, 'Match call match correct');
is($object->match('src/Mutator/Unwrap/'), 0, 'Match call no match correct');

is($object->matchBlacklist('/src/Command/Config/Processor'), 1, 'MatchBlacklist call match correct');
is($object->matchBlacklist('/src/Service/SomeService'), 0, 'MatchBlacklist call no match correct');
is($object->matchBlacklist('/src/Command/Config/ConfigPhraseKeyCommand.php'), 1, 'MatchBlacklist call match correct');
is($object->matchBlacklist('/src/Service/SomeService.php'), 0, 'MatchBlacklist call no match correct');
};
};

Expand Down
Loading

0 comments on commit f1a8f44

Please sign in to comment.