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