From d8e73b915c50d3eec7b9df10398f7dae43453ab3 Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Fri, 18 Oct 2024 10:54:05 +0000 Subject: [PATCH] Add possibility to pass host with `http`. --- DESCRIPTION | 2 +- NEWS.md | 1 + R/GitHost.R | 5 ++++- R/GitHostGitHub.R | 1 + tests/testthat/test-GitHost-helpers.R | 20 +++++++++++++++++++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e284f54..116fcf8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GitStats Title: Get Statistics from GitHub and GitLab -Version: 2.1.0.9006 +Version: 2.1.0.9007 Authors@R: c( person(given = "Maciej", family = "Banas", email = "banasmaciek@gmail.com", role = c("aut", "cre")), person(given = "Kamil", family = "Koziej", email = "koziej.k@gmail.com", role = "aut"), diff --git a/NEWS.md b/NEWS.md index bfb2c44..1392b25 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,7 @@ - Fixed getting large search responses for GitHub ([#491](https://github.com/r-world-devs/GitStats/issues/491)). - Fixed checking token scopes ([#501](https://github.com/r-world-devs/GitStats/issues/501)). If token scopes are insufficient error is returned and `GitHost` is not passed to `GitStats`. This also applies to situation when `GitStats` looks for default tokens (not defined by user). Earlier, if tests for token failed, an empty token was passed and `GitStats` was created, which was misleading for the user. - User can now optionally pass public GitHub host name (`github.com` or `https://github.com`) to `set_github_host()` ([#475](https://github.com/r-world-devs/GitStats/issues/475)). +- It is possible to pass hosts in more flexible way than before (e.g. `{host_url}`, `http://{host_url}` or `https://{host_url}`) to `host` parameter in `set_*_host() function ([#399](https://github.com/r-world-devs/GitStats/issues/399)). # GitStats 2.1.0 diff --git a/R/GitHost.R b/R/GitHost.R index c4fba30..3e32508 100644 --- a/R/GitHost.R +++ b/R/GitHost.R @@ -297,11 +297,14 @@ GitHost <- R6::R6Class( # Set API url set_custom_api_url = function(host) { - private$api_url <- if (!grepl("https://", host)) { + private$api_url <- if (!grepl("https|http", host)) { glue::glue( "https://{host}/api/v{private$api_version}" ) } else { + if (grepl("http(?!s)", host, perl = TRUE)) { + host <- gsub("http", "https", host) + } glue::glue( "{host}/api/v{private$api_version}" ) diff --git a/R/GitHostGitHub.R b/R/GitHostGitHub.R index 24cdc67..f4e570d 100644 --- a/R/GitHostGitHub.R +++ b/R/GitHostGitHub.R @@ -56,6 +56,7 @@ GitHostGitHub <- R6::R6Class( set_api_url = function(host) { if (is.null(host) || host == "https://github.com" || + host == "http://github.com" || host == "github.com") { private$api_url <- "https://api.github.com" } else { diff --git a/tests/testthat/test-GitHost-helpers.R b/tests/testthat/test-GitHost-helpers.R index d42e58f..fbadb26 100644 --- a/tests/testthat/test-GitHost-helpers.R +++ b/tests/testthat/test-GitHost-helpers.R @@ -22,7 +22,7 @@ test_that("set_owner_types sets attributes to owners list", { expect_equal(owner[[1]], "test_org", ignore_attr = TRUE) }) -test_that("set_api_url works for public hosts", { +test_that("set_api_url works", { expect_equal({ github_testhost_priv$set_api_url( host = "github.com" @@ -33,6 +33,11 @@ test_that("set_api_url works for public hosts", { host = "https://github.com" ) }, "https://api.github.com") + expect_equal({ + github_testhost_priv$set_api_url( + host = "http://github.com" + ) + }, "https://api.github.com") expect_equal({ github_testhost_priv$set_api_url( host = "https://github.company.com" @@ -49,3 +54,16 @@ test_that("set_api_url works for public hosts", { ) }, "https://gitlab.com/api/v4") }) + +test_that("set_custom_api_url works", { + expect_equal({ + gitlab_testhost_priv$set_custom_api_url( + host = "http://gitlab.com" + ) + }, "https://gitlab.com/api/v4") + expect_equal({ + github_testhost_priv$set_custom_api_url( + host = "http://github.company.com" + ) + }, "https://github.company.com/api/v3") +})