Blog Infos
Author
Published
Topics
Published

Photo by Fernando Reyes on Unsplash

 

Have you ever imagined being able to get some details about how much your latest pull request increased or decreased the total code coverage in the project that you are working on?

Now this is possible using BitriseGithub and a couple of scripts. Let’s get stuck into it 👊🏽

Pre-requisites
  • Jacoco plugin to generate code coverage from your unit tests. As an example, I will mention some gradle tasks that Android-Root-Coverage-Plugin offers. This SDK allows the use of a multi-module project and get either an overall or an individual module coverage report.
  • Github repository linked with Bitrise.
  • Admin permissions on Bitrise, in order to be able to configure your Workflows.
Steps
  • Create a Comparator Workflow (unit_test_and_coverage_comparison). This workflow will be in charge of running the tests, create the code coverage of the PR branch and it will execute a code coverage comparison script, to then show these values comparison in the PR comments.
  • Configure the Bitrise PR Trigger (linking the previous Comparator Workflow), in order to run a build whenever a PR is raised.

This is the PR Trigger linked to the Comparator Workflow.

 

  • Create a Code Coverage Saver Workflow (unit_test_and_save_coverage). This workflow will be in charge of saving the latest code coverage in Bitrise Secrets, after running the unit tests and parse such value from the code coverage report.
  • Configure the Bitrise Push Trigger (linking the previous Code Coverage Saver Workflow), in order to run a build when the PR gets merged with main/master (or any other targeted) branch.

This is the Push Trigger linked to the Code Coverage Saver Workflow.

 

  • Generate a Bitrise Personal Access Token to be able to execute some Bitrise API calls to access the secrets from the scripts. Then set it up in your Bitrise Secrets, as a new variable (BITRISE_PERSONAL_ACCESS_TOKEN).
  • Generate a Github Personal access token. It will be needed by the Step that adds a message in the current PR. Then set it up in your Bitrise Secrets, as a new variable (GITHUB_BOT_CODE_COVERAGE).
  • Create another variable in your Bitrise Secrets (CODE_COVERAGE), to be able to save the code coverage at some point during the process.

These three secrets will be the ones needed for the scripts and Steps to work with

 

  • Raise a PR on Github, wait until the build passes all of the checks set up on Bitrise. And then, have a look at the Github message generated on your PR. This message will contain the code coverage comparison 🤩

Comparator Workflow

  • Clone your Github repository.
  • Configure your project, if necessary.
  • Run the unit tests in the project.
  • Execute Jacoco gradle task to generate reports (i.e. -Pcoverage rootCoverageReport).
  • Parse the code coverage percentage from the report file already generated.
  • Get the value, that contains the main/master branch code coverage percentage.
# Get master branch code-coverage value (from secrets)
curl -X GET 'https://api.bitrise.io/v0.1/apps/$APP_ID/secrets/CODE_COVERAGE/value' \
-H 'accept: application/json' \
-H 'Authorization: '"${BITRISE_PERSONAL_ACCESS_TOKEN}"'' \
-H 'Content-Type: application/json' > current-code-coverage.json
MASTER_BRANCH_CODE_COVERAGE=`jq -r '.value' current-code-coverage.json`
# Parse current code-coverage value (from the Jacoco report)
chmod +x $BITRISE_SOURCE_DIR/build/reports/jacoco/index.html
CURRENT_COVERAGE=$(cat $BITRISE_SOURCE_DIR/build/reports/jacoco/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total//')
  • Set these two values in the environment variables provided by Bitrise (env man).
envman add --key PR_BRANCH_CODE_COVERAGE --value $CURRENT_COVERAGE
envman add --key MASTER_BRANCH_CODE_COVERAGE --value $MASTER_BRANCH_CODE_COVERAGE
view raw envman_set.sh hosted with ❤ by GitHub

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

No results found.

  • Use the Comment on GitHub Pull Request (Bitrise Step), in order to write a comment on the given PR, with the two code coverage percentages added to the environment variables. ⚠️ The very first time running this, the value obtained from the Bitrise Secrets, will be empty.

This is the text that will be showed in the PR message

 

Code Coverage Saver Workflow

  • Clone your Github repository.
  • Configure your project if necessary.
  • Run the unit tests in the project.
  • Execute Jacoco gradle task to generate reports (i.e. -Pcoverage rootCoverageReport).
  • Parse the code coverage percentage from the report file already generated.
  • Set that code coverage percentage in the Secrets. This new value will be available when a new PR is raised.
# Get code coverage generated by Jacoco reports
chmod +x $BITRISE_SOURCE_DIR/build/reports/jacoco/index.html
CURRENT_COVERAGE=$(cat $BITRISE_SOURCE_DIR/build/reports/jacoco/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total//')
echo ${CURRENT_COVERAGE//[[:blank:]]/}
# Try delete previous value in Bitrise Secrets
curl -X DELETE 'https://api.bitrise.io/v0.1/apps/$APP_ID/secrets/CODE_COVERAGE' \
-H 'Content-Type: application/json' \
-H 'Authorization: $BITRISE_PERSONAL_ACCESS_TOKEN'> delete_secret_result.json
# Update Code Coverage in Bitrise Secrets
curl -X PUT 'https://api.bitrise.io/v0.1/apps/$APP_ID/secrets/CODE_COVERAGE' \
-H "accept: application/json" \
-H "Authorization: $BITRISE_PERSONAL_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"expand_in_step_inputs\": false, \"is_exposed_for_pull_requests\": true, \"is_protected\": false, \"value\": \"$CURRENT_COVERAGE\"}" > secret-result.json

This excutes the last two tasks mentioned above.

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu