Skip to content

Commit

Permalink
Fix handling of Windows paths on unix.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 7, 2024
1 parent 4cb69c1 commit 8dfcf3c
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package edu.hm.hafner.analysis.parser.violations;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Pattern;

import edu.hm.hafner.analysis.IssueBuilder;

import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.parsers.SarifParser;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.InvalidPathException;

/**
* Parses SARIF files.
*
Expand All @@ -17,29 +17,29 @@
public class SarifAdapter extends AbstractViolationAdapter {
private static final long serialVersionUID = -5699747899173867285L;

private static final Pattern WINDOWS_PATH_ON_UNIX = Pattern.compile("^/[a-zA-Z]:.*");

@Override
SarifParser createParser() {
return new SarifParser();
}

@Override
void updateIssueBuilder(final Violation violation, final IssueBuilder builder) {
super.updateIssueBuilder(violation, builder);
builder.setFileName(convertFileUriSchemeToPath(violation.getFile()));
}

String convertFileUriSchemeToPath(final String fileName) {
if (!fileName.startsWith("file:")) {
return fileName;
}

void extractAdditionalProperties(final IssueBuilder builder, final Violation violation) {
try {
File file = new File(new URI(fileName));
return file.toPath().toString();
}
catch (URISyntaxException | InvalidPathException ignored) {
var fileName = violation.getFile();
var uri = new URI(fileName);
builder.setFileName(removePrefix(uri.getPath()));
}
catch (URISyntaxException exception) {

Check warning on line 34 in src/main/java/edu/hm/hafner/analysis/parser/violations/SarifAdapter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 34 is not covered by tests

Check warning on line 34 in src/main/java/edu/hm/hafner/analysis/parser/violations/SarifAdapter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/analysis/parser/violations/SarifAdapter.java#L34

Added line #L34 was not covered by tests
// ignore
}
}

private String removePrefix(final String fileName) {
if (WINDOWS_PATH_ON_UNIX.matcher(fileName).matches()) {
return fileName.substring(1);
}
return fileName;
}
}

0 comments on commit 8dfcf3c

Please sign in to comment.