Quick Start API

badamowicz edited this page Oct 7, 2014 · 5 revisions

For accessing a SonarQube server and retrieve a list of all projects, first add the Sonar-HLA dependency to your Maven project. Currently there's no assembly available which contains all necessary libraries. If you think this might be useful, just file an issue or drop me an email.

<dependency>
  <groupId>com.github.badamowicz</groupId>
  <artifactId>sonar-hla</artifactId>
  <version>0.4.1</version>
</dependency>

Then, these lines will get all projects:

import java.util.List;

import com.github.badamowicz.sonar.hla.api.HLAMeasure;
import com.github.badamowicz.sonar.hla.api.IProject;
import com.github.badamowicz.sonar.hla.api.ISonarExtractor;
import com.github.badamowicz.sonar.hla.impl.SonarHLAFactory;

public class ExtractProjectsExample {

    public void extractProjects() {

        ISonarExtractor extractor = null;
        List<IProject> projects = null;

        extractor = SonarHLAFactory.getExtractor("http://localhost:9000");
        projects = extractor.getAllProjects();

        for (IProject currentProject : projects) {

            for (HLAMeasure currentMeasure : currentProject.getMeasures()) {

                // Read all measures and do something with them:
                currentProject.getMeasureValue(currentMeasure, false);
            }
        }
    }
}

It's that easy! Of course there are some more options:

  • Provide user credentials for accessing SonarQube.
  • Retrieve only a single project by specifying its key.
  • Retrieve a subset of projects by specifying a pattern which must match projects keys.

Have a look at the Javadocs, it's well documented! Or simply have a look at the API reference here in the Wiki.

Going a step further, we now want to convert the projects retrieved from SonarQube into a CSV list:

import java.util.Arrays;
import java.util.List;

import com.github.badamowicz.sonar.hla.api.HLAMeasure;
import com.github.badamowicz.sonar.hla.api.IProject;
import com.github.badamowicz.sonar.hla.api.ISonarConverter;
import com.github.badamowicz.sonar.hla.api.ISonarExtractor;
import com.github.badamowicz.sonar.hla.impl.SonarHLAFactory;

public class ExtractProjectsExample {

    public void extractProjects() {

        ISonarExtractor extractor = null;
        ISonarConverter converter = null;
        List<IProject> projects = null;
        String csvData = null;

        extractor = SonarHLAFactory.getExtractor("http://localhost:9000");
        converter = SonarHLAFactory.getConverterInstance();
        projects = extractor.getAllProjects();
        csvData = converter.getCSVData(projects, Arrays.asList(HLAMeasure.values()), true);

        // Now do something with CSV...
    }
}

Optionally, you may also create a stream object containing the CSV data or a file for further processing. And again, have a look at the Javadocs. They will provide sufficient information. If you feel something is missing, just file an issue.

Maybe you may also want to refer to my homepage as well as my Blog which contains various posts about different aspects of Software Management, Continuous Integration and some related stuff.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.