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

feat: add sonar code coverage #798

Merged
merged 6 commits into from
Nov 24, 2023
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
16 changes: 8 additions & 8 deletions .github/workflows/gandalf.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: Eno V3 test

on:
pull_request:
branches:
- 'v3-main'
- 'v3-next'
types: [opened, synchronize, reopened, ready_for_review]
paths-ignore: ['docs/**', 'logo/**', 'Dockerfile', 'README**.md']
on: workflow_dispatch # replaced by sonar workflow
# pull_request:
# branches:
# - 'v3-main'
# - 'v3-next'
# types: [opened, synchronize, reopened, ready_for_review]
# paths-ignore: ['docs/**', 'logo/**', 'Dockerfile', 'README**.md']

jobs:
test:
if: ${{ (github.event.pull_request.draft == false) && !contains(github.event.pull_request.labels.*.name, 'deploy-snapshot') }}
# if: ${{ (github.event.pull_request.draft == false) && !contains(github.event.pull_request.labels.*.name, 'deploy-snapshot') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test and Sonar Analysis

on:
pull_request:
branches:
- 'v3-main'
- 'v3-next'
types: [opened, synchronize, reopened, ready_for_review]
paths-ignore: ['docs/**', 'logo/**', 'Dockerfile', 'README**.md']

jobs:
build:
if: ${{ (github.event.pull_request.draft == false) && !contains(github.event.pull_request.labels.*.name, 'deploy-snapshot') }}
name: Build and analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew clean build test codeCoverageReport
54 changes: 54 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.1.5' apply false
id 'io.spring.dependency-management' version '1.1.4' apply false
id 'jacoco'
id "org.sonarqube" version "4.4.1.3373"
}

allprojects {
Expand All @@ -12,6 +14,17 @@ allprojects {
}

subprojects {
apply plugin: 'org.sonarqube'
sonar {
properties {
property 'sonar.coverage.jacoco.xmlReportPaths', "$projectDir.parentFile.path/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
}
}

tasks.withType(Test).configureEach {
useJUnitPlatform()
}

compileJava {
options.release = 17
}
Expand All @@ -22,6 +35,47 @@ subprojects {
}
}

apply from: "$project.rootDir/sonar.gradle"

// See here for more info: https://docs.gradle.org/6.4-rc-1/samples/sample_jvm_multi_project_with_code_coverage.html
//
// task to gather code coverage from multiple subprojects
// NOTE: the `JacocoReport` tasks do *not* depend on the `test` task by default. Meaning you have to ensure
// that `test` (or other tasks generating code coverage) run before generating the report.
// You can achieve this by calling the `test` lifecycle task manually
// $ ./gradlew test codeCoverageReport
tasks.register("codeCoverageReport", JacocoReport) {
// If a subproject applies the 'jacoco' plugin, add the result it to the report
subprojects { subproject ->
subproject.plugins.withType(JacocoPlugin).configureEach {
subproject.tasks.matching({ t -> t.extensions.findByType(JacocoTaskExtension) }).configureEach { testTask ->
//the jacoco extension may be disabled for some projects
if (testTask.extensions.getByType(JacocoTaskExtension).isEnabled()) {
sourceSets subproject.sourceSets.main
executionData(testTask)
} else {
logger.warn('Jacoco extension is disabled for test task \'{}\' in project \'{}\'. this test task will be excluded from jacoco report.',testTask.getName(),subproject.getName())
}
}

// To automatically run `test` every time `./gradlew codeCoverageReport` is called,
// you may want to set up a task dependency between them as shown below.
// Note that this requires the `test` tasks to be resolved eagerly (see `forEach`) which
// may have a negative effect on the configuration time of your build.
subproject.tasks.matching({ t -> t.extensions.findByType(JacocoTaskExtension) }).forEach {
rootProject.tasks.codeCoverageReport.dependsOn(it)
}
}
}

// enable the different report types (html, xml, csv)
reports {
xml.required = true
html.required = true
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
}

tasks.register('printVersion') {
doLast {
println project.version
Expand Down
1 change: 1 addition & 0 deletions eno-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'io.spring.dependency-management'
id 'java-library'
id 'maven-publish'
id 'jacoco'
}

// https://stackoverflow.com/a/61671513/13425151
Expand Down
1 change: 1 addition & 0 deletions eno-treatments/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'io.spring.dependency-management'
id 'java-library'
id 'maven-publish'
id 'jacoco'
}

// https://stackoverflow.com/a/61671513/13425151
Expand Down
1 change: 1 addition & 0 deletions eno-ws/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'io.spring.dependency-management'
id 'java'
id 'maven-publish'
id 'jacoco'
}

ext {
Expand Down
9 changes: 9 additions & 0 deletions sonar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: "org.sonarqube"

sonar {
properties {
property 'sonar.projectKey', 'InseeFr_Eno'
property 'sonar.host.url', 'https://sonarcloud.io'
property 'sonar.organization', 'inseefr'
}
}