| 1 | package App::Workflow::Lint::Formatter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak carp); | ||
| 6 | use JSON::PP; | ||
| 7 | |||
| 8 | #---------------------------------------------------------------------- | ||
| 9 | # format($format, \@diagnostics) | ||
| 10 | # | ||
| 11 | # Supported formats: | ||
| 12 | # human - plain text | ||
| 13 | # json - JSON array of diagnostics | ||
| 14 | # sarif - SARIF 2.1.0 output | ||
| 15 | #---------------------------------------------------------------------- | ||
| 16 | |||
| 17 | sub format { | ||
| 18 | my ($class, $format, $diags) = @_; | ||
| 19 | |||
| 20 | croak "format() requires diagnostics arrayref" | ||
| 21 | unless ref $diags eq 'ARRAY'; | ||
| 22 | |||
| 23 | if ($format eq 'human') { | ||
| 24 | return _format_human($diags); | ||
| 25 | } | ||
| 26 | elsif ($format eq 'json') { | ||
| 27 | return _format_json($diags); | ||
| 28 | } | ||
| 29 | elsif ($format eq 'sarif') { | ||
| 30 | return _format_sarif($diags); | ||
| 31 | } | ||
| 32 | |||
| 33 | croak "Unknown output format '$format'"; | ||
| 34 | } | ||
| 35 | |||
| 36 | #---------------------------------------------------------------------- | ||
| 37 | # Human-readable output | ||
| 38 | #---------------------------------------------------------------------- | ||
| 39 | |||
| 40 | sub _format_human { | ||
| 41 | my ($diags) = @_; | ||
| 42 | |||
| 43 | my @out; | ||
| 44 | for my $d (@$diags) { | ||
| 45 | push @out, sprintf( | ||
| 46 | "%s: %s (%s) at %s", | ||
| 47 | $d->{level}, | ||
| 48 | $d->{message}, | ||
| 49 | $d->{rule}, | ||
| 50 | $d->{path}, | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | return join("\n", @out) . "\n"; | ||
| 55 | } | ||
| 56 | |||
| 57 | #---------------------------------------------------------------------- | ||
| 58 | # JSON output | ||
| 59 | #---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | sub _format_json { | ||
| 62 | my ($diags) = @_; | ||
| 63 | return JSON::PP->new->pretty->encode($diags); | ||
| 64 | } | ||
| 65 | |||
| 66 | #---------------------------------------------------------------------- | ||
| 67 | # SARIF output (Static Analysis Results Interchange Format) | ||
| 68 | #---------------------------------------------------------------------- | ||
| 69 | |||
| 70 | sub _format_sarif { | ||
| 71 | my ($diags) = @_; | ||
| 72 | |||
| 73 | my @results; | ||
| 74 | |||
| 75 | for my $d (@$diags) { | ||
| 76 | push @results, { | ||
| 77 | ruleId => $d->{rule}, | ||
| 78 | level => _sarif_level($d->{level}), | ||
| 79 | message => { text => $d->{message} }, | ||
| 80 | locations => [ | ||
| 81 | { | ||
| 82 | physicalLocation => { | ||
| 83 | artifactLocation => { | ||
| 84 | uri => $d->{file} // 'workflow.yml', | ||
| 85 | }, | ||
| 86 | region => { | ||
| 87 | startLine => 1, # We don't have line numbers yet | ||
| 88 | }, | ||
| 89 | }, | ||
| 90 | } | ||
| 91 | ], | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $sarif = { | ||
| 96 | version => "2.1.0", | ||
| 97 | '$schema' => "https://json.schemastore.org/sarif-2.1.0.json", | ||
| 98 | runs => [ | ||
| 99 | { | ||
| 100 | tool => { | ||
| 101 | driver => { | ||
| 102 | name => "App::Workflow::Lint", | ||
| 103 | informationUri => "https://metacpan.org/pod/App::Workflow::Lint", | ||
| 104 | rules => [], | ||
| 105 | }, | ||
| 106 | }, | ||
| 107 | results => \@results, | ||
| 108 | } | ||
| 109 | ], | ||
| 110 | }; | ||
| 111 | |||
| 112 | return JSON::PP->new->pretty->encode($sarif); | ||
| 113 | } | ||
| 114 | |||
| 115 | #---------------------------------------------------------------------- | ||
| 116 | # Map our levels to SARIF levels | ||
| 117 | #---------------------------------------------------------------------- | ||
| 118 | |||
| 119 | sub _sarif_level { | ||
| 120 | my ($lvl) = @_; | ||
| 121 | |||
| 122 | return 'error' if $lvl eq 'error'; | ||
| 123 | return 'warning' if $lvl eq 'warning'; | ||
| 124 | return 'note'; # info, etc. | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 |