JoseCarlosPB
JoseCarlosPB

Reputation: 875

Sonarqube gives me issues of deleted codes and can't filter issues

I'm trying to get Issues from a fileName on sonarqube with this code

public class SonarqubeServiceImpl implements SonarqubeService {

private final String SONAR_URL = "http://localhost:9000/";
private final String PROJECT_KEY = "refactor2";
private final String SRC_FOLDER = "src/main/java/com/uca/refactor2/activities";
private String EXECUTE_SONAR;
private String GET_ISSUES_URL = SONAR_URL + "api/issues/search?q=";
private static String POM_PATH = "pom.xml";
private RestTemplate restTemplate = new RestTemplate();

private InvocationRequest request;
private Invoker invoker;

@PostConstruct
private void init() {
    System.out.println("PostConstruct");
    buildSonarCommand();
    configureMavenWithSonar();
}

@Override
public void runAnalysis() throws MavenInvocationException {
    // Assuming everything is set up (done in init)

    invoker.execute(request);
}

@Override
public IssuesResponse getIssuesFromFileName(String fileName) {
    String URL = GET_ISSUES_URL + fileName;
    return restTemplate.getForObject(URL, IssuesResponse.class);
}

private void configureMavenWithSonar() {
    request = new DefaultInvocationRequest();
    request.setPomFile(new File(POM_PATH));
    request.setGoals(Collections.singletonList(EXECUTE_SONAR));

    invoker = new DefaultInvoker();
    // Set maven home in case env variables are not set
    // (and using own installation)
    invoker.setMavenHome(new File("apache-maven-3.5.2"));
}

private void buildSonarCommand() {
    StringBuilder builder = new StringBuilder();
    builder.append("sonar:sonar ");
    builder.append(String.format("-Dsonar.host.url=%s ", SONAR_URL));
    builder.append(String.format("-Dsonar.projectKey=%s ", PROJECT_KEY));
    builder.append(String.format("-Dsonar.sources=%s ", SRC_FOLDER));
    EXECUTE_SONAR = builder.toString();
        }
   }

I'm having two problem with this code. First one is that this is retrieving issues from filenames of source folder that were deleted long time ago and I don't understand why, because if I enter to sonarqube local web api on localhost/9000 that code is not there

and second and most important, I'm trying to get Issues from a filename but sonarqube is giving me issues from all the project(even projects when I had more than one), including deleted codes from the first problem I said above.

I'm fetching issues with this URL

http://localhost:9000/api/issues/search?q=" + fileName;

I'm using SonarQube version: 6.7.1

this is my first time with sonarqube so maybe I'm missing something

Upvotes: 0

Views: 555

Answers (1)

G. Ann - SonarSource Team
G. Ann - SonarSource Team

Reputation: 22824

Where did you see that q is a valid parameter? It's not in the list I'm looking at, which is probably why you're getting random results.

You need to be using componentKeys instead, altho the right-hand side is going to be more than just the file name; instead it will be something like projectKey:path/to/file so you'll need to work out the details of that.

Regarding the issues you're getting back on deleted files, closed issues are cleaned out of the database 30 days (default value) later. Since your query is currently pulling a random set of issues, unconstrained by status, that explains seeing these "ghost" issues.

Upvotes: 1

Related Questions