lib/App/Project/Doctor/Check/GitHubActions.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 87.5% 14 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 91.5% 98 / 107 CC 12 SCAR 13.0
CC
12
Cov
89%
check
8.0
_lint_workflow
4.0
__ANON__
2.9
CRAP 3.7
« blib/lib/App/Project/Doctor/Fixer.pm ↑ Summary blib/lib/App/Project/Doctor.pm »
1 package App::Project::Doctor::Check::GitHubActions;
2  
3 # This check validates that GitHub Actions workflow files are present and
4 # syntactically correct. It uses App::Workflow::Lint for the actual YAML
5 # validation. Note: it does NOT own the "missing CI entirely" error --
6 # that belongs to Check::CI. When the workflow directory is absent this
7 # check emits only an informational finding.
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Inherit the standard check interface from Check::Base.
14 2 ▶use parent -norequire, 'App::Project::Doctor::Check::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # croak dies at the caller's location; carp warns there.
17 2 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # File::Spec builds OS-portable paths for the fix closure.
19 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # Readonly makes constants truly immutable at runtime.
21 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our $VERSION = '0.02';
24  
25 # The directory under the repo root where workflow files live.
26 2 Readonly::Scalar my $WORKFLOW_DIR => '.github/workflows';
27  
28 # Short name used in Finding.check_name and the text-report column.
29 1 ▶sub name { 'GitHub Actions' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
30 # One-line description for --help and verbose output.
31 1 ▶sub description { 'Workflow files are present and lint cleanly.' }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
32 # This check can offer a fix (generate a workflow) when no files exist.
33 1 ▶sub can_fix { 1 }
Subroutine can_fix: called CC=1 SCAR=0.0
Pod documented
34 # Run after CI (20) but before Meta (30).
35 1 ▶sub order { 25 }
Subroutine order: called CC=1 SCAR=0.0
Pod documented
36  
37 9 ▶sub check {
Subroutine check: called CC=8 SCAR=20.8
Pod documented
38 9 my ($self, $ctx) = @_;
39 # Guard: require a proper Context object with filesystem helpers.
40 9 ▶ croak 'check requires an App::Project::Doctor::Context' unless ref $ctx;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ctx 0 9 9
41  
42 9 my @findings;
43  
44 # If the workflow directory doesn't exist at all, emit info and stop.
45 # Check::CI already emits the error for a missing CI setup.
46 9 ▶ unless ($ctx->has_file($WORKFLOW_DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->has_file($WORKFLOW_DIR)) 2 7 9
47 2 return _f(
48 severity => 'info',
49 message => 'No .github/workflows/ -- skipping GitHub Actions validation.',
50 );
51 }
52  
53 # The directory exists; find all YAML files inside it.
54 7 my $workflow_files = $ctx->find_files($WORKFLOW_DIR, qr/\.ya?ml$/i);
55  
56 # Directory present but empty of YAML: warn and offer to generate a default workflow.
57 7 ▶ unless (@{$workflow_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$workflow_files;}) 2 5 7
58 2 return _f(
59 severity => 'warning',
60 message => '.github/workflows/ exists but contains no YAML files.',
61 fix => _fix_generate($ctx),
62 );
63 }
64  
65 # Lint each workflow file and collect errors.
66 5 for my $wf (@{$workflow_files}) {
67 7 my @errors = _lint_workflow($ctx->abs_path($wf));
68 7 for my $err (@errors) {
69 # Each lint error becomes a separate Finding with file and optional line.
70 push @findings, _f(
71 severity => 'error',
72 message => "Workflow '$wf': $err->{message}",
73 file => $wf,
74 4 ▶ defined $err->{line} ? (line => $err->{line}) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $err->{'line'} ? : 3 1 4
75 );
76 }
77 }
78  
79 # Only add a pass finding when no errors were collected.
80 5 ▶ unless (@findings) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@findings) 2 3 5
81 push @findings, _f(
82 severity => 'pass',
83 2 message => sprintf('%d workflow file(s) validated OK.', scalar @{$workflow_files}),
84 );
85 }
86  
87 5 return @findings;
88 }
89  
90 # ---------------------------------------------------------------------------
91 # Private helpers
92 # ---------------------------------------------------------------------------
93  
94 # Purpose: Create a Finding with check_name pre-filled to 'GitHub Actions'.
95 # Entry: %args is a valid Finding constructor argument list.
96 # Exit: App::Project::Doctor::Finding object.
97 # Side effects: None.
98 10 ▶sub _f {
Subroutine _f: called CC=1 SCAR=0.0
99 10 require App::Project::Doctor::Finding;
100 10 return App::Project::Doctor::Finding->new(check_name => 'GitHub Actions', @_);
101 }
102  
103 # Purpose: Run App::Workflow::Lint against a single workflow file and
104 # normalise its output into a consistent list of error hashrefs.
105 # Entry: $abs_path is the absolute path to the YAML file being linted.
106 # Exit: List of hashrefs, each with 'message' (string) and optional 'line' (int).
107 # Side effects: Loads App::Workflow::Lint if not already in memory.
108 4 ▶sub _lint_workflow {
Subroutine _lint_workflow: called CC=4 SCAR=13.9
109 4 my $abs_path = shift;
110 4 require App::Workflow::Lint;
111 # App::Workflow::Lint is instantiated fresh per call to avoid any state leakage.
112 4 my $linter = App::Workflow::Lint->new;
113 4 my @raw = $linter->lint($abs_path);
114  
115 # Normalise: linter may return hashrefs OR plain strings.
116 return map {
117 4 ref $_ eq 'HASH'
118 ? {
119 # Use the message key when present; fall back to a generic string.
120 message => $_->{message} // '(unknown lint error)',
121 # Only include 'line' when the linter actually provided one.
122 4 ▶ (defined $_->{line} ? (line => $_->{line}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 3 1 4
defined $_->{'line'} ? : 2 1 3
Condition100%2/2
$_->{"message"} // "(unknown lint error)"
l!l
21
MC/DC100%1/1
$_->{"message"} // "(unknown lint error)"
$_->{"message"}
Truth table100%2/2
A
A= $_->{"message"}
Aresult
11
00
123 }
124 : { message => "$_" } # Plain string errors have no line number.
125 } @raw;
126 }
127  
128 # Purpose: Return a coderef that generates a GitHub Actions workflow file.
129 # Entry: $ctx is the current App::Project::Doctor::Context.
130 # Exit: Coderef ($ctx) -> void; creates .github/workflows/perl-ci.yml.
131 # Side effects: Creates directories and files under $ctx->root when called.
132 2 ▶sub _fix_generate {
Subroutine _fix_generate: called CC=1 SCAR=1.4
133 2 my $ctx = shift;
134 # Return the fix as a closure; it captures $ctx but does not run yet.
135 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=10.8
136 1 my $root = $ctx->root;
137 1 require App::GHGen::Generator;
138 # generate_workflow returns a YAML string or undef on failure.
139 1 my $yaml = App::GHGen::Generator::generate_workflow('perl');
140 1 ▶ return unless $yaml; # Nothing to write if generation failed.
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml 1 0 1
141 # Build the target directory path in a cross-platform way.
142 0 my $wf_dir = File::Spec->catdir($root, '.github', 'workflows');
143 0 require File::Path;
144 # make_path creates .github/ and .github/workflows/ if they don't exist.
145 0 File::Path::make_path($wf_dir);
146 # Write the generated YAML to the standard workflow file name.
147 0 open my $fh, '>', File::Spec->catfile($wf_dir, 'perl-ci.yml');
148 0 print {$fh} $yaml;
149 0 close $fh;
150 2 };
151 }
152  
153 2 1;
154  
155 __END__
« blib/lib/App/Project/Doctor/Fixer.pm ↑ Summary blib/lib/App/Project/Doctor.pm »