Setting up private GitHub R package repo with covr/codecov

PUBLISHED ON JAN 21, 2022 — GITHUB, R

Thanks to the usethis, testthat, and covr packages, integrating testing and code coverage into your R package is easier than ever. After installing the packages, the usethis package will setup the infrastructure for you:

library(usethis)
library(testthat)
library(covr)
usethis::use_testthat()
usethis::use_coverage()
usethis::use_github_action("test-coverage")

You can then start writing tests and monitor your coverage. If you’re using this on a private repository, you need to take a couple extra steps, which is where I got hung up. You first need to go to codecov.io and login with GitHub, allowing codecov access to your repositories. Then, find the repository on codecov and activate the repository under settings. Add the webhook, copy the token, and head back to GitHub. Under the repo settings on GitHub, add the token as a new ‘Secret’ and name it ‘CODECOV_TOKEN’. This is where I got stuck. GitHub and codecov still were not communicating as expected. I incorrectly assumed from the covr::codecov man page that it would find the GitHub secret – it does not. I finally found this helpful post.

You need to edit the .github/workflows/test-coverage.yaml file created by the call to use_github_action(), replacing the line

run: covr::codecov()

with

run: covr::codecov(token = "${{ secrets.CODECOV_TOKEN }}")

The two should now communicate like expected. The last step is to make sure your badge has the token; see the badges page under the codecov repo settings to get a link that works!