gitlabr
gitlabr copied to clipboard
gitlab-ci: Add badges in the README when running use_gitlab_ci()
I'd like use_gitlb_ci() to add appropriate badges in the README.
Badges look like those:
# _Badges
usethis::use_badge(badge_name = "pipeline status",
href = "https://<url>/<group>/<project>/-/commits/master",
src = "https://<url>/<group>/<project>/badges/master/pipeline.svg")
usethis::use_badge(badge_name = "coverage report",
href = "http://<group>.<pages-url>/<project>/coverage.html",
src = "https://<url>/<group>/<project>/badges/master/coverage.svg")
Note that pages-url is different than url
Set default to gitlab.com
Why we won't fix it in {gitlabr}
- Badges depend on the type of project: R-package, bookdown, golem do not need the same badges, in particular for the coverage
- Automatically detecting the project url, group, project, require one of the two solutions:
- (1) either, to connect to GitLab with
set_gitlab_connection(). However, when we useuse_gitlab_ci(), we only want to add a CI yml file, and this should not be related to any distant project. At least, we should be able to start a project before synchronizing it to a GitLab instance - (2) or, to get information from the current git repository. This requires unit tests that clone the GitLab project locally, hence using extra dependencies like {gert} to get information locally.
- (1) either, to connect to GitLab with
- To properly add the badges, {usethis} is the most appropriate way, as the communication with the users is clean and the verifications of the Readme is included. This would add to {gitlabr} a package dedicated to development as dependency: {gitlabr} is to communicate with the GitLab API, not to develop R packages.
Hence, this functionality should be separated in a dedicated package, one that also test "gitlab-ci.yml" files according to the type of project created.
As a start, this could be added in {lozen}, before it to be separated.
From @ALanguillaume
use_gitlab_badge <- function(
badge_name,
href_slug,
svg_name
) {
git_remote_url <- sub("\\.git", "", usethis::git_remotes()$origin)
usethis::use_badge(
badge_name = badge_name,
href = sprintf(
"%s/%s",
git_remote_url,
href_slug
),
src = sprintf(
"%s/badges/%s/%s",
git_remote_url,
# if this function end up in another package than {gitlabr}
# use usethis::git_default_branch() instead
get_main(),
svg_name
)
)
}
use_gitlab_badge_build <- function() {
use_gitlab_badge(
badge_name = "R build status",
href_slug = "-/pipelines",
svg_name = "pipeline.svg"
)
}
use_gitlab_badge_coverage <- function() {
use_gitlab_badge(
badge_name = "Codecov test coverage",
href_slug = sprintf(
"commits/%s",
# if this function end up in another package than {gitlabr}
# use usethis::git_default_branch() instead
get_main()
),
svg_name = "coverage.svg"
)
}