Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export is_github_url() #211

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(get_version_config)
export(get_version_file)
export(get_version_hub)
export(is_github_repo_url)
export(is_github_url)
export(is_s3_base_fs)
export(is_url)
export(is_v3_config)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added utilities for working with URLs:
- `is_url()`: checks whether a character string is a URL.
- `is_valid_url()`: checks whether a URL is valid and reachable.
- `is_github_url()`: checks whether a URL is a `github.com` URL.
- `is_github_repo_url()`: checks whether a URL is a GitHub repository URL.
- `create_s3_url()`: creates an S3 URL from a bucket name and object path.
- `is_s3_base_fs()`: checks whether an object of class `<SubTreeFileSystem>` is a base file system (i.e. the root of a cloud hub).
Expand Down
20 changes: 19 additions & 1 deletion R/utils-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,25 @@ convert_to_raw_github_url <- function(repo_url) {
sanitize_url()
}

# Detect a github URL

#' Detect a URL on github.com
#'
#' @param url character string of the URL to check.
#'
#' @returns Logical. `TRUE` if the URL on `github.com`, `FALSE` otherwise.
#' @export
#'
#' @examples
#' # Returns TRUE
#' is_github_url("https://github.com/hubverse-org/example-simple-forecast-hub")
#' is_github_url("https://github.com/hubverse-org/schemas/tree/main/v5.0.0")
#' # Returns FALSE
#' is_github_url("https://gitlab.com/hubverse-org/schemas/tree/main/v5.0.0")
#' raw_url <- paste0(
#' "https://raw.githubusercontent.com/hubverse-org/",
#' "example-simple-forecast-hub/refs/heads/main/hub-config/tasks.json"
#' )
#' is_github_url(raw_url)
is_github_url <- function(url) {
grepl("^https?://(www\\.)?github\\.com/[^/]+/[^/]+", url)
}
Expand Down
29 changes: 29 additions & 0 deletions man/is_github_url.Rd

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

9 changes: 8 additions & 1 deletion tests/testthat/test-utils-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ test_that("create_s3_url works", {

test_that("GitHub utils work", {
hub_url <- "https://github.com/hubverse-org/example-simple-forecast-hub"

expect_true(is_github_url(hub_url))
file_url <- "https://github.com/hubverse-org/schemas/tree/main/v5.0.0"
expect_true(is_github_url(hub_url))

expect_false(is_github_url("https://hubverse.io"))
raw_url <- paste0(
"https://raw.githubusercontent.com/hubverse-org/",
"example-simple-forecast-hub/refs/heads/main/hub-config/tasks.json"
)
expect_false(is_github_url(raw_url))

expect_equal(
convert_to_raw_github_url(hub_url),
Expand Down
Loading