« blib/lib/Test/CPAN/Health/Check/Benchmarks.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/CPANTesters.pm »
| 1 | package Test::CPAN::Health::Check::CIConfig; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Carp qw(croak carp); | ||
| 8 | use File::Find::Rule; | ||
| 9 | use File::Spec; | ||
| 10 | use Readonly; | ||
| 11 | use Params::Validate::Strict qw(validate_strict); | ||
| 12 | |||
| 13 | use parent 'Test::CPAN::Health::Check'; | ||
| 14 | |||
| 15 | our $VERSION = '0.1.0'; | ||
| 16 | |||
| 17 | # YAML-based CI config files that live at the distribution root. | ||
| 18 | Readonly::Array my @ROOT_YAML_CONFIGS => qw( | ||
| 19 | .travis.yml | ||
| 20 | .appveyor.yml | ||
| 21 | azure-pipelines.yml | ||
| 22 | ); | ||
| 23 | |||
| 24 | # Non-YAML CI config files (syntax checking not applicable). | ||
| 25 | Readonly::Array my @ROOT_OTHER_CONFIGS => qw( | ||
| 26 | Jenkinsfile | ||
| 27 | ); | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | Test::CPAN::Health::Check::CIConfig - Check that a CI configuration is present and valid | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | use Test::CPAN::Health::Check::CIConfig; | ||
| 36 | |||
| 37 | my $check = Test::CPAN::Health::Check::CIConfig->new; | ||
| 38 | my $result = $check->run($dist); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Checks for the presence of a recognised CI configuration file and, where the | ||
| 43 | format is YAML, validates that the file parses without errors. | ||
| 44 | |||
| 45 | Recognised CI systems: | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item * GitHub Actions -- C<.github/workflows/*.yml> | ||
| 50 | |||
| 51 | =item * Travis CI -- C<.travis.yml> | ||
| 52 | |||
| 53 | =item * AppVeyor -- C<.appveyor.yml> | ||
| 54 | |||
| 55 | =item * Azure Pipelines -- C<azure-pipelines.yml> | ||
| 56 | |||
| 57 | =item * CircleCI -- C<.circleci/config.yml> | ||
| 58 | |||
| 59 | =item * Jenkins -- C<Jenkinsfile> | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | Score matrix: | ||
| 64 | |||
| 65 | =over 4 | ||
| 66 | |||
| 67 | =item * 100 -- at least one valid CI config found. | ||
| 68 | |||
| 69 | =item * 70 -- CI config file(s) found but all YAML files failed to parse. | ||
| 70 | |||
| 71 | =item * 0 -- no CI configuration found. | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub id { return 'ci_config' } | ||
| 78 | sub name { return 'CI Configuration' } | ||
| 79 | sub description { return 'Checks that a valid CI configuration file is present' } | ||
| 80 | sub weight { return 4 } | ||
| 81 | sub category { return 'ci' } | ||
| 82 | |||
| 83 | =head2 run | ||
| 84 | |||
| 85 | =head3 PURPOSE | ||
| 86 | |||
| 87 | Scan the distribution root for known CI configuration files and validate | ||
| 88 | YAML ones for parse errors. | ||
| 89 | |||
| 90 | =head3 API SPECIFICATION | ||
| 91 | |||
| 92 | =head4 INPUT | ||
| 93 | |||
| 94 | dist Test::CPAN::Health::Distribution required | ||
| 95 | context Hashref optional (unused) | ||
| 96 | |||
| 97 | =head4 OUTPUT | ||
| 98 | |||
| 99 | L<Test::CPAN::Health::Result> with: | ||
| 100 | |||
| 101 | check_id 'ci_config' | ||
| 102 | status 'pass' | 'warn' | 'fail' | ||
| 103 | score 100 | 70 | 0 | ||
| 104 | summary human-readable verdict | ||
| 105 | details list of file names found or error descriptions | ||
| 106 | |||
| 107 | =head3 MESSAGES | ||
| 108 | |||
| 109 | Code | Severity | Message | Resolution | ||
| 110 | ------+----------+------------------------------------+--------------------- | ||
| 111 | CI001 | FAIL | No CI configuration found | Add .github/workflows/ or .travis.yml | ||
| 112 | CI002 | WARN | CI config found but YAML invalid | Fix YAML syntax errors | ||
| 113 | CI003 | PASS | CI configuration found | | ||
| 114 | |||
| 115 | =head3 FORMAL SPECIFICATION | ||
| 116 | |||
| 117 | -- Z schema (placeholder) -- | ||
| 118 | CIConfigOp | ||
| 119 | dist : Distribution | ||
| 120 | files : seq String | ||
| 121 | valid : seq String | ||
| 122 | score : {0, 70, 100} | ||
| 123 | ------------------------------------------------------- | ||
| 124 | #files = 0 => score = 0 | ||
| 125 | #valid > 0 => score = 100 | ||
| 126 | #files > 0 /\ #valid = 0 => score = 70 | ||
| 127 | |||
| 128 | =head3 SIDE EFFECTS | ||
| 129 | |||
| 130 | Reads CI configuration files from disk. | ||
| 131 | |||
| 132 | =head3 USAGE EXAMPLE | ||
| 133 | |||
| 134 | my $result = Test::CPAN::Health::Check::CIConfig->new->run($dist); | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub run { | ||
| 139 | my ($self, $dist, $context) = @_; | ||
| 140 | |||
| 141 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 142 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 143 | |||
| 144 | my (@yaml_files, @other_files); | ||
| 145 | |||
| 146 | # GitHub Actions: .github/workflows/*.yml or *.yaml | ||
| 147 | my $workflows_dir = File::Spec->catdir($dist->path, '.github', 'workflows'); | ||
| 148 | if (-d $workflows_dir) { | ||
| 149 | push @yaml_files, | ||
| 150 | File::Find::Rule->file->name('*.yml', '*.yaml')->in($workflows_dir); | ||
| 151 | } | ||
| 152 | |||
| 153 | # CircleCI | ||
| 154 | my $circle = File::Spec->catfile($dist->path, '.circleci', 'config.yml'); | ||
| 155 | push @yaml_files, $circle if -f $circle; | ||
| 156 | |||
| 157 | # Root-level YAML configs | ||
| 158 | for my $name (@ROOT_YAML_CONFIGS) { | ||
| 159 | my $f = File::Spec->catfile($dist->path, $name); | ||
| 160 | push @yaml_files, $f if -f $f; | ||
| 161 | } | ||
| 162 | |||
| 163 | # Non-YAML configs | ||
| 164 | for my $name (@ROOT_OTHER_CONFIGS) { | ||
| 165 | my $f = File::Spec->catfile($dist->path, $name); | ||
| 166 | push @other_files, $f if -f $f; | ||
| 167 | } | ||
| 168 | |||
| 169 | unless (@yaml_files || @other_files) { | ||
| 170 | return $self->_result( | ||
| 171 | status => 'fail', | ||
| 172 | score => 0, | ||
| 173 | summary => 'No CI configuration file found', | ||
| 174 | details => [ | ||
| 175 | 'Add .github/workflows/*.yml for GitHub Actions,', | ||
| 176 | 'or .travis.yml, .appveyor.yml, .circleci/config.yml, etc.', | ||
| 177 | ], | ||
| 178 | data => { name => $self->name }, | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | # Validate YAML files; non-YAML files (Jenkinsfile) are always accepted. | ||
| 183 | my (@valid_labels, @invalid_labels); | ||
| 184 | |||
| 185 | for my $f (@yaml_files) { | ||
| 186 | my $label = File::Spec->abs2rel($f, $dist->path); | ||
| 187 | my $ok = _yaml_valid($f); | ||
| 188 | if ($ok) { | ||
| 189 | push @valid_labels, $label; | ||
| 190 | } else { | ||
| 191 | push @invalid_labels, $label; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | for my $f (@other_files) { | ||
| 196 | push @valid_labels, File::Spec->abs2rel($f, $dist->path); | ||
| 197 | } | ||
| 198 | |||
| 199 | if (@valid_labels) { | ||
| 200 | return $self->_result( | ||
| 201 | status => 'pass', | ||
| 202 | score => 100, | ||
| 203 | summary => sprintf('CI configuration found: %s', join(', ', @valid_labels)), | ||
| 204 | data => { name => $self->name, files => \@valid_labels }, | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | |||
| 208 | # Files found but all YAML failed to parse. | ||
| 209 | return $self->_result( | ||
| 210 | status => 'warn', | ||
| 211 | score => 70, | ||
| 212 | summary => sprintf( | ||
| 213 | '%d CI config file(s) found but contain invalid YAML', | ||
| 214 | scalar @invalid_labels, | ||
| 215 | ), | ||
| 216 | details => [ map { "Invalid YAML: $_" } @invalid_labels ], | ||
| 217 | data => { name => $self->name, files => \@invalid_labels }, | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | |||
| 221 | # --------------------------------------------------------------------------- | ||
| 222 | # Private helpers | ||
| 223 | # --------------------------------------------------------------------------- | ||
| 224 | |||
| 225 | sub _yaml_valid { | ||
| 226 | my ($file) = @_; | ||
| 227 | |||
| 228 | # CPAN::Meta::YAML is a transitive dep via CPAN::Meta (in core since 5.14). | ||
| 229 | # If unavailable, give benefit of the doubt and call the file valid. | ||
| 230 | my $loaded = eval { require CPAN::Meta::YAML; 1 }; | ||
| 231 | return 1 unless $loaded; | ||
| 232 | |||
| 233 | my $ok = eval { CPAN::Meta::YAML->read($file); 1 }; | ||
| 234 | return $ok ? 1 : 0; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head1 AUTHOR | ||
| 238 | |||
| 239 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 240 | |||
| 241 | =head1 LICENSE AND COPYRIGHT | ||
| 242 | |||
| 243 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 244 | |||
| 245 | This program is free software; you can redistribute it and/or modify it | ||
| 246 | under the terms of the GNU General Public License as published by the Free | ||
| 247 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 248 | any later version. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; | ||