Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SonarQube pick up the Cobertura Code Coverage file #1823

Merged
merged 3 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Tasks/Gradle/Strings/resources.resjson/en-US/resources.resjson
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"loc.input.help.sqAnalysisEnabled": "Run a [SonarQube analysis](http://go.microsoft.com/fwlink/?LinkID=708598) after executing the current goals. 'install' or 'package' goals should be executed first.",
"loc.input.label.sqConnectedServiceName": "SonarQube Endpoint",
"loc.input.help.sqConnectedServiceName": "The endpoint that specifies the SonarQube server to use",
"loc.input.label.sqProjectName": "SonarQube Project Name",
"loc.input.help.sqProjectName": "The SonarQube project name, i.e. sonar.projectName.",
"loc.input.label.sqProjectKey": "SonarQube Project Key",
"loc.input.help.sqProjectKey": "The SonarQube project unique key, i.e. sonar.projectKey.",
"loc.input.label.sqProjectVersion": "SonarQube Project Version",
"loc.input.help.sqProjectVersion": "The SonarQube project version, i.e. sonar.projectVersion.",
"loc.input.label.sqDbDetailsRequired": "The SonarQube server version is lower than 5.2",
"loc.input.help.sqDbDetailsRequired": "If using a SonarQube server 5.1 or lower, you must specify the database connection details.",
"loc.input.label.sqDbUrl": "Db Connection String",
Expand All @@ -45,12 +51,6 @@
"loc.input.help.sqDbUsername": "SonarQube server 5.1 and lower only. Enter the username for the database user (i.e. sonar.jdbc.username).",
"loc.input.label.sqDbPassword": "Db User Password",
"loc.input.help.sqDbPassword": "SonarQube server 5.1 and lower only. Enter the password for the database user i.e. sonar.jdbc.password",
"loc.input.label.sqProjectName": "SonarQube Project Name",
"loc.input.help.sqProjectName": "The SonarQube project name, i.e. sonar.projectName. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"loc.input.label.sqProjectKey": "SonarQube Project Key",
"loc.input.help.sqProjectKey": "The SonarQube project unique key, i.e. sonar.projectKey. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"loc.input.label.sqProjectVersion": "SonarQube Project Version",
"loc.input.help.sqProjectVersion": "The SonarQube project version, i.e. sonar.projectVersion. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"loc.messages.sqAnalysis_isEnabled": "SonarQube analysis is enabled.",
"loc.messages.sqAnalysis_isNotEnabled": "SonarQube analysis is not enabled."
}
18 changes: 16 additions & 2 deletions Tasks/Gradle/gradlesonar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {SonarQubeEndpoint} from 'sonarqube-common/sonarqube-common';

// Apply arguments to enable SonarQube analysis
export function applyEnabledSonarQubeArguments(gradleRun: trm.ToolRunner):trm.ToolRunner {
if (!tl.getBoolInput('sqAnalysisEnabled')) {
if (!isSonarQubeEnabled()) {
// Looks like: 'SonarQube analysis is not enabled.'
console.log(tl.loc('sqAnalysis_isNotEnabled'));
return gradleRun;
Expand All @@ -27,8 +27,8 @@ export function applyEnabledSonarQubeArguments(gradleRun: trm.ToolRunner):trm.To
var initScriptPath:string = path.join(__dirname, 'sonar.gradle');

// Specify that the build should run the init script
gradleRun.arg(['-I', initScriptPath]);
gradleRun.arg(['sonarqube']);
gradleRun.arg(['-I', initScriptPath]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small unrelated change - I'm adding the "sonarqube" argument before the -I arg so that all the task arguments are grouped

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AshwiniChalla - just as an aside, we found that you can pass init Gradle scripts that contain plugins. This way we didn't have to change the user's gradle.build file.


// #2: Configure additional command-line parameters
// Add parameters to connect to the SonarQube server for reporting
Expand All @@ -52,4 +52,18 @@ export function applyEnabledSonarQubeArguments(gradleRun: trm.ToolRunner):trm.To
gradleRun = sqCommon.applySonarQubeAnalysisParams(gradleRun, projectName, projectKey, projectVersion);

return gradleRun;
}

// Points SonarQube to the CC file as it is in a non-standard location. Not required for Jacoco.
export function applySonarQubeCodeCoverageArguments(gradleRun: trm.ToolRunner, isCodeCoverageEnabled:boolean, ccTool:string, reportPath:string):trm.ToolRunner {

if (isSonarQubeEnabled() && isCodeCoverageEnabled && ccTool.toLowerCase() == "cobertura" && reportPath ) {
gradleRun.arg("-Dsonar.cobertura.reportPath="+reportPath);
}

return gradleRun;
}

function isSonarQubeEnabled():boolean {
return tl.getBoolInput('sqAnalysisEnabled')
}
12 changes: 8 additions & 4 deletions Tasks/Gradle/gradletask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var gb = tl.createToolRunner(wrapperScript);

gb.argString(tl.getInput('options', false));
gb.arg(tl.getDelimitedInput('tasks', ' ', true));
gb = sqGradle.applyEnabledSonarQubeArguments(gb);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the SQ logic after the CC logic


// update JAVA_HOME if user selected specific JDK version or set path manually
var javaHomeSelection = tl.getInput('javaHomeSelection', true);
Expand Down Expand Up @@ -62,15 +62,19 @@ if (specifiedJavaHome) {
var ccTool = tl.getInput('codeCoverageTool');
var isCodeCoverageOpted = (typeof ccTool != "undefined" && ccTool && ccTool.toLowerCase() != 'none');

var summaryFile: string = null;

if (isCodeCoverageOpted) {
var summaryFile: string = null;
var reportDirectory: string = null;
enableCodeCoverage()
}
else {
tl.debug("Option to enable code coverage was not selected and is being skipped.");
}

gb = sqGradle.applyEnabledSonarQubeArguments(gb);
gb = sqGradle.applySonarQubeCodeCoverageArguments(gb, isCodeCoverageOpted, ccTool, summaryFile );

var publishJUnitResults = tl.getBoolInput('publishJUnitResults');
var testResultsFiles = tl.getInput('testResultsFiles', true);

Expand Down Expand Up @@ -121,7 +125,7 @@ function enableCodeCoverage() {

if (ccTool.toLowerCase() == "jacoco") {
var summaryFileName = "summary.xml";

if (isMultiModule) {
var reportingTaskName = "jacocoRootReport";
}
Expand All @@ -134,7 +138,7 @@ function enableCodeCoverage() {
var reportingTaskName = "cobertura";
}

summaryFile = path.join(reportDirectory, summaryFileName);
summaryFile = path.join(reportDirectory, summaryFileName);
var buildFile = path.join(buildRootPath, "build.gradle");

tl.rmRF(reportDirectory, true);
Expand Down
7 changes: 1 addition & 6 deletions Tasks/Gradle/sonar.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ allprojects {
if (project.getParent() == null && !project.plugins.hasPlugin('org.sonarqube')) {
//Add sonar plugin only to root projects, and only if it is not already applied
project.apply plugin: 'org.sonarqube'
}

// Warn if the project name has not been set
if (project.name.equalsIgnoreCase("s")) {
logger.warn('Project name is not set.')
}
}
}
}
8 changes: 4 additions & 4 deletions Tasks/Gradle/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 40
"Patch": 41
},
"demands": [
"java"
Expand Down Expand Up @@ -210,7 +210,7 @@
"type": "string",
"label": "SonarQube Project Name",
"required": true,
"helpMarkDown": "The SonarQube project name, i.e. sonar.projectName. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"helpMarkDown": "The SonarQube project name, i.e. sonar.projectName.",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change - the plugin will not set anything by default because the project name, key and version are mandatory.

"groupName": "CodeAnalysis",
"visibleRule": "sqAnalysisEnabled = true"
},
Expand All @@ -219,7 +219,7 @@
"type": "string",
"label": "SonarQube Project Key",
"required": true,
"helpMarkDown": "The SonarQube project unique key, i.e. sonar.projectKey. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"helpMarkDown": "The SonarQube project unique key, i.e. sonar.projectKey.",
"groupName": "CodeAnalysis",
"visibleRule": "sqAnalysisEnabled = true"
},
Expand All @@ -228,7 +228,7 @@
"type": "string",
"label": "SonarQube Project Version",
"required": true,
"helpMarkDown": "The SonarQube project version, i.e. sonar.projectVersion. If not set, the plugin will select its own default. See: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle",
"helpMarkDown": "The SonarQube project version, i.e. sonar.projectVersion.",
"groupName": "CodeAnalysis",
"visibleRule": "sqAnalysisEnabled = true"
},
Expand Down
2 changes: 1 addition & 1 deletion Tasks/Gradle/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 40
"Patch": 41
},
"demands": [
"java"
Expand Down
30 changes: 27 additions & 3 deletions Tests/L0/Gradle/_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,31 @@ describe('gradle Suite', function() {
});
});

it('Gradle with Cobertura and SonarQube', (done) => {
setResponseFile('gradleGood.json');

var tr = new TaskRunner('gradle', true, true);
tr = setDefaultInputs(tr);
tr.setInput('codeCoverageTool', 'Cobertura');
tr.setInput('sqAnalysisEnabled', 'true');
tr.setInput('sqConnectedServiceName', 'ID1');
tr.setInput('sqProjectName', 'test_sqProjectName');
tr.setInput('sqProjectKey', 'test_sqProjectKey');
tr.setInput('sqProjectVersion', 'test_sqProjectVersion');

tr.run()
.then(() => {
assert(tr.ran('gradlew properties'), 'it should have run gradlew build');
assert(tr.ran('gradlew build cobertura sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion -Dsonar.cobertura.reportPath=CCReport43F6D5EF/coverage.xml'), 'it should have run gradlew build');
assert(tr.stderr.length == 0, 'should not have written to stderr');
assert(tr.succeeded, 'task should have succeeded');
done();
})
.fail((err) => {
done(err);
});
})

it('Gradle with SonarQube - Should run Gradle with SonarQube', function(done) {
// Arrange

Expand All @@ -658,7 +683,7 @@ describe('gradle Suite', function() {
assert(tr.invokedToolCount == 1, 'should have only run gradle 1 time');
assert(tr.resultWasSet, 'task should have set a result');
assert(tr.stderr.length == 0, 'should not have written to stderr');
assert(tr.ran('gradlew build -I /gradle/sonar.gradle sonarqube -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion'),
assert(tr.ran('gradlew build sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion'),
'should have run the gradle wrapper with the appropriate SonarQube arguments');
done();
})
Expand Down Expand Up @@ -693,7 +718,7 @@ describe('gradle Suite', function() {
assert(tr.invokedToolCount == 1, 'should have only run gradle 1 time');
assert(tr.resultWasSet, 'task should have set a result');
assert(tr.stderr.length == 0, 'should not have written to stderr');
assert(tr.ran('gradlew build -I /gradle/sonar.gradle sonarqube -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.jdbc.url=jdbc:test:tcp://localhost:8080/sonar -Dsonar.jdbc.username=testDbUsername -Dsonar.jdbc.password=testDbPassword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion'),
assert(tr.ran('gradlew build sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.jdbc.url=jdbc:test:tcp://localhost:8080/sonar -Dsonar.jdbc.username=testDbUsername -Dsonar.jdbc.password=testDbPassword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion'),
'should have run the gradle wrapper with the appropriate SonarQube arguments');
done();
})
Expand All @@ -702,5 +727,4 @@ describe('gradle Suite', function() {
done(err);
});
});

});
17 changes: 13 additions & 4 deletions Tests/L0/Gradle/gradleGood.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
"code": 0,
"stdout": "More sample gradle output"
},
"gradlew build -I /gradle/sonar.gradle sonarqube -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion": {
"gradlew build sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion": {
"code": 0,
"stdout": "Maven package and SQ analysis done"
"stdout": "Gradle build and SQ analysis done"
},
"gradlew build -I /gradle/sonar.gradle sonarqube -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.jdbc.url=jdbc:test:tcp://localhost:8080/sonar -Dsonar.jdbc.username=testDbUsername -Dsonar.jdbc.password=testDbPassword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion": {
"gradlew build sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.jdbc.url=jdbc:test:tcp://localhost:8080/sonar -Dsonar.jdbc.username=testDbUsername -Dsonar.jdbc.password=testDbPassword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion": {
"code": 0,
"stdout": "Maven package and SQ analysis (older version) done"
"stdout": "Gradle build and SQ analysis with db details done"
},
"gradlew build cobertura sonarqube -I /gradle/sonar.gradle -Dsonar.host.url=http://sonarqube/end/point -Dsonar.login=uname -Dsonar.password=pword -Dsonar.projectName=test_sqProjectName -Dsonar.projectKey=test_sqProjectKey -Dsonar.projectVersion=test_sqProjectVersion -Dsonar.cobertura.reportPath=CCReport43F6D5EF/coverage.xml": {
"code": 0,
"stdout": "Gradle build, Cobertura Code Coverage and SQ analysis done"
}
},
"checkPath": {
Expand All @@ -50,6 +54,11 @@
"ENDPOINT_URL_ID1": "http://sonarqube/end/point",
"ENDPOINT_AUTH_ID1": "{\"scheme\":\"UsernamePassword\", \"parameters\": {\"username\": \"uname\", \"password\": \"pword\"}}"
},
"stats": {
"CCReport43F6D5EF\\coverage.xml": {
"isFile": true
}
},
"rmRF": {
"CCReport43F6D5EF": {
"success": true,
Expand Down