/
add_github_actions_codecov_badge.R
95 lines (67 loc) · 2.41 KB
/
add_github_actions_codecov_badge.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#' Add a Test coverage badge
#'
#' @description
#' This function adds a **Test coverage** badge to the `README.Rmd`. This
#' function must be run after [add_github_actions_codecov()] which will setup
#' GitHub Actions to report the percentage of code cover by units tests.
#'
#' Make sure that 1) a `README.Rmd` file exists at the project root and 2) it
#' contains a block starting with the line `<!-- badges: start -->` and ending
#' with the line `<!-- badges: end -->`.
#'
#' Don't forget to re-render the `README.md`.
#'
#' @param organisation A character of length 1. The name of the GitHub
#' organisation to host the package. If `NULL` (default) the GitHub account
#' will be used.
#'
#' @param quiet A logical value. If `TRUE` messages are deleted. Default is
#' `FALSE`.
#'
#' @return A badge as a markdown expression.
#'
#' @export
#'
#' @family adding badges
#'
#' @examples
#' \dontrun{
#' add_github_actions_codecov_badge()
#' }
add_github_actions_codecov_badge <- function(organisation = NULL,
quiet = FALSE) {
stop_if_not_logical(quiet)
## Check if GH Actions are set ----
if (!file.exists(file.path(path_proj(), ".github", "workflows",
"test-coverage.yaml"))) {
stop("Please run `add_github_actions_codecov()` to setup GitHub Actions.")
}
## Retrieve GitHub pseudo/organization ----
if (!is.null(organisation)) {
github <- organisation
} else {
github <- gh::gh_whoami()$"login"
if (is.null(github)) {
stop("Unable to find GitHub username. Please run ",
"`?gert::git_config_global` for more information.")
}
}
stop_if_not_string(github)
## Check URL ----
is_gh_user()
project_name <- get_package_name()
## Create Badge Markdown Expression ----
alt <- "Test coverage"
href <- paste("https://github.com", github, project_name,
"actions/workflows/test-coverage.yaml", sep = "/")
img <- paste("https://github.com", github, project_name,
"actions/workflows/test-coverage.yaml/badge.svg", sep = "/")
badge <- paste0("[![", alt, "](", img, ")](", href, ")")
## Add Badge ----
add_badge(badge, pattern = alt)
if (!quiet) {
ui_done(paste0("Adding {ui_field('Test coverage')} badge to ",
"{ui_value('README.Rmd')}"))
}
invisible(badge)
}