Skip to content

Add jacoco reports parser

Panos Kanellidis requested to merge add_jacoco_reports_parser into master

What does this MR do and why?

This MR adds the Jacoco Coverage Reports Visualization

Changes:

  • Adds JacocoDocument parser
  • Exposes the Merge Request changed files in order to complete the full paths as jacoco only provides relative paths
  • Adds boilerplate code to enable Jacoco on Gitlab's ci.yml files

By design, Jacoco needs to run and generate the report in Debug mode in order to provide line coverage.

Changes for next MRs:

  • Document the feature
  • Potential bug fixes

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshot_2024-08-01_at_11.27.57_AM Screenshot_2024-08-01_at_12.33.57_PM

How to set up and validate locally

  1. Set up your local runners
  2. Create a new project using the Java/Spring project template from Gitlab
  3. Edit the pom.xml file and add the jacoco dependency found below
  4. Edit the gitlab-ci.yml and add the configuration found below
  5. Create a new branch and edit the source file and its' respective test file in order for them to appear in the MR Diff
  6. Create a new MR for the branch and let the pipeline run (You might need to retry the job a few times)
  7. Finally, when the Pipeline succeeds, go to your MR diff and see the highlighted marks. If the marks don't appear, try refreshing your MR page.

pom.xml

Add the following plugin to the pom.xml file

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.8</version> <!-- use the latest version -->
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

gitlab-ci.yml

This configuration is if you're using a shell runner on a mac.


stages:
  - test
  - report

before_script:
  # Install homebrew
  - which brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  - echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/$USER/.zprofile
  - eval "$(/opt/homebrew/bin/brew shellenv)"

  # Install Maven if not installed
  - which mvn || brew install maven

  # Set Maven options
  - 'export MAVEN_CLI_OPTS="-B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --no-transfer-progress"'
  - 'export MAVEN_OPTS="-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"'


variables:
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"

cache:
  paths:
    - .m2/repository

stages:
  - test

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS clean org.jacoco:jacoco-maven-plugin:prepare-agent test jacoco:report
  artifacts:
    reports:
      coverage_report:
        coverage_format: jacoco
        path: target/site/jacoco/jacoco.xml

Edited by Panos Kanellidis

Merge request reports

Loading