44 "encoding/json"
55 "fmt"
66 "os"
7+ "strings"
78)
89
910// PylintIssue represents a single issue in Pylint's JSON output
@@ -80,6 +81,7 @@ type ArtifactLocation struct {
8081type Region struct {
8182 StartLine int `json:"startLine"`
8283 StartColumn int `json:"startColumn"`
84+ EndLine int `json:"endLine,omitempty"`
8385}
8486
8587type MessageText struct {
@@ -290,7 +292,7 @@ type LicenseSimIssue struct {
290292}
291293
292294// ConvertLicenseSimToSarif converts license-sim JSON output to SARIF format
293- func ConvertLicenseSimToSarif (licenseSimOutput []byte ) []byte {
295+ func ConvertLicenseSimToSarifWithFile (licenseSimOutput []byte , scannedFile string ) []byte {
294296 var issues []LicenseSimIssue
295297
296298 // Try to unmarshal as {"results": [...]}
@@ -307,6 +309,21 @@ func ConvertLicenseSimToSarif(licenseSimOutput []byte) []byte {
307309 }
308310 }
309311
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+ }
320+ }
321+ }
322+ if scannedFile == "" {
323+ scannedFile = "license-sim-test.php" // fallback, ideally should be passed in
324+ }
325+ }
326+
310327 sarifReport := SarifReport {
311328 Version : "2.1.0" ,
312329 Schema : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" ,
@@ -325,13 +342,24 @@ func ConvertLicenseSimToSarif(licenseSimOutput []byte) []byte {
325342 }
326343
327344 for _ , issue := range issues {
345+ referenceFile := issue .FilePath
328346 ruleId := issue .License
329347 if ruleId == "" {
330348 ruleId = "license-sim-match"
331349 }
332350 msg := issue .Message
333351 if msg == "" {
334- msg = "code similar to licensed code"
352+ msg = "code similar to licensed code in " + referenceFile
353+ } else {
354+ msg = msg + " (reference: " + referenceFile + ")"
355+ }
356+ startLine := issue .Line
357+ if startLine <= 0 {
358+ startLine = 1
359+ }
360+ endLine := startLine
361+ if endLine <= 0 {
362+ endLine = startLine
335363 }
336364 result := Result {
337365 RuleID : ruleId ,
@@ -340,9 +368,10 @@ func ConvertLicenseSimToSarif(licenseSimOutput []byte) []byte {
340368 Locations : []Location {
341369 {
342370 PhysicalLocation : PhysicalLocation {
343- ArtifactLocation : ArtifactLocation {URI : issue . FilePath },
371+ ArtifactLocation : ArtifactLocation {URI : scannedFile },
344372 Region : Region {
345- StartLine : issue .Line ,
373+ StartLine : startLine ,
374+ EndLine : endLine ,
346375 },
347376 },
348377 },
@@ -358,3 +387,8 @@ func ConvertLicenseSimToSarif(licenseSimOutput []byte) []byte {
358387
359388 return sarifData
360389}
390+
391+ // Backward compatible wrapper
392+ func ConvertLicenseSimToSarif (licenseSimOutput []byte ) []byte {
393+ return ConvertLicenseSimToSarifWithFile (licenseSimOutput , "" )
394+ }
0 commit comments