📦 EqualifyEverything / equalify

📄 VeraToEqualify2.ts · 107 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107/*
/* Converts Vera results to Equalify v2 Blockers format (formerly stream2)
/*
/* See equalifyv2 format types in shared/types/
*/

import {
  Blocker,
  StreamResults,
} from "../types/streamResults.equalifyV2_format";

// Inferred Interfaces from results
interface CheckDetail {
  status: string;
  context: string;
  errorMessage: string;
  errorArguments: string[];
}

interface RuleSummary {
  ruleStatus: "FAILED" | "PASSED" | "WARNING";
  specification: string; // Maps to Blocker.source
  tags?: string[] | null; // Maps to Blocker.tags
  description: string; // Maps to Blocker.description
  checks?: CheckDetail[];
}

interface ValidationResult {
  details: {
    ruleSummaries: RuleSummary[];
  };
}

interface ReportJob {
  validationResult: ValidationResult[];
}

export interface ReportData {
  report: {
    jobs: ReportJob[];
  };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function convertVeraToEqualifyV2(reportData: ReportData, job: any): StreamResults {
  const blockers: Blocker[] = [];

    try {
      // Ensure the path to jobs exists before proceeding
      if (!reportData?.report?.jobs) {
        console.warn(
          "Input JSON structure error!"
        );
        throw new Error;
      }

      // Traverse the jobs array
      for (const job of reportData.report.jobs) {
        if (!job.validationResult) continue;

        // Traverse the validation results array
        for (const validation of job.validationResult) {
          if (!validation.details?.ruleSummaries) continue;

          // Traverse the rule summaries array
          for (const ruleSummary of validation.details.ruleSummaries) {
            // Only process rules that have failed and have detailed check data
            if (ruleSummary.ruleStatus !== "FAILED" || !ruleSummary.checks) {
              continue;
            }

            // Map each individual check detail to the Blocker format
            for (const check of ruleSummary.checks) {
              const blocker: Blocker = {
                source: 'pdf-scan',
                tags: ruleSummary.tags || null,
                description: ruleSummary.description,

                // Fields specific to the individual CheckDetail instance
                test: ruleSummary.specification,
                summary: check.errorMessage,
                node: check.context,
              };
              blockers.push(blocker);
            }
          }
        }
      }
    } catch (error) {
      throw error;
    }
    const timeNow = new Date().toISOString();
    const out: StreamResults = {
      auditId: job.auditId,
      scanId: job.scanId,
      urlId: job.urlId,
      blockers,
      date: timeNow,
      message: `Vera scan of ${job.url} complete.`,
      status: "complete"
    };

    return out;
}

export default convertVeraToEqualifyV2;