@@ -283,47 +283,114 @@ func FilterRulesFromSarif(sarifData []byte) ([]byte, error) {
283283// Adjust fields as needed based on actual license-sim JSON output
284284// Example fields: File, Function, License, Similarity, etc.
285285type LicenseSimIssue struct {
286- FilePath string `json:"file_path"`
287- Function string `json:"function_name"`
288- License string `json:"license_type"`
289- Similarity float64 `json:"similarity"`
290- Line int `json:"line"`
291- Message string `json:"message"`
286+ FilePath string `json:"file_path"`
287+ Function string `json:"function_name"`
288+ License string `json:"license_type"`
289+ Similarity float64 `json:"similarity"`
290+ Line int `json:"line"`
291+ Message string `json:"message"`
292+ SeverityLevel string `json:"severity_level"`
293+ EnhancedMessage string `json:"enhanced_message"`
292294}
293295
294- // ConvertLicenseSimToSarif converts license-sim JSON output to SARIF format
295- func ConvertLicenseSimToSarifWithFile (licenseSimOutput []byte , scannedFile string ) [] byte {
296+ // parseLicenseSimInput parses the license-sim JSON output into issues
297+ func parseLicenseSimInput (licenseSimOutput []byte ) ([] LicenseSimIssue , error ) {
296298 var issues []LicenseSimIssue
297299
298300 // Try to unmarshal as {"results": [...]}
299301 var wrapper struct {
300302 Results []LicenseSimIssue `json:"results"`
301303 }
302304 if err := json .Unmarshal (licenseSimOutput , & wrapper ); err == nil && len (wrapper .Results ) > 0 {
303- issues = wrapper .Results
304- } else {
305- // Fallback: try to unmarshal as a flat array
306- if err := json .Unmarshal (licenseSimOutput , & issues ); err != nil {
307- fmt .Fprintf (os .Stderr , "[DEBUG] LicenseSimToSarif: failed to parse input as array or results wrapper: %v\n Raw input: %s\n " , err , string (licenseSimOutput ))
308- return createEmptySarifReport ()
309- }
305+ return wrapper .Results , nil
310306 }
311307
312- if scannedFile == "" {
313- // Try to detect the scanned file from the input (first issue's file_path or fallback)
314- if len (issues ) > 0 {
315- for _ , issue := range issues {
316- if ! strings .HasPrefix (issue .FilePath , "../" ) && issue .FilePath != "" {
317- scannedFile = issue .FilePath
318- break
319- }
308+ // Fallback: try to unmarshal as a flat array
309+ if err := json .Unmarshal (licenseSimOutput , & issues ); err != nil {
310+ return nil , fmt .Errorf ("failed to parse input: %w" , err )
311+ }
312+ return issues , nil
313+ }
314+
315+ // determineScannedFile determines the scanned file name from issues or uses fallback
316+ func determineScannedFile (issues []LicenseSimIssue , scannedFile string ) string {
317+ if scannedFile != "" {
318+ return scannedFile
319+ }
320+
321+ // Try to detect the scanned file from the input (first issue's file_path or fallback)
322+ if len (issues ) > 0 {
323+ for _ , issue := range issues {
324+ if ! strings .HasPrefix (issue .FilePath , "../" ) && issue .FilePath != "" {
325+ return issue .FilePath
320326 }
321327 }
322- if scannedFile == "" {
323- scannedFile = "license-sim-test.php" // fallback, ideally should be passed in
328+ }
329+ return "license-sim-test.php" // fallback
330+ }
331+
332+ // processLicenseSimIssue converts a single license-sim issue to a SARIF result
333+ func processLicenseSimIssue (issue LicenseSimIssue , scannedFile string ) * Result {
334+ ruleId := issue .License
335+ if ruleId == "" {
336+ ruleId = "license-sim-match"
337+ }
338+
339+ // Use severity level and message from license-sim if available
340+ level := issue .SeverityLevel
341+ if level == "" {
342+ // Fallback to note if not provided
343+ level = "note"
344+ }
345+
346+ message := issue .EnhancedMessage
347+ if message == "" {
348+ // Fallback to basic message if enhanced message not available
349+ percent := int (issue .Similarity * 100 )
350+ if issue .FilePath != "" {
351+ message = fmt .Sprintf ("code similar to licensed code (%d%%) in %s" , percent , issue .FilePath )
352+ } else {
353+ message = fmt .Sprintf ("code similar to licensed code (%d%%)" , percent )
324354 }
325355 }
326356
357+ startLine := issue .Line
358+ if startLine <= 0 {
359+ startLine = 1
360+ }
361+ endLine := startLine
362+ if endLine <= 0 {
363+ endLine = startLine
364+ }
365+
366+ return & Result {
367+ RuleID : ruleId ,
368+ Level : level ,
369+ Message : MessageText {Text : message },
370+ Locations : []Location {
371+ {
372+ PhysicalLocation : PhysicalLocation {
373+ ArtifactLocation : ArtifactLocation {URI : scannedFile },
374+ Region : Region {
375+ StartLine : startLine ,
376+ EndLine : endLine ,
377+ },
378+ },
379+ },
380+ },
381+ }
382+ }
383+
384+ // ConvertLicenseSimToSarif converts license-sim JSON output to SARIF format
385+ func ConvertLicenseSimToSarifWithFile (licenseSimOutput []byte , scannedFile string ) []byte {
386+ issues , err := parseLicenseSimInput (licenseSimOutput )
387+ if err != nil {
388+ fmt .Fprintf (os .Stderr , "[DEBUG] LicenseSimToSarif: %v\n Raw input: %s\n " , err , string (licenseSimOutput ))
389+ return createEmptySarifReport ()
390+ }
391+
392+ scannedFile = determineScannedFile (issues , scannedFile )
393+
327394 sarifReport := SarifReport {
328395 Version : "2.1.0" ,
329396 Schema : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" ,
@@ -342,44 +409,9 @@ func ConvertLicenseSimToSarifWithFile(licenseSimOutput []byte, scannedFile strin
342409 }
343410
344411 for _ , issue := range issues {
345- ruleId := issue .License
346- if ruleId == "" {
347- ruleId = "license-sim-match"
348- }
349- // Compose message with similarity score and reference file if available
350- percent := int (issue .Similarity * 100 )
351- msg := issue .Message
352- if msg == "" {
353- msg = fmt .Sprintf ("code similar to licensed code (%d%%)" , percent )
354- if issue .FilePath != "" {
355- msg += " in " + issue .FilePath
356- }
357- }
358- startLine := issue .Line
359- if startLine <= 0 {
360- startLine = 1
412+ if result := processLicenseSimIssue (issue , scannedFile ); result != nil {
413+ sarifReport .Runs [0 ].Results = append (sarifReport .Runs [0 ].Results , * result )
361414 }
362- endLine := startLine
363- if endLine <= 0 {
364- endLine = startLine
365- }
366- result := Result {
367- RuleID : ruleId ,
368- Level : "note" ,
369- Message : MessageText {Text : msg },
370- Locations : []Location {
371- {
372- PhysicalLocation : PhysicalLocation {
373- ArtifactLocation : ArtifactLocation {URI : scannedFile },
374- Region : Region {
375- StartLine : startLine ,
376- EndLine : endLine ,
377- },
378- },
379- },
380- },
381- }
382- sarifReport .Runs [0 ].Results = append (sarifReport .Runs [0 ].Results , result )
383415 }
384416
385417 sarifData , err := json .MarshalIndent (sarifReport , "" , " " )
0 commit comments