Skip to content

Commit 868cbab

Browse files
wip still building duncan support
1 parent 69e0030 commit 868cbab

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
lines changed

license-sim-test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
function wpmu_activate_stylesheet() {
4+
?>
5+
<style type="text/css">
6+
.wp-activate-container { width: 90%; margin: 0 auto; }
7+
.wp-activate-container form { margin-top: 2em; }
8+
#submit, #key { width: 100%; font-size: 24px; box-sizing: border-box; }
9+
#language { margin-top: 0.5em; }
10+
.wp-activate-container .error { background: #f66; color: #333; }
11+
span.h3 { padding: 0 8px; font-size: 1.3em; font-weight: 600; }
12+
</style>
13+
<?php
14+
}
15+
add_action( 'wp_head', 'wpmu_activate_stylesheet' );
16+
add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
17+
add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
18+
19+
get_header( 'wp-activate' );
20+
21+
$blog_details = get_site();

tools/licenseSimRunner.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tools
22

33
import (
4+
"codacy/cli-v2/utils"
45
"fmt"
56
"os"
67
"os/exec"
@@ -10,29 +11,75 @@ import (
1011

1112
// RunLicenseSim runs the license-sim tool (duncan.py) in the Python venv with the required environment variable.
1213
func RunLicenseSim(workDirectory string, binary string, files []string, outputFile string, outputFormat string) error {
13-
// Determine the file to check and extension
1414
var fileToCheck string
1515
var ext string
1616
if len(files) > 0 {
1717
fileToCheck = files[0]
1818
ext = filepath.Ext(fileToCheck)
1919
if ext != "" {
2020
ext = ext[1:] // remove dot
21+
// Hardcode support for .php files
22+
if ext == ".php" {
23+
ext = "php"
24+
}
2125
} else {
2226
ext = "py" // default
2327
}
2428
} else {
2529
return fmt.Errorf("No file specified for license-sim")
2630
}
2731

28-
// Prepare command: ../../license-sim/venv/bin/python ../../license-sim/duncan.py search -f <file> -e <ext>
2932
parts := strings.Split(binary, " ")
3033
cmdArgs := append(parts[1:], "search", "-f", fileToCheck, "-e", ext)
34+
35+
if outputFormat == "sarif" {
36+
tempFile, err := os.CreateTemp("", "license-sim-*.json")
37+
if err != nil {
38+
return fmt.Errorf("failed to create temporary file: %w", err)
39+
}
40+
tempFilePath := tempFile.Name()
41+
tempFile.Close()
42+
defer os.Remove(tempFilePath)
43+
44+
cmdArgs = append(cmdArgs, "--json")
45+
cmd := exec.Command(parts[0], cmdArgs...)
46+
cmd.Dir = workDirectory
47+
cmd.Env = append(os.Environ(), "KMP_DUPLICATE_LIB_OK=TRUE")
48+
outFile, err := os.Create(tempFilePath)
49+
if err != nil {
50+
return fmt.Errorf("failed to redirect output: %w", err)
51+
}
52+
defer outFile.Close()
53+
cmd.Stdout = outFile
54+
cmd.Stderr = os.Stderr
55+
56+
if err := cmd.Run(); err != nil {
57+
return fmt.Errorf("failed to run license-sim: %w", err)
58+
}
59+
60+
jsonOutput, err := os.ReadFile(tempFilePath)
61+
if err != nil {
62+
return fmt.Errorf("failed to read license-sim output: %w", err)
63+
}
64+
65+
sarifOutput := utils.ConvertLicenseSimToSarif(jsonOutput)
66+
67+
if outputFile != "" {
68+
err = os.WriteFile(outputFile, sarifOutput, 0644)
69+
if err != nil {
70+
return fmt.Errorf("failed to write SARIF output: %w", err)
71+
}
72+
} else {
73+
fmt.Println(string(sarifOutput))
74+
}
75+
return nil
76+
}
77+
78+
// Non-SARIF output
3179
cmd := exec.Command(parts[0], cmdArgs...)
3280
cmd.Dir = workDirectory
3381
cmd.Env = append(os.Environ(), "KMP_DUPLICATE_LIB_OK=TRUE")
3482

35-
// Output handling
3683
if outputFile != "" {
3784
outputWriter, err := os.Create(filepath.Clean(outputFile))
3885
if err != nil {

utils/sarif.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,85 @@ func FilterRulesFromSarif(sarifData []byte) ([]byte, error) {
276276

277277
return filteredData, nil
278278
}
279+
280+
// LicenseSimIssue represents a single issue from license-sim JSON output
281+
// Adjust fields as needed based on actual license-sim JSON output
282+
// Example fields: File, Function, License, Similarity, etc.
283+
type LicenseSimIssue struct {
284+
FilePath string `json:"file_path"`
285+
Function string `json:"function_name"`
286+
License string `json:"license_type"`
287+
Similarity float64 `json:"similarity"`
288+
Line int `json:"line"`
289+
Message string `json:"message"`
290+
}
291+
292+
// ConvertLicenseSimToSarif converts license-sim JSON output to SARIF format
293+
func ConvertLicenseSimToSarif(licenseSimOutput []byte) []byte {
294+
var issues []LicenseSimIssue
295+
296+
// Try to unmarshal as {"results": [...]}
297+
var wrapper struct {
298+
Results []LicenseSimIssue `json:"results"`
299+
}
300+
if err := json.Unmarshal(licenseSimOutput, &wrapper); err == nil && len(wrapper.Results) > 0 {
301+
issues = wrapper.Results
302+
} else {
303+
// Fallback: try to unmarshal as a flat array
304+
if err := json.Unmarshal(licenseSimOutput, &issues); err != nil {
305+
fmt.Fprintf(os.Stderr, "[DEBUG] LicenseSimToSarif: failed to parse input as array or results wrapper: %v\nRaw input: %s\n", err, string(licenseSimOutput))
306+
return createEmptySarifReport()
307+
}
308+
}
309+
310+
sarifReport := SarifReport{
311+
Version: "2.1.0",
312+
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
313+
Runs: []Run{
314+
{
315+
Tool: Tool{
316+
Driver: Driver{
317+
Name: "license-sim",
318+
Version: "local",
319+
InformationURI: "https://github.com/codacy/license-sim",
320+
},
321+
},
322+
Results: make([]Result, 0, len(issues)),
323+
},
324+
},
325+
}
326+
327+
for _, issue := range issues {
328+
ruleId := issue.License
329+
if ruleId == "" {
330+
ruleId = "license-sim-match"
331+
}
332+
msg := issue.Message
333+
if msg == "" {
334+
msg = "code similar to licensed code"
335+
}
336+
result := Result{
337+
RuleID: ruleId,
338+
Level: "note",
339+
Message: MessageText{Text: msg},
340+
Locations: []Location{
341+
{
342+
PhysicalLocation: PhysicalLocation{
343+
ArtifactLocation: ArtifactLocation{URI: issue.FilePath},
344+
Region: Region{
345+
StartLine: issue.Line,
346+
},
347+
},
348+
},
349+
},
350+
}
351+
sarifReport.Runs[0].Results = append(sarifReport.Runs[0].Results, result)
352+
}
353+
354+
sarifData, err := json.MarshalIndent(sarifReport, "", " ")
355+
if err != nil {
356+
return createEmptySarifReport()
357+
}
358+
359+
return sarifData
360+
}

0 commit comments

Comments
 (0)