| 1 | package App::Workflow::Lint::Engine; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak carp); | ||
| 6 | |||
| 7 | use YAML::XS qw(DumpFile); | ||
| 8 | use App::Workflow::Lint::YAML; | ||
| 9 | use App::Workflow::Lint::Rule::MissingPermissions; | ||
| 10 | use App::Workflow::Lint::Rule::MissingTimeout; | ||
| 11 | use App::Workflow::Lint::Rule::UnpinnedActions; | ||
| 12 | use App::Workflow::Lint::Rule::MissingConcurrency; | ||
| 13 | use App::Workflow::Lint::Rule::DeprecatedSetEnv; | ||
| 14 | use App::Workflow::Lint::Rule::MissingRunsOn; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | App::Workflow::Lint::Engine - Workflow loading, rule execution, and fixing | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use App::Workflow::Lint::Engine; | ||
| 23 | |||
| 24 | my $engine = App::Workflow::Lint::Engine->new; | ||
| 25 | |||
| 26 | my $wf = $engine->load_workflow('workflow.yml'); | ||
| 27 | my @diags = $engine->check_file('workflow.yml'); | ||
| 28 | |||
| 29 | my ($fixed, $diags) = $engine->fix_file('workflow.yml'); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | C<App::Workflow::Lint::Engine> is responsible for loading workflow files, | ||
| 34 | running linting rules, and applying automatic fixes where available. | ||
| 35 | |||
| 36 | It is used internally by C<App::Workflow::Lint> and the | ||
| 37 | C<workflow-lint> CLI. | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head2 new | ||
| 42 | |||
| 43 | my $engine = App::Workflow::Lint::Engine->new(%opts); | ||
| 44 | |||
| 45 | Creates a new engine instance. | ||
| 46 | |||
| 47 | =head2 load_workflow | ||
| 48 | |||
| 49 | my $wf = $engine->load_workflow($file); | ||
| 50 | |||
| 51 | Reads the YAML workflow file and returns the parsed data structure. | ||
| 52 | Position information is stored internally but currently unused. | ||
| 53 | |||
| 54 | =head2 save_workflow | ||
| 55 | |||
| 56 | $engine->save_workflow($file, $wf); | ||
| 57 | |||
| 58 | Writes the modified workflow back to disk. | ||
| 59 | |||
| 60 | =head2 rules | ||
| 61 | |||
| 62 | my @rules = $engine->rules; | ||
| 63 | |||
| 64 | Returns the list of linting rule objects applied to workflows. | ||
| 65 | |||
| 66 | =head2 check_file | ||
| 67 | |||
| 68 | my @diagnostics = $engine->check_file($file); | ||
| 69 | |||
| 70 | Runs all linting rules against the workflow and returns diagnostics. | ||
| 71 | |||
| 72 | =head2 apply_fixes | ||
| 73 | |||
| 74 | $engine->apply_fixes($workflow, @diagnostics); | ||
| 75 | |||
| 76 | Executes fix callbacks for any diagnostics that provide them. | ||
| 77 | |||
| 78 | =head2 fix_file | ||
| 79 | |||
| 80 | my ($workflow, $diagnostics) = $engine->fix_file($file); | ||
| 81 | |||
| 82 | Loads the workflow, applies rules, executes fixes, and returns the | ||
| 83 | modified workflow and diagnostics. | ||
| 84 | |||
| 85 | =head2 line_for_path | ||
| 86 | |||
| 87 | my $line = $engine->line_for_path($file, $path); | ||
| 88 | |||
| 89 | Returns the stored line number for a given YAML path. Currently always | ||
| 90 | undef, retained for API compatibility. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | #---------------------------------------------------------------------- | ||
| 95 | |||
| 96 | sub new { | ||
| 97 | my ($class, %opts) = @_; | ||
| 98 | return bless { %opts }, $class; | ||
| 99 | } | ||
| 100 | |||
| 101 | #---------------------------------------------------------------------- | ||
| 102 | |||
| 103 | sub load_workflow { | ||
| 104 | my ($self, $file) = @_; | ||
| 105 | |||
| 106 | # Read the YAML file | ||
| 107 | open my $fh, '<', $file | ||
| 108 | or croak "Cannot open workflow file '$file': $!"; | ||
| 109 | local $/; | ||
| 110 | my $yaml_text = <$fh>; | ||
| 111 | close $fh; | ||
| 112 | |||
| 113 | # Load YAML (returns $data, $positions) | ||
| 114 | my ($wf, $pos) = App::Workflow::Lint::YAML->load_yaml($yaml_text); | ||
| 115 | |||
| 116 | # Store positions (empty hashref now) | ||
| 117 | $self->{_positions}{$file} = $pos; | ||
| 118 | |||
| 119 | return $wf; | ||
| 120 | } | ||
| 121 | |||
| 122 | #---------------------------------------------------------------------- | ||
| 123 | |||
| 124 | sub save_workflow { | ||
| 125 | my ($self, $file, $wf) = @_; | ||
| 126 | |||
| 127 | DumpFile($file, $wf); | ||
| 128 | |||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | #---------------------------------------------------------------------- | ||
| 133 | |||
| 134 | sub rules { | ||
| 135 | return ( | ||
| 136 | App::Workflow::Lint::Rule::MissingPermissions->new, | ||
| 137 | App::Workflow::Lint::Rule::MissingTimeout->new, | ||
| 138 | App::Workflow::Lint::Rule::UnpinnedActions->new, | ||
| 139 | App::Workflow::Lint::Rule::MissingConcurrency->new, | ||
| 140 | App::Workflow::Lint::Rule::DeprecatedSetEnv->new, | ||
| 141 | App::Workflow::Lint::Rule::MissingRunsOn->new, | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | #---------------------------------------------------------------------- | ||
| 146 | |||
| 147 | sub check_file { | ||
| 148 | my ($self, $file) = @_; | ||
| 149 | |||
| 150 | my $wf = $self->load_workflow($file); | ||
| 151 | my @results; | ||
| 152 | |||
| 153 | for my $rule ($self->rules) { | ||
| 154 | my @r = $rule->check($wf, { file => $file }); | ||
| 155 | push @results, @r if @r; | ||
| 156 | } | ||
| 157 | |||
| 158 | return @results; | ||
| 159 | } | ||
| 160 | |||
| 161 | #---------------------------------------------------------------------- | ||
| 162 | # apply_fixes($workflow, @diagnostics) | ||
| 163 | # | ||
| 164 | # Applies all fix coderefs returned by rules. | ||
| 165 | #---------------------------------------------------------------------- | ||
| 166 | |||
| 167 | sub apply_fixes { | ||
| 168 | my ($self, $wf, @diags) = @_; | ||
| 169 | |||
| 170 | for my $d (@diags) { | ||
| 171 | next unless $d->{fix}; | ||
| 172 | $d->{fix}->($wf); # Execute the fix | ||
| 173 | } | ||
| 174 | |||
| 175 | return $wf; | ||
| 176 | } | ||
| 177 | |||
| 178 | #---------------------------------------------------------------------- | ||
| 179 | # fix_file($file) | ||
| 180 | # | ||
| 181 | # Loads workflow, runs rules, applies fixes, returns modified workflow. | ||
| 182 | #---------------------------------------------------------------------- | ||
| 183 | |||
| 184 | sub fix_file { | ||
| 185 | my ($self, $file) = @_; | ||
| 186 | |||
| 187 | my $wf = $self->load_workflow($file); | ||
| 188 | my @diags = $self->check_file($file); | ||
| 189 | |||
| 190 | $self->apply_fixes($wf, @diags); | ||
| 191 | |||
| 192 | return ($wf, \@diags); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub line_for_path { | ||
| 196 | my ($self, $file, $path) = @_; | ||
| 197 | return $self->{_positions}{$file}{$path}; | ||
| 198 | } | ||
| 199 | |||
| 200 | 1; |