Skip to content

Commit

Permalink
Merge pull request #11 from MaibornWolff/newsonarqubeapi
Browse files Browse the repository at this point in the history
Connector to SonarQube Measures-API, Default metrics, Exception handling for sonarimporter
  • Loading branch information
ukinimod authored May 1, 2017
2 parents aaf7ee4 + 26ca9fe commit 63d88ee
Show file tree
Hide file tree
Showing 41 changed files with 2,572 additions and 612 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

### Fixed

## [1.1.3] - 2017-05-01
### Added
- Support for SonarQube Measures-API
- Error logging for sonarqube errors

### Changed
- Standard Sonar metric is now complexity,ncloc,functions,duplicated_lines,classes,blocker_violations,generated_lines,bugs,commented_out_code_lines,lines,violations,comment_lines,duplicated_blocks

### Removed

### Fixed

## [1.1.2] - 2017-04-28
### Added
- Translation API for Metrics
Expand Down
4 changes: 2 additions & 2 deletions analysis/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ plugins {
}

group 'de.maibornwolff.codecharta'
version '1.1.2'
version '1.1.3'

repositories {
mavenCentral()
}

allprojects {
group 'de.maibornwolff.codecharta'
version '1.1.2'
version '1.1.3'

repositories.addAll(rootProject.getBuildscript().getRepositories())

Expand Down
2 changes: 1 addition & 1 deletion analysis/dist/ccsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ esac

CC_INSTALL_DIR=$(dirname "${CCSH_PATH}")

CC_VERSION="1.1.2"
CC_VERSION="1.1.3"

CC_CMD=(
"install"
Expand Down
Binary file modified analysis/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion analysis/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Thu Feb 16 12:27:11 CET 2017
#Tue Apr 04 15:18:55 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Empty file modified analysis/gradlew
100644 → 100755
Empty file.
168 changes: 84 additions & 84 deletions analysis/gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
2 changes: 2 additions & 0 deletions analysis/import/SonarImporter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ apply plugin: 'application'

dependencies {
compile project(':model')
compile 'io.reactivex.rxjava2:rxjava:2.0.7'
compile 'org.glassfish.jersey.core:jersey-client:2.25.1'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
compile 'com.beust:jcommander:1.58'
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.maibornwolff.codecharta.importer.sonar;

import de.maibornwolff.codecharta.importer.sonar.model.Component;
import de.maibornwolff.codecharta.importer.sonar.model.Measure;
import de.maibornwolff.codecharta.importer.sonar.model.Qualifier;
import de.maibornwolff.codecharta.model.Node;
import de.maibornwolff.codecharta.model.NodeType;
import de.maibornwolff.codecharta.model.Project;
import de.maibornwolff.codecharta.nodeinserter.FileSystemPath;
import de.maibornwolff.codecharta.nodeinserter.NodeInserter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SonarComponentProjectAdapter extends Project {

public SonarComponentProjectAdapter(String name) {
super(name);
this.getNodes().add(new Node("root", NodeType.Folder));
}

public void addComponentAsNode(Component component) {
Node node = new Node(createNodeName(component.getPath()), createNodeTypeFromQualifier(component.getQualifier()), createAttributes(component.getMeasures()));
NodeInserter.insertByPath(this, createParentPath(component.getPath()), node);
}

private Map<String, Object> createAttributes(List<Measure> measures) {
return measures.stream()
.filter(this::isMeasureConvertible)
.collect(Collectors.toMap(Measure::getMetric, this::convertMetricValue));
}

private Object convertMetricValue(Measure measure) {
return Double.parseDouble(measure.getValue());
}

private boolean isMeasureConvertible(Measure measure) {
if (measure.getValue() != null) {
try {
Double.parseDouble(measure.getValue());
return true;
} catch (NumberFormatException nfe) {
return false;
}
}

return false;

}

private NodeType createNodeTypeFromQualifier(Qualifier qualifier) {
switch (qualifier) {
case FIL:
case UTS:
return NodeType.File;
default:
return NodeType.Folder;
}
}

private static String createNodeName(String path) {
return path.substring(path.lastIndexOf('/') + 1);
}

private FileSystemPath createParentPath(String lname) {
return new FileSystemPath(lname.substring(0, lname.lastIndexOf('/') + 1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.maibornwolff.codecharta.importer.sonar;

import com.google.common.collect.ImmutableList;
import de.maibornwolff.codecharta.importer.sonar.dataaccess.SonarMeasuresAPIDatasource;
import de.maibornwolff.codecharta.importer.sonar.dataaccess.SonarMetricsAPIDatasource;
import de.maibornwolff.codecharta.importer.sonar.model.Qualifier;
import de.maibornwolff.codecharta.model.Project;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;

import java.util.List;

public class SonarImporter {

public static final List STANDARD_SONAR_METRICS = ImmutableList.of(
"complexity", "ncloc", "functions", "duplicated_lines", "classes", "blocker_violations", "generated_lines", "bugs", "commented_out_code_lines", "lines", "violations", "comment_lines", "duplicated_blocks");

private SonarMeasuresAPIDatasource measuresDS;

public SonarImporter(SonarMeasuresAPIDatasource measuresDS) {
this.measuresDS = measuresDS;
}

public Project getProjectFromMeasureAPI(String name, List<String> metrics) throws SonarImporterException {
List<String> metricsList = getMetricList(metrics);
SonarComponentProjectAdapter project = new SonarComponentProjectAdapter(name);

int noPages = measuresDS.getNumberOfPages(metricsList);

Flowable.range(1, noPages)
.flatMap(p -> Flowable.just(p)
.subscribeOn(Schedulers.io())
.map(page -> measuresDS.getMeasures(metricsList, page)))
.filter(m -> m.getComponents() != null)
.flatMap(m -> Flowable.fromIterable(m.getComponents()))
.filter(c -> c.getQualifier() == Qualifier.FIL || c.getQualifier() == Qualifier.UTS)
.blockingForEach(project::addComponentAsNode);

return project;
}

public List<String> getMetricList(List<String> metrics) {
if (metrics.isEmpty()) {
return STANDARD_SONAR_METRICS;
}
return metrics;
}

}
Loading

0 comments on commit 63d88ee

Please sign in to comment.