Skip to content

Commit

Permalink
Add possibility to pass host with http.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekbanas committed Oct 18, 2024
1 parent ae8c8da commit d8e73b9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Expand Down
1 change: 1 addition & 0 deletions R/GitHostGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-GitHost-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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")
})

0 comments on commit d8e73b9

Please sign in to comment.