Skip to content

Commit 0c7e979

Browse files
author
Adam Soos
committed
WS-3330: CI add coverage for sonar
1 parent e87fab5 commit 0c7e979

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Dependency directory
22
node_modules
33

4+
coverage
45
target
56

67
.grunt

CI.Jenkinsfile

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,41 @@ node ("docker-light") {
99
checkout scm
1010
}
1111
stage("Build And Test") {
12-
nodeVersions.each { version ->
13-
sh "docker run --rm \
14-
--pull always \
15-
--volume ${sourceDir}:/source \
16-
node:${version}-alpine \
17-
sh -c \"cd /source && npm install && npx grunt\""
12+
13+
withSonarQubeEnv {
14+
mySonarOpts="-Dsonar.login=${env.SONAR_AUTH_TOKEN} -Dsonar.host.url=${env.SONAR_HOST_URL} -D"
15+
if("${env.CHANGE_ID}" != "null"){
16+
mySonarOpts = "$mySonarOpts -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.branch=${env.BRANCH_NAME}"
17+
} else {
18+
mySonarOpts = "$mySonarOpts -Dsonar.branch.name=${env.BRANCH_NAME}"
19+
}
20+
if ("${env.CHANGE_BRANCH}" != "null") {
21+
mySonarOpts="$mySonarOpts -Dsonar.pullrequest.base=${env.CHANGE_TARGET} -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH}"
22+
}
23+
24+
nodeVersions.each { version ->
25+
if (version == "23") {
26+
sonarExec="cd /root/ && \
27+
wget -q https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.8.1.3023-linux.zip && \
28+
unzip -q sonar-scanner-cli-4.8.1.3023-linux.zip && \
29+
cd /source && \
30+
/root/sonar-scanner-4.8.1.3023-linux/bin/sonar-scanner ${mySonarOpts}"
31+
} else {
32+
sonarExec="echo Skipping Sonar for this version."
33+
}
34+
35+
sh "docker run --rm \
36+
--pull always \
37+
--volume ${sourceDir}:/source \
38+
node:${version}-alpine \
39+
sh -c \"cd /source && npm install && npx grunt && ${sonarExec} && chown -R 9960:9960 /source\""
40+
}
1841
}
1942
}
2043
}
2144
catch (e) {
2245
currentBuild.result = "FAILED"
2346
throw e
2447
}
25-
}
48+
}
49+
// todo post to teams

Gruntfile.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function(grunt) {
2424
}
2525
}
2626
},
27-
mochaTest: {
27+
mochaTest: {
2828
test: {
2929
options: {
3030
reporter: 'spec',
@@ -34,6 +34,17 @@ module.exports = function(grunt) {
3434
src: ['tests/**/*.js']
3535
}
3636
},
37+
nyc: {
38+
cover: {
39+
options: {
40+
include: ['lib/**/*.js'],
41+
reporter: ['html', 'lcov'],
42+
reportDir: 'coverage'
43+
},
44+
cmd: false,
45+
args: ['grunt', 'mochaTest']
46+
}
47+
},
3748
'gh-pages': {
3849
options: {
3950
base: 'target/html'
@@ -54,14 +65,16 @@ module.exports = function(grunt) {
5465

5566
// These plugins provide necessary tasks.
5667
grunt.loadNpmTasks("grunt-contrib-clean");
57-
grunt.loadNpmTasks("grunt-contrib-watch");;
68+
grunt.loadNpmTasks("grunt-contrib-watch");
5869
grunt.loadNpmTasks("grunt-jsdoc");
5970
grunt.loadNpmTasks('grunt-mocha-test');
6071
grunt.loadNpmTasks('grunt-gh-pages');
72+
grunt.loadNpmTasks('grunt-simple-nyc');
6173

6274
// Task definitions.
6375
// run `grunt <task>` in command line and it will run the sequence in brackets
64-
grunt.registerTask("default", ["clean","jsdoc", "test"]);
76+
grunt.registerTask("default", ["clean","jsdoc", "nyc:cover"]);
6577
grunt.registerTask("doc", ["jsdoc", "gh-pages"]);
6678
grunt.registerTask("test", ["mochaTest"]);
79+
grunt.registerTask("coverage", ["nyc:cover"]);
6780
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
"grunt-jsdoc": "^2.4.1",
5555
"grunt-jslint": "^1.1.15",
5656
"grunt-mocha-test": "^0.13.3",
57+
"grunt-simple-nyc": "^3.0.1",
5758
"mocha": "^10.8.2",
5859
"nock": "^13.5.5",
60+
"nyc": "^17.1.0",
5961
"temporary": "1.1.0"
6062
},
6163
"dependencies": {

sonar-project.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sonar.projectKey=rosette-api-nodejs-binding
2+
sonar.sources=lib
3+
sonar.javascript.lcov.reportPaths=./coverage/lcov.info

0 commit comments

Comments
 (0)