diff --git a/DESCRIPTION b/DESCRIPTION index df18ba33..e8bc2fc1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GitStats Title: Standardized Git Repository Data -Version: 2.5.1.9000 +Version: 2.5.1.9001 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 097f1dec..fe9503ab 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # GitStats (development version) +## Improvements + +- `set_github_host()` and `set_gitlab_host()` now print elapsed time after setting up the host ([#798](https://github.com/r-world-devs/GitStats/issues/798)). + ## Bug fixes - Fixed `get_orgs()` failing for GitLab hosts with more than 10,000 groups. Since GitLab 15.7, the `x-total` header is suppressed from REST API responses when the result set exceeds 10,000 records. The org count is now obtained via GraphQL `groups { count }` query instead ([#793](https://github.com/r-world-devs/GitStats/issues/793)). diff --git a/R/set_host.R b/R/set_host.R index bded7c6a..d5fc0e87 100644 --- a/R/set_host.R +++ b/R/set_host.R @@ -32,6 +32,7 @@ set_github_host <- function(gitstats, repos = NULL, verbose = is_verbose(gitstats), .error = TRUE) { + start_time <- Sys.time() gitstats$set_github_host( host = host, token = token, @@ -40,6 +41,11 @@ set_github_host <- function(gitstats, verbose = verbose, .error = .error ) + end_time <- Sys.time() + time_taken <- end_time - start_time + if (verbose) { + cli::cli_alert_info("Set up in {round(time_taken, 1)} {attr(time_taken, 'units')}.") + } return(invisible(gitstats)) } @@ -69,6 +75,7 @@ set_gitlab_host <- function(gitstats, repos = NULL, verbose = is_verbose(gitstats), .error = TRUE) { + start_time <- Sys.time() gitstats$set_gitlab_host( host = host, token = token, @@ -77,6 +84,11 @@ set_gitlab_host <- function(gitstats, verbose = verbose, .error = .error ) + end_time <- Sys.time() + time_taken <- end_time - start_time + if (verbose) { + cli::cli_alert_info("Set up in {round(time_taken, 1)} {attr(time_taken, 'units')}.") + } return(invisible(gitstats)) } diff --git a/tests/testthat/_snaps/set_host.md b/tests/testthat/_snaps/set_host.md index f6009a82..96f7fecd 100644 --- a/tests/testthat/_snaps/set_host.md +++ b/tests/testthat/_snaps/set_host.md @@ -1,70 +1,3 @@ -# Set connection returns appropriate messages - - Code - set_github_host(gitstats = test_gitstats, token = Sys.getenv("GITHUB_PAT"), - orgs = c("openpharma", "r-world-devs")) - Message - > Checking owners... - v Set connection to GitHub. - ---- - - Code - set_gitlab_host(test_gitstats, token = Sys.getenv("GITLAB_PAT_PUBLIC"), orgs = c( - "mbtests")) - Message - > Checking owners... - v Set connection to GitLab. - -# When empty token for GitHub, GitStats pulls default token - - Code - test_gitstats <- set_github_host(create_gitstats(), orgs = c("openpharma", - "r-world-devs")) - Message - i Using PAT from GITHUB_PAT envar. - > Checking owners... - v Set connection to GitHub. - -# When empty token for GitLab, GitStats pulls default token - - Code - withr::with_envvar(new = c(GITLAB_PAT = Sys.getenv("GITLAB_PAT_PUBLIC")), { - test_gitstats <- set_gitlab_host(create_gitstats(), orgs = "mbtests") - }) - Message - i Using PAT from GITLAB_PAT envar. - > Checking owners... - v Set connection to GitLab. - -# Set GitHub host with particular repos vector instead of orgs - - Code - set_github_host(test_gitstats, token = Sys.getenv("GITHUB_PAT"), repos = c( - "r-world-devs/GitStats", "r-world-devs/shinyCohortBuilder", - "openpharma/GithubMetrics", "openpharma/DataFakeR")) - Message - > Checking repositories... - v Set connection to GitHub. - -# Set GitLab host with particular repos vector instead of orgs - - Code - set_gitlab_host(test_gitstats, token = Sys.getenv("GITLAB_PAT_PUBLIC"), repos = c( - "mbtests/gitstatstesting", "mbtests/gitstats-testing-2")) - Message - > Checking repositories... - v Set connection to GitLab. - -# When no organizations or repositories are set information is printed - - Code - set_github_host(test_gitstats, token = Sys.getenv("GITHUB_PAT"), verbose = TRUE) - Message - i No `orgs` nor `repos` specified. - i Searching scope set to [all]. - v Set connection to GitHub. - # Error shows, when wrong input is passed when setting connection and host is not passed x Token exists but does not grant access. @@ -73,17 +6,7 @@ # Error pops out, when two clients of the same url api are passed as input - Code - set_github_host(set_github_host(test_gitstats, token = Sys.getenv("GITHUB_PAT"), - orgs = "pharmaverse"), token = Sys.getenv("GITHUB_PAT"), orgs = "openpharma") - Message - > Checking owners... - v Set connection to GitHub. - > Checking owners... - v Set connection to GitHub. - Condition - Error: - x You can not provide two hosts of the same API urls. + x You can not provide two hosts of the same API urls. # Error pops out when `org` does not exist @@ -127,18 +50,3 @@ x Org/user you provided does not exist or its name was passed in a wrong way: r_world_devs ! Please type your org/user name the way you see it in web URL. -# When wrong orgs and repos are passed they are excluded but host is created - - Code - test_gitstats <- set_github_host(create_gitstats(), orgs = c("openpharma", - "r_world_devs"), repos = c("r-world-devs/GitStats", "r-world-devs/GitMetrics"), - verbose = TRUE, .error = FALSE) - Message - i Using PAT from GITHUB_PAT envar. - > Checking owners... - ! Org/user you provided does not exist: r_world_devs - > Checking repositories... - x HTTP 404 Not Found. - ! Repository you provided does not exist: https://api.github.com/repos/r-world-devs/GitMetrics - v Set connection to GitHub. - diff --git a/tests/testthat/test-set_host.R b/tests/testthat/test-set_host.R index 58d57dd4..b23aa070 100644 --- a/tests/testthat/test-set_host.R +++ b/tests/testthat/test-set_host.R @@ -1,58 +1,53 @@ test_gitstats <- create_gitstats() -test_that("Set connection returns appropriate messages", { +test_that("Set connection works as expected", { if (!integration_tests_skipped) { - expect_snapshot( + test_gitstats |> set_github_host( - gitstats = test_gitstats, token = Sys.getenv("GITHUB_PAT"), orgs = c("openpharma", "r-world-devs") ) + expect_equal( + unlist(test_gitstats$.__enclos_env__$private$hosts[[1]]$.__enclos_env__$private$orgs), + c("openpharma", "r-world-devs") ) - expect_snapshot( - test_gitstats |> set_gitlab_host( + test_gitstats |> + set_gitlab_host( token = Sys.getenv("GITLAB_PAT_PUBLIC"), orgs = c("mbtests") ) + expect_equal( + unlist(test_gitstats$.__enclos_env__$private$hosts[[2]]$.__enclos_env__$private$orgs), + c("mbtests") ) } }) -test_that("When empty token for GitHub, GitStats pulls default token", { - skip_on_cran() +test_that("When empty token for GitLab, GitStats pulls default token", { if (!integration_tests_skipped) { - expect_snapshot( + withr::with_envvar(new = c("GITLAB_PAT" = Sys.getenv("GITLAB_PAT_PUBLIC")), { test_gitstats <- create_gitstats() |> - set_github_host( - orgs = c("openpharma", "r-world-devs") + set_gitlab_host( + orgs = "mbtests", + verbose = FALSE ) - ) - } -}) - -test_that("When empty token for GitLab, GitStats pulls default token", { - if (!integration_tests_skipped) { - expect_snapshot( - withr::with_envvar(new = c("GITLAB_PAT" = Sys.getenv("GITLAB_PAT_PUBLIC")), { - test_gitstats <- create_gitstats() |> - set_gitlab_host( - orgs = "mbtests" - ) - }) - ) + expect_equal( + test_gitstats$.__enclos_env__$private$hosts[[1]]$.__enclos_env__$private$token, + Sys.getenv("GITLAB_PAT_PUBLIC") + ) + }) } }) test_that("Set GitHub host with particular repos vector instead of orgs", { if (!integration_tests_skipped) { test_gitstats <- create_gitstats() - expect_snapshot( - test_gitstats |> - set_github_host( - token = Sys.getenv("GITHUB_PAT"), - repos = c("r-world-devs/GitStats", "r-world-devs/shinyCohortBuilder", "openpharma/GithubMetrics", "openpharma/DataFakeR") - ) - ) + test_gitstats |> + set_github_host( + token = Sys.getenv("GITHUB_PAT"), + repos = c("r-world-devs/GitStats", "r-world-devs/shinyCohortBuilder", "openpharma/GithubMetrics", "openpharma/DataFakeR"), + verbose = FALSE + ) expect_length( test_gitstats$.__enclos_env__$private$hosts, 1 @@ -63,13 +58,12 @@ test_that("Set GitHub host with particular repos vector instead of orgs", { test_that("Set GitLab host with particular repos vector instead of orgs", { if (!integration_tests_skipped) { test_gitstats <- create_gitstats() - expect_snapshot( - test_gitstats |> - set_gitlab_host( - token = Sys.getenv("GITLAB_PAT_PUBLIC"), - repos = c("mbtests/gitstatstesting", "mbtests/gitstats-testing-2") - ) - ) + test_gitstats |> + set_gitlab_host( + token = Sys.getenv("GITLAB_PAT_PUBLIC"), + repos = c("mbtests/gitstatstesting", "mbtests/gitstats-testing-2"), + verbose = FALSE + ) expect_length( test_gitstats$.__enclos_env__$private$hosts, 1 @@ -77,18 +71,6 @@ test_that("Set GitLab host with particular repos vector instead of orgs", { } }) -test_that("When no organizations or repositories are set information is printed", { - skip_on_cran() - test_gitstats <- create_gitstats() - expect_snapshot( - test_gitstats |> - set_github_host( - token = Sys.getenv("GITHUB_PAT"), - verbose = TRUE - ) - ) -}) - test_that("Error shows, when wrong input is passed when setting connection and host is not passed", { if (!integration_tests_skipped) { test_gitstats <- create_gitstats() @@ -114,8 +96,7 @@ test_that("Error shows, when wrong input is passed when setting connection and h test_that("Error pops out, when two clients of the same url api are passed as input", { if (!integration_tests_skipped) { test_gitstats <- create_gitstats() - expect_snapshot( - error = TRUE, + expect_snapshot_error( test_gitstats |> set_github_host( token = Sys.getenv("GITHUB_PAT"), @@ -165,14 +146,25 @@ test_that("Error pops out when `org` does not exist", { test_that("When wrong orgs and repos are passed they are excluded but host is created", { if (!integration_tests_skipped) { - expect_snapshot( - test_gitstats <- create_gitstats() |> - set_github_host( - orgs = c("openpharma", "r_world_devs"), - repos = c("r-world-devs/GitStats", "r-world-devs/GitMetrics"), - verbose = TRUE, - .error = FALSE - ) + test_gitstats <- create_gitstats() |> + set_github_host( + orgs = c("openpharma", "r_world_devs"), + repos = c("r-world-devs/GitStats", "r-world-devs/GitMetrics"), + verbose = TRUE, + .error = FALSE + ) + + expect_length( + test_gitstats$.__enclos_env__$private$hosts, + 1 + ) + expect_equal( + unlist(test_gitstats$.__enclos_env__$private$hosts[[1]]$.__enclos_env__$private$orgs), + "openpharma" + ) + expect_equal( + unlist(test_gitstats$.__enclos_env__$private$hosts[[1]]$.__enclos_env__$private$repos), + "GitStats" ) } })