Skip to content

Commit db8882c

Browse files
hacks required to get license-sim running for board demo
1 parent 868cbab commit db8882c

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

cmd/analyze.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,35 +94,41 @@ func GetFileExtension(filePath string) string {
9494
func IsToolSupportedForFile(toolName string, filePath string, langConfig *LanguagesConfig) bool {
9595
if langConfig == nil {
9696
// If no language config is available, assume all tools are supported
97+
fmt.Printf("[DEBUG] No language config, assuming tool %s supports file %s\n", toolName, filePath)
9798
return true
9899
}
99100

100101
fileExt := GetFileExtension(filePath)
101102
if fileExt == "" {
102103
// If file has no extension, assume tool is supported
104+
fmt.Printf("[DEBUG] File %s has no extension, assuming tool %s supports it\n", filePath, toolName)
103105
return true
104106
}
105107

106108
for _, tool := range langConfig.Tools {
107109
if tool.Name == toolName {
108110
// If tool has no extensions defined, assume it supports all files
109111
if len(tool.Extensions) == 0 {
112+
fmt.Printf("[DEBUG] Tool %s has no extensions defined, supports all files. File: %s\n", toolName, filePath)
110113
return true
111114
}
112115

113-
// Check if file extension is supported by this tool
116+
fmt.Printf("[DEBUG] Checking if tool %s supports file %s (ext: %s). Tool extensions: %v\n", toolName, filePath, fileExt, tool.Extensions)
114117
for _, ext := range tool.Extensions {
115118
if strings.EqualFold(ext, fileExt) {
119+
fmt.Printf("[DEBUG] Tool %s supports file %s (matched ext: %s)\n", toolName, filePath, fileExt)
116120
return true
117121
}
118122
}
119123

124+
fmt.Printf("[DEBUG] Tool %s does NOT support file %s (ext: %s)\n", toolName, filePath, fileExt)
120125
// Extension not found in tool's supported extensions
121126
return false
122127
}
123128
}
124129

125130
// If tool not found in config, assume it's supported
131+
fmt.Printf("[DEBUG] Tool %s not found in language config, assuming it supports file %s\n", toolName, filePath)
126132
return true
127133
}
128134

tools/licenseSimRunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func RunLicenseSim(workDirectory string, binary string, files []string, outputFi
6262
return fmt.Errorf("failed to read license-sim output: %w", err)
6363
}
6464

65-
sarifOutput := utils.ConvertLicenseSimToSarif(jsonOutput)
65+
sarifOutput := utils.ConvertLicenseSimToSarifWithFile(jsonOutput, fileToCheck)
6666

6767
if outputFile != "" {
6868
err = os.WriteFile(outputFile, sarifOutput, 0644)

utils/sarif.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
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 {
8081
type Region struct {
8182
StartLine int `json:"startLine"`
8283
StartColumn int `json:"startColumn"`
84+
EndLine int `json:"endLine,omitempty"`
8385
}
8486

8587
type 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

Comments
 (0)