Skip to content

Commit

Permalink
Support Codeberg repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jan 23, 2025
1 parent 6615322 commit 9f419fd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
#' of your source repository. This is used in the navbar, on the homepage,
#' in articles and reference topics, and in the changelog (to link to issue
#' numbers and user names). pkgdown can automatically figure out the necessary
#' URLs if you link to a GitHub or GitLab repo in your `BugReports` or `URL`
#' field.
#' URLs if you link to a GitHub, GitLab or Codeberg repo in your `BugReports`
#' or `URL` field.
#'
#' Otherwise, you can supply your own in the `repo` field:
#'
Expand Down
1 change: 1 addition & 0 deletions R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ navbar_components <- function(pkg = ".") {
repo_type(pkg),
github = menu_icon("fab fa-github fa-lg", repo_home(pkg), "GitHub"),
gitlab = menu_icon("fab fa-gitlab fa-lg", repo_home(pkg), "GitLab"),
codeberg = menu_icon("fas fa-code fa-lg", repo_home(pkg), "Codeberg"),
NULL
)

Expand Down
7 changes: 5 additions & 2 deletions R/repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ repo_type <- function(pkg) {
"github"
} else if (grepl("^https?://gitlab\\..+/", home)) {
"gitlab"
} else if (grepl("^https?://codeberg\\..+/", home)) {
"codeberg"
} else {
"other"
}
Expand Down Expand Up @@ -75,7 +77,7 @@ package_repo <- function(pkg) {
pkg$desc$get_urls()
)

gh_links <- grep("^https?://git(hub|lab)\\..+/", urls, value = TRUE)
gh_links <- grep("^https?://(git(hub|lab)|codeberg)\\..+/", urls, value = TRUE)
if (length(gh_links) > 0) {
branch <- config_pluck_string(pkg, "repo.branch")
return(repo_meta_gh_like(gh_links[[1]], branch))
Expand All @@ -98,10 +100,11 @@ repo_meta <- function(home = NULL, source = NULL, issue = NULL, user = NULL) {
repo_meta_gh_like <- function(link, branch = NULL) {
gh <- parse_github_like_url(link)
branch <- branch %||% gha_current_branch()
blob <- if (grepl("^https?://codeberg\\.", link)) "/src/branch/" else "/blob/"

repo_meta(
paste0(gh$host, "/", gh$owner, "/", gh$repo, "/"),
paste0(gh$host, "/", gh$owner, "/", gh$repo, "/blob/", branch, "/"),
paste0(gh$host, "/", gh$owner, "/", gh$repo, blob, branch, "/"),
paste0(gh$host, "/", gh$owner, "/", gh$repo, "/issues/"),
paste0(gh$host, "/")
)
Expand Down
4 changes: 2 additions & 2 deletions man/build_site.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion tests/testthat/_snaps/navbar.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# adds github/gitlab link when available
# adds github/gitlab/codeberg link when available

reference:
text: Reference
Expand Down Expand Up @@ -33,6 +33,19 @@
aria-label: GitLab


---

reference:
text: Reference
href: reference/index.html
search:
search: []
github:
icon: fas fa-code fa-lg
href: https://codeberg.org/r-lib/pkgdown
aria-label: Codeberg


# vignette with package name turns into getting started

reference:
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-navbar.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("adds github/gitlab link when available", {
test_that("adds github/gitlab/codeberg link when available", {
pkg <- pkg_navbar()
expect_snapshot_output(navbar_components(pkg))

Expand All @@ -7,6 +7,9 @@ test_that("adds github/gitlab link when available", {

pkg <- pkg_navbar(github_url = "https://gitlab.com/r-lib/pkgdown")
expect_snapshot_output(navbar_components(pkg))

pkg <- pkg_navbar(github_url = "https://codeberg.org/r-lib/pkgdown")
expect_snapshot_output(navbar_components(pkg))
})

test_that("vignette with package name turns into getting started", {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ test_that("can find gitlab url", {
expect_equal(package_repo(pkg), repo_meta_gh_like(url))
})

test_that("can find codeberg url", {
withr::local_envvar(GITHUB_HEAD_REF = "HEAD")
url <- "https://codeberg.org/msberends/AMR"
pkg <- local_pkgdown_site(desc = list(URL = url))
expect_equal(package_repo(pkg), repo_meta_gh_like(url))
})

test_that("uses GITHUB env vars if set", {
withr::local_envvar(GITHUB_HEAD_REF = NA, GITHUB_REF_NAME = "abc")
expect_equal(
Expand Down Expand Up @@ -172,5 +179,6 @@ test_that("repo_type detects repo type", {
expect_equal(repo_type2("https://github.r-lib.com/pkgdown"), "github")
expect_equal(repo_type2("https://gitlab.com/r-lib/pkgdown"), "gitlab")
expect_equal(repo_type2("https://gitlab.r-lib.com/pkgdown"), "gitlab")
expect_equal(repo_type2("https://codeberg.org/r-lib/pkgdown"), "codeberg")
expect_equal(repo_type2(NULL), "other")
})

0 comments on commit 9f419fd

Please sign in to comment.