File Coverage

File:lib/CheckSpelling/Sarif.pm
Coverage:86.7%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::Sarif;
4
5our $VERSION='0.1.0';
6our $flatten=0;
7
8
1
1
1
112628
1
3
use utf8;
9
10
1
1
1
16
0
29
use File::Basename;
11
1
1
1
4
1
14
use File::Spec;
12
1
1
1
489
1345
77
use Digest::SHA qw($errmsg);
13
1
1
1
2
1
31
use JSON::PP;
14
1
1
1
190
3846
26
use Hash::Merge qw( merge );
15
1
1
1
164
1
18
use CheckSpelling::Util;
16
1
1
1
163
1
1917
use CheckSpelling::GitSources;
17
18sub encode_low_ascii {
19
10
286
    $_ = shift;
20
10
1
14
3
    s/([\x{0}-\x{9}\x{0b}\x{1f}#%])/"\\u".sprintf("%04x",ord($1))/eg;
21
10
8
    return $_;
22}
23
24sub url_encode {
25
11
16
    $_ = shift;
26
11
0
13
0
    s<([^-!\$&'()*+,/:;=?\@A-Za-z0-9_.~])><"%".sprintf("%02x",ord($1))>eg;
27
11
12
    return $_;
28}
29
30sub double_slash_escape {
31
9
4
    $_ = shift;
32
9
55
    s/(["()\[\]\\])/\\\\$1/g;
33
9
8
    return $_;
34}
35
36sub fingerprintLocations {
37
7
8
    my ($locations, $encoded_files_ref, $line_hashes_ref, $hashes_needed_for_files_ref, $message, $hashed_message) = @_;
38
7
6
    my @locations_json = ();
39
7
6
    my @fingerprints = ();
40
7
7
    for my $location (@$locations) {
41
10
8
        my $encoded_file = $location->{uri};
42
10
11
        my $line = $location->{startLine};
43
10
4
        my $column = $location->{startColumn};
44
10
9
        my $endColumn = $location->{endColumn};
45
10
6
        my $partialFingerprint = '';
46
10
7
        my $file = $encoded_files_ref->{$encoded_file};
47
10
5
        if (defined $line_hashes_ref->{$file}) {
48
8
12
            my $line_hash = $line_hashes_ref->{$file}{$line};
49
8
6
            if (defined $line_hash) {
50
4
4
2
7
                my @instances = sort keys %{$hashes_needed_for_files_ref->{$file}{$line}{$hashed_message}};
51
4
3
                my $hit = scalar @instances;
52
4
2
                while (--$hit > 0) {
53
0
0
                    last if $instances[$hit] == $column;
54                }
55
4
8
                $partialFingerprint = Digest::SHA::sha1_base64("$line_hash:$message:$hit");
56            }
57        }
58
10
11
        push @fingerprints, $partialFingerprint;
59
10
11
        my $startColumn = $column ? qq<, "startColumn": $column> : '';
60
10
8
        $endColumn = $endColumn ? qq<, "endColumn": $endColumn> : '';
61
10
7
        $line = 1 unless $line;
62
10
11
        my $json_fragment = qq<{ "physicalLocation": { "artifactLocation": { "uri": "$encoded_file", "uriBaseId": "%SRCROOT%" }, "region": { "startLine": $line$startColumn$endColumn } } }>;
63
10
8
        push @locations_json, $json_fragment;
64    }
65
7
14
    return { locations_json => \@locations_json, fingerprints => \@fingerprints };
66}
67
68sub hashFiles {
69
2
5
    my ($hashes_needed_for_files_ref, $line_hashes_ref, $directoryToRepo_ref, $used_hashes_ref) = @_;
70
2
12
    for my $file (sort keys %$hashes_needed_for_files_ref) {
71
4
5
        $line_hashes_ref->{$file} = ();
72
4
60
        unless (-e $file) {
73
2
4
            delete $hashes_needed_for_files_ref->{$file};
74
2
2
            next;
75        }
76
2
2
3
10
        my @lines = sort (keys %{$hashes_needed_for_files_ref->{$file}});
77
2
82
        unless (defined $directoryToRepo_ref->{dirname($file)}) {
78
2
13
            my ($parsed_file, $git_base_dir, $prefix, $remote_url, $rev, $branch) = CheckSpelling::GitSources::git_source_and_rev($file);
79        }
80
2
36
        open $file_fh, '<', $file;
81
2
2
        my $line = shift @lines;
82
2
4
        $line = 2 if $line == 1;
83
2
3
        my $buffer = '';
84
2
20
        while (<$file_fh>) {
85
10
17
            if ($line == $.) {
86
5
4
                my $sample = substr $buffer, -100, 100;
87
5
8
                my $hash = Digest::SHA::sha1_base64($sample);
88
5
5
                for (; $line == $.; $line = shift @lines) {
89
6
9
                    my $hit = $used_hashes_ref->{$hash}++;
90
6
6
                    $hash = "$hash:$hit" if $hit;
91
6
6
                    $line_hashes_ref->{$file}{$line} = $hash;
92
6
8
                    last unless @lines;
93                }
94            }
95
10
7
            $buffer .= $_;
96
10
25
            $buffer =~ s/\s+/ /g;
97
10
14
            $buffer = substr $buffer, -100, 100;
98        }
99
2
18
        close $file_fh;
100    }
101}
102
103sub addToHashesNeededForFiles {
104
10
10
    my ($file, $line, $column, $message, $hashes_needed_for_files_ref) = @_;
105
10
32
    my $hashed_message = Digest::SHA::sha1_base64($message);
106
10
13
    $hashes_needed_for_files_ref->{$file} = () unless defined $hashes_needed_for_files_ref->{$file};
107
10
48
    $hashes_needed_for_files_ref->{$file}{$line} = () unless defined $hashes_needed_for_files_ref->{$file}{$line};
108
10
20
    $hashes_needed_for_files_ref->{$file}{$line}{$hashed_message} = () unless defined $hashes_needed_for_files_ref->{$file}{$line}{$hashed_message};
109
10
23
    $hashes_needed_for_files_ref->{$file}{$line}{$hashed_message}{$column} = '1';
110}
111
112sub parse_warnings {
113
1
1
    my ($warnings) = @_;
114
1
1
    our $flatten;
115
1
0
    our %directoryToRepo;
116
1
1
    our $provenanceInsertion;
117
1
0
    our %provenanceStringToIndex;
118
1
1
    our %directoryToProvenanceInsertion;
119
1
1
    my @results;
120
1
27
    unless (open WARNINGS, '<', $warnings) {
121
0
0
        print STDERR "Could not open $warnings\n";
122
0
0
        return [];
123    }
124
1
1
    my $rules = ();
125
1
1
    my %encoded_files = ();
126
1
3
    my %hashes_needed_for_files = ();
127
1
20
    while (<WARNINGS>) {
128
11
13
        next if m{^https://};
129
10
42
        next unless m{^(.+):(\d+):(\d+) \.\.\. (\d+),\s(Error|Warning|Notice)\s-\s(.+\s\((.+)\))$};
130
9
42
        my ($file, $line, $column, $endColumn, $severity, $message, $code) = ($1, $2, $3, $4, $5, $6, $7);
131
9
118
        my $directory = dirname($file);
132
9
7
        unless (defined $directoryToProvenanceInsertion{$directory}) {
133
2
5
            my $provenanceString = collectVersionControlProvenance($file);
134
2
173
            if ($provenanceString) {
135
2
5
                if (defined $provenanceStringToIndex{$provenanceString}) {
136
0
0
                    $directoryToProvenanceInsertion{$directory} = $provenanceStringToIndex{$provenanceString};
137                } else {
138
2
5
                    $provenanceStringToIndex{$provenanceString} = $provenanceInsertion;
139
2
5
                    $directoryToProvenanceInsertion{$directory} = $provenanceInsertion;
140
2
2
                    ++$provenanceInsertion;
141                }
142            }
143        }
144        # single-slash-escape `"` and `\`
145
9
15
        $message =~ s/(["\\])/\\$1/g;
146
9
9
        $message = encode_low_ascii $message;
147        # double-slash-escape `"`, `(`, `)`, `]`
148
9
9
        $message = double_slash_escape $message;
149        # encode `message` and `file` to protect against low ascii`
150
9
9
        my $encoded_file = url_encode $file;
151
9
9
        $encoded_files{$encoded_file} = $file;
152        # hack to make the first `...` identifier a link (that goes nowhere, but is probably blue and underlined) in GitHub's SARIF view
153
9
9
        if ($message =~ /(`{2,})/) {
154
1
1
            my $backticks = $1;
155
1
35
            while ($message =~ /($backticks`+)(?=[`].*?\g{-1})/gs) {
156
0
0
                $backticks = $1 if length($1) > length($backticks);
157            }
158
1
17
            $message =~ s/(^|[^\\])$backticks(.+?)$backticks/${1}[${2}](#security-tab)/;
159        } else {
160
8
27
            $message =~ s/(^|[^\\])\`((?:[^`\\]|\\(?!`))+)\`/${1}[${2}](#security-tab)/;
161        }
162        # replace '`' with `\`+`'` because GitHub's SARIF parser doesn't like them
163
9
16
        $message =~ s/\`/'/g;
164
9
13
        unless (defined $rules->{$code}) {
165
3
8
            $rules->{$code} = {};
166        }
167
9
4
        my $rule = $rules->{$code};
168
9
7
        unless (defined $rule->{$message}) {
169
6
14
            $rule->{$message} = [];
170        }
171
9
12
        addToHashesNeededForFiles($file, $line, $column, $message, \%hashes_needed_for_files);
172
9
7
        my $locations = $rule->{$message};
173
9
20
        my $physicalLocation = {
174            'uri' => $encoded_file,
175            'startLine' => $line,
176            'startColumn' => $column,
177            'endColumn' => $endColumn,
178        };
179
9
5
        push @$locations, $physicalLocation;
180
9
33
        $rule->{$message} = $locations;
181    }
182
1
2
    my %line_hashes = ();
183
1
1
    my %used_hashes = ();
184
1
4
    hashFiles(\%hashes_needed_for_files, \%line_hashes, \%directoryToRepo, \%used_hashes);
185
1
1
1
4
    for my $code (sort keys %{$rules}) {
186
3
18
        my $rule = $rules->{$code};
187
3
3
3
6
        for my $message (sort keys %{$rule}) {
188
6
9
            my $hashed_message = Digest::SHA::sha1_base64($message);
189
6
5
            my $locations = $rule->{$message};
190
6
6
            my $fingerprintResults = fingerprintLocations($locations, \%encoded_files, \%line_hashes, \%hashes_needed_for_files, $message, $hashed_message);
191
6
6
4
7
            my @locations_json = @{$fingerprintResults->{locations_json}};
192
6
6
2
5
            my @fingerprints = @{$fingerprintResults->{fingerprints}};
193
6
3
            if ($flatten) {
194
0
0
                my $locations_json_flat = join ',', @locations_json;
195
0
0
                my $partialFingerprints;
196
0
0
                my $partialFingerprint = (sort @fingerprints)[0];
197
0
0
                if ($partialFingerprint ne '') {
198
0
0
                    $partialFingerprints = qq<"partialFingerprints": { "cs0" : "$partialFingerprint" },>;
199                }
200
0
0
                $message =~ s/\s\\\\\([^()]+?\\\)$//g;
201
0
0
                my $result_json = qq<{"ruleId": "$code", $partialFingerprints "message": { "text": "$message" }, "locations": [ $locations_json_flat ] }>;
202
0
0
                my $result = decode_json $result_json;
203
0
0
                push @results, $result;
204            } else {
205
6
4
                my $limit = scalar @locations_json;
206
6
4
                for (my $i = 0; $i < $limit; ++$i) {
207
9
6
                    my $locations_json_flat = $locations_json[$i];
208
9
5
                    my $partialFingerprints = '';
209
9
6
                    my $partialFingerprint = $fingerprints[$i];
210
9
6
                    if ($partialFingerprint ne '') {
211
4
2
                        $partialFingerprints = qq<"partialFingerprints": { "cs0" : "$partialFingerprint" },>;
212                    }
213
9
23
                    $message =~ s/\s\\\\\([^()]+?\\\)$//g;
214
9
6
                    my $result_json = qq<{"ruleId": "$code", $partialFingerprints "message": { "text": "$message" }, "locations": [ $locations_json_flat ] }>;
215
9
9
                    my $result = decode_json $result_json;
216
9
9807
                    push @results, $result;
217                }
218            }
219        }
220    }
221
1
9
    close WARNINGS;
222
1
18
    return \@results;
223}
224
225sub get_runs_from_sarif {
226
2
3
    my ($sarif_json) = @_;
227
2
2
    my %runs_view;
228
2
3
    return %runs_view unless $sarif_json->{'runs'};
229
2
2
1
3
    my @sarif_json_runs=@{$sarif_json->{'runs'}};
230
2
2
    foreach my $sarif_json_run (@sarif_json_runs) {
231
2
2
1
6
        my %sarif_json_run_hash=%{$sarif_json_run};
232
2
3
        next unless defined $sarif_json_run_hash{'tool'};
233
234
2
2
1
4
        my %sarif_json_run_tool_hash = %{$sarif_json_run_hash{'tool'}};
235
2
2
        next unless defined $sarif_json_run_tool_hash{'driver'};
236
237
2
2
1
4
        my %sarif_json_run_tool_driver_hash = %{$sarif_json_run_tool_hash{'driver'}};
238        next unless defined $sarif_json_run_tool_driver_hash{'name'} &&
239
2
10
            defined $sarif_json_run_tool_driver_hash{'rules'};
240
241
2
1
        my $driver_name = $sarif_json_run_tool_driver_hash{'name'};
242
2
2
3
6
        my @sarif_json_run_tool_driver_rules = @{$sarif_json_run_tool_driver_hash{'rules'}};
243
2
1
        my %driver_view;
244
2
1
        for my $driver_rule (@sarif_json_run_tool_driver_rules) {
245
45
39
            next unless defined $driver_rule->{'id'};
246
45
52
            $driver_view{$driver_rule->{'id'}} = $driver_rule;
247        }
248
2
22
        $runs_view{$sarif_json_run_tool_driver_hash{'name'}} = \%driver_view;
249    }
250
2
3
    return %runs_view;
251}
252
253sub collectVersionControlProvenance {
254
2
2
    my ($file) = @_;
255
2
6
    my ($parsed_file, $git_base_dir, $prefix, $remote_url, $rev, $branch) = CheckSpelling::GitSources::git_source_and_rev($file);
256
2
31
    return '' unless $remote_url;
257
2
8
    my $base = substr $parsed_file, 0, length($file);
258
2
4
    my $provenance = [$remote_url, $rev, $branch, $git_base_dir];
259
2
11
    return JSON::PP::encode_json($provenance);
260}
261
262sub generateVersionControlProvenance {
263
1
0
    my ($versionControlProvenanceList, $run) = @_;
264
1
1
    my %provenance;
265    sub buildVersionControlProvenance {
266
1
1
        my $d = $_;
267
1
1
1
1
        my ($remote_url, $rev, $branch, $git_base_dir) = @{JSON::PP::decode_json($d)};
268
1
300
        my $dir = $git_base_dir eq '.' ? '%SRCROOT%' : "DIR_$provenanceStringToIndex{$d}";
269
1
2
        my $mappedTo = {
270            "uriBaseId" => $dir
271        };
272
1
1
        my $versionControlProvenance = {
273            "mappedTo" => $mappedTo
274        };
275
1
2
        $versionControlProvenance->{"revisionId"} = $rev if defined $rev;
276
1
3
        $versionControlProvenance->{"branch"} = $branch if defined $branch;
277
1
2
        $versionControlProvenance->{"repositoryUri"} = $remote_url if defined $remote_url;
278
1
2
        return $versionControlProvenance;
279    }
280
1
2
    @provenanceList = map(buildVersionControlProvenance,@$versionControlProvenanceList);
281
1
1
    $run->{"versionControlProvenance"} = \@provenanceList;
282}
283
284my $provenanceInsertion = 0;
285my %provenanceStringToIndex = ();
286my %directoryToProvenanceInsertion = ();
287
288sub main {
289
1
22144
    my ($sarif_template_file, $sarif_template_overlay_file, $category) = @_;
290
1
32
    unless (-f $sarif_template_file) {
291
0
0
        warn "Could not find sarif template";
292
0
0
        return '';
293    }
294
295
1
6
    $ENV{GITHUB_SERVER_URL} = '' unless defined $ENV{GITHUB_SERVER_URL};
296
1
6
    $ENV{GITHUB_REPOSITORY} = '' unless defined $ENV{GITHUB_REPOSITORY};
297
1
10
    my $sarif_template = CheckSpelling::Util::read_file $sarif_template_file;
298
1
3
    die "sarif template is empty" unless $sarif_template;
299
300
1
0
15
0
    my $json = JSON::PP->new->utf8->pretty->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b });
301
1
109
    my $sarif_json = $json->decode($sarif_template);
302
303
1
124943
    if (defined $sarif_template_overlay_file && -s $sarif_template_overlay_file) {
304
1
13
        my $merger = Hash::Merge->new();
305
1
138
        my $merge_behaviors = $merger->{'behaviors'}->{$merger->get_behavior()};
306
1
7
        my $merge_arrays = $merge_behaviors->{'ARRAY'}->{'ARRAY'};
307
308        $merge_behaviors->{'ARRAY'}->{'ARRAY'} = sub {
309
41
4105
            return $merge_arrays->(@_) if ref($_[0][0]).ref($_[1][0]);
310
41
41
17
49
            return [@{$_[1]}];
311
1
8
        };
312
313
1
5
        my $sarif_template_overlay = CheckSpelling::Util::read_file $sarif_template_overlay_file;
314
1
3
        my %runs_base = get_runs_from_sarif($sarif_json);
315
316
1
3
        my $sarif_template_hash = $json->decode($sarif_template_overlay);
317
1
1793
        my %runs_overlay = get_runs_from_sarif($sarif_template_hash);
318
1
3
        for my $run_id (keys %runs_overlay) {
319
1
2
            if (defined $runs_base{$run_id}) {
320
1
1
                my $run_base_hash = $runs_base{$run_id};
321
1
1
                my $run_overlay_hash = $runs_overlay{$run_id};
322
1
5
                for my $overlay_id (keys %$run_overlay_hash) {
323                    $run_base_hash->{$overlay_id} = $merger->merge(
324                        $run_overlay_hash->{$overlay_id},
325
1
3
                        $run_base_hash->{$overlay_id}
326                    );
327                }
328            } else {
329
0
0
                $runs_base{$run_id} = $runs_overlay{$run_id};
330            }
331        }
332        #$sarif_json->
333
1
1
26
2
        my @sarif_json_runs = @{$sarif_json->{'runs'}};
334
1
1
        foreach my $sarif_json_run (@sarif_json_runs) {
335
1
1
0
2
            my %sarif_json_run_hash=%{$sarif_json_run};
336
1
2
            next unless defined $sarif_json_run_hash{'tool'};
337
338
1
1
0
1
            my %sarif_json_run_tool_hash = %{$sarif_json_run_hash{'tool'}};
339
1
2
            next unless defined $sarif_json_run_tool_hash{'driver'};
340
341
1
1
0
2
            my %sarif_json_run_tool_driver_hash = %{$sarif_json_run_tool_hash{'driver'}};
342
1
2
            my $driver_name = $sarif_json_run_tool_driver_hash{'name'};
343            next unless defined $driver_name &&
344
1
3
                defined $sarif_json_run_tool_driver_hash{'rules'};
345
346
1
1
            my $driver_view_hash = $runs_base{$driver_name};
347
1
1
            next unless defined $driver_view_hash;
348
349
1
1
0
5
            my @sarif_json_run_tool_driver_rules = @{$sarif_json_run_tool_driver_hash{'rules'}};
350
1
1
            for my $driver_rule_number (0 .. scalar @sarif_json_run_tool_driver_rules) {
351
45
3571
                my $driver_rule = $sarif_json_run_tool_driver_rules[$driver_rule_number];
352
45
24
                my $driver_rule_id = $driver_rule->{'id'};
353                next unless defined $driver_rule_id &&
354
45
64
                    defined $driver_view_hash->{$driver_rule_id};
355
44
30
                $sarif_json_run_tool_driver_hash{'rules'}[$driver_rule_number] = $merger->merge($driver_view_hash->{$driver_rule_id}, $driver_rule);
356            }
357        }
358
1
2
        delete $sarif_template_hash->{'runs'};
359
1
1
        $sarif_json = $merger->merge($sarif_json, $sarif_template_hash);
360    }
361    {
362
1
1
1
740
1
1
        my @sarif_json_runs = @{$sarif_json->{'runs'}};
363
1
48
        foreach my $sarif_json_run (@sarif_json_runs) {
364
1
0
            my %sarif_json_run_automationDetails;
365
1
2
            $sarif_json_run_automationDetails{id} = $category;
366
1
2
            $sarif_json_run->{'automationDetails'} = \%sarif_json_run_automationDetails;
367        }
368    }
369
370
1
1
0
2
    my %sarif = %{$sarif_json};
371
372
1
3
    $sarif{'runs'}[0]{'tool'}{'driver'}{'version'} = $ENV{CHECK_SPELLING_VERSION};
373
374
1
4
    my $results = parse_warnings $ENV{warning_output};
375
1
1
    if ($results) {
376
1
3
        $sarif{'runs'}[0]{'results'} = $results;
377
1
1
        our %provenanceStringToIndex;
378
1
1
        my @provenanceList = keys %provenanceStringToIndex;
379
1
5
        generateVersionControlProvenance(\@provenanceList, $sarif{'runs'}[0]);
380
1
1
        my %codes;
381
1
2
        for my $result_ref (@$results) {
382
9
9
3
9
            my %result = %{$result_ref};
383
9
9
            $codes{$result{'ruleId'}} = 1;
384        }
385
1
2
        my $rules_ref = $sarif{'runs'}[0]{'tool'}{'driver'}{'rules'};
386
1
1
1
6
        my @rules = @{$rules_ref};
387
1
1
        my $missing_rule_definition_id = 'missing-rule-definition';
388
1
44
1
39
        my ($missing_rule_definition_ref) = grep { $_->{'id'} eq $missing_rule_definition_id } @rules;
389
1
44
1
25
        @rules = grep { defined $codes{$_->{'id'}} } @rules;
390
1
2
        my $code_index = 0;
391
1
2
0
3
        my %defined_codes = map { $_->{'id'} => $code_index++ } @rules;
392
1
3
1
3
        my @missing_codes = grep { !defined $defined_codes{$_}} keys %codes;
393
1
1
        my $missing_rule_definition_index;
394
1
1
        if (@missing_codes) {
395
1
1
            push @rules, $missing_rule_definition_ref;
396
1
3
            $missing_rule_definition_index = $defined_codes{$missing_rule_definition_id} = $code_index++;
397
1
36
            my $spellchecker = $ENV{spellchecker} || dirname(dirname(dirname(__FILE__)));
398
1
1
            my %hashes_needed_for_files = ();
399
1
1
            my %line_hashes = ();
400
1
1
            my %used_hashes = ();
401
1
0
            our %directoryToRepo;
402
1
1
            for my $missing_code (@missing_codes) {
403
1
2
                my $message = "No rule definition for `$missing_code`";
404
1
10540
                my $code_locations = `find '$spellchecker' -name '.git*' -prune -o \\( -name '*.sh' -o -name '*.pl' -o -name '*.pm' \\) -type f -print0|xargs -0 grep -n '$missing_code' | perl -pe 's<^\./><>'`;
405
1
10
                my @locations;
406
1
11
                for my $line (split /\n/, $code_locations) {
407
1
3
                    chomp $line;
408
1
18
                    my ($file, $lineno, $code) = $line =~ /^(.+?):(\d+):(.+)$/;
409
1
5
                    next unless defined $file;
410
1
42
                    $code =~ /^(.*?)\b$missing_code\b/;
411
1
6
                    my $startColumn = length($1) + 1;
412
1
3
                    my $endColumn = length($1) + length($missing_code) + 1;
413
1
10
                    my $location = {
414                        'uri' => url_encode($file),
415                        'startLine' => $lineno,
416                        'startColumn' => $startColumn,
417                        'endColumn' => $endColumn,
418                    };
419
1
130
                    my $relative = File::Spec->abs2rel($file, $spellchecker);
420
1
17
                    print STDERR "::notice title=${missing_rule_definition_id}::$relative:$lineno:$startColumn ... $endColumn, Notice - $message ($missing_rule_definition_id)\n";
421
1
3
                    push @locations, $location;
422
1
1
                    my $encoded_file = url_encode $file;
423
1
6
                    $encoded_files{$encoded_file} = $file;
424
1
7
                    addToHashesNeededForFiles($file, $lineno, $startColumn, $message, \%hashes_needed_for_files);
425                }
426
1
7
                hashFiles(\%hashes_needed_for_files, \%line_hashes, \%directoryToRepo, \%used_hashes);
427
1
4
                my $fingerprintResults = fingerprintLocations(\@locations, \%encoded_files, \%encoded_files, \%line_hashes, $message, Digest::SHA::sha1_base64($message));
428
1
1
1
1
                my @locations_json = @{$fingerprintResults->{locations_json}};
429
1
1
1
1
                my @fingerprints = @{$fingerprintResults->{fingerprints}};
430
1
3
                my $locations_json_flat = join ',', @locations_json;
431
1
1
                my $partialFingerprints = '';
432
1
5
                my $locations = $locations_json_flat ? qq<, "locations": [ $locations_json_flat ]> : '';
433
1
4
                my $result_json = qq<{"ruleId": "$missing_rule_definition_id", $partialFingerprints "message": { "text": "$message" }$locations }>;
434
1
11
                my $result = decode_json $result_json;
435
1
1
1069
7
                push @{$results}, $result;
436            }
437        }
438
1
3
        $sarif{'runs'}[0]{'tool'}{'driver'}{'rules'} = \@rules;
439
1
1
2
1
        for my $result_index (0 .. scalar @{$results}) {
440
11
9
            my $result = $results->[$result_index];
441
11
10
            my $ruleId = $result->{'ruleId'};
442
11
21
            next if defined $ruleId && defined $defined_codes{$ruleId};
443
2
195
            $result->{'ruleId'} = $missing_rule_definition_id;
444        }
445    }
446
447
1
11
    return JSON::PP->new->canonical([1])->utf8->encode(\%sarif);
448}
449
4501;