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

413 399 415 improve set hosts #416

Merged
merged 4 commits into from
May 9, 2024
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
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.0.0.9000
Version: 2.0.0.9001
Authors@R: c(
person(given = "Maciej", family = "Banas", email = "[email protected]", role = c("aut", "cre")),
person(given = "Kamil", family = "Koziej", email = "[email protected]", role = "aut"),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

- Getting files feature has been speeded up with switching to `Search API` instead of pulling files via `GraphQL` via iteration over organizations and repositories ([#411](https://github.com/r-world-devs/GitStats/issues/411)).
- When setting hosts to be scanned in whole (without specifying `orgs` or `repos`) GitStats does not pull no more all organizations. Pulling all organizations from host is triggered only when user decides to pull repositories from organizations. If he decides, e.g. to pull repositories by code, there is no need to pull all organizations (which may be a time consuming process), as GitStats uses then `Search API` ([#393](https://github.com/r-world-devs/GitStats/issues/393)).
- It is now possible to mute messages also from `set_*_host()` functions with `verbose_off()` or `verbose` parameter ([#413](https://github.com/r-world-devs/GitStats/issues/413)).

## Fixes:

- Pulling repositories from GitLab subgroups was fixed. It did not work, as the URL of a group (org) was passed to GraphQL API the same way as to REST API, i.e. with URL sign ("%2F", instead of "/").
- GitStats returns now proper error, when you pass wrong host URL to `set_*_host()` function ([#415](https://github.com/r-world-devs/GitStats/issues/415))

# GitStats 2.0.0

Expand Down
60 changes: 45 additions & 15 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ GitHost <- R6::R6Class("GitHost",
#' @param repos A character vector of repositories.
#' @param token A token.
#' @param host A host.
#' @param verbose A logical, `TRUE` by default. If `FALSE` messages and printing
#' output is switched off.
#' @return A new `GitHost` object.
initialize = function(orgs = NA,
repos = NA,
token = NA,
host = NA) {
host = NA,
verbose = NA) {
private$set_verbose(verbose)
private$set_api_url(host)
private$set_endpoints()
private$check_if_public(host)
Expand Down Expand Up @@ -112,6 +116,7 @@ GitHost <- R6::R6Class("GitHost",
}
until <- until %||% Sys.time()
release_logs_table <- purrr::map(private$orgs, function(org) {
org <- utils::URLdecode(org)
release_logs_table_org <- NULL
if (!private$scan_all && verbose) {
show_message(
Expand Down Expand Up @@ -258,20 +263,28 @@ GitHost <- R6::R6Class("GitHost",
),
call = NULL)
} else {
cli::cli_alert_warning(cli::col_yellow(
"No `orgs` specified."
))
cli::cli_alert_info(cli::col_grey("Searching scope set to [all]."))
if (private$verbose) {
cli::cli_alert_warning(cli::col_yellow(
"No `orgs` specified."
))
cli::cli_alert_info(cli::col_grey(
"Searching scope set to [all]."
))
}
private$searching_scope <- "all"
private$scan_all <- TRUE
}
}
if (!is.null(repos) && is.null(orgs)) {
cli::cli_alert_info(cli::col_grey("Searching scope set to [repo]."))
if (private$verbose) {
cli::cli_alert_info(cli::col_grey("Searching scope set to [repo]."))
}
private$searching_scope <- "repo"
}
if (is.null(repos) && !is.null(orgs)) {
cli::cli_alert_info(cli::col_grey("Searching scope set to [org]."))
if (private$verbose) {
cli::cli_alert_info(cli::col_grey("Searching scope set to [org]."))
}
private$searching_scope <- "org"
}
if (!is.null(repos) && !is.null(orgs)) {
Expand Down Expand Up @@ -303,7 +316,9 @@ GitHost <- R6::R6Class("GitHost",

# Check if repositories exist
check_repositories = function(repos) {
cli::cli_alert_info(cli::col_grey("Checking passed repositories..."))
if (private$verbose) {
cli::cli_alert_info(cli::col_grey("Checking host data..."))
}
repos <- purrr::map(repos, function(repo) {
repo_endpoint = glue::glue("{private$endpoints$repositories}/{repo}")
check <- private$check_endpoint(
Expand All @@ -325,7 +340,9 @@ GitHost <- R6::R6Class("GitHost",

# Check if organizations exist
check_organizations = function(orgs) {
cli::cli_alert_info(cli::col_grey("Checking passed organizations..."))
if (private$verbose) {
cli::cli_alert_info(cli::col_grey("Checking host data..."))
}
orgs <- purrr::map(orgs, function(org) {
org_endpoint = glue::glue("{private$endpoints$orgs}/{org}")
check <- private$check_endpoint(
Expand Down Expand Up @@ -353,12 +370,22 @@ GitHost <- R6::R6Class("GitHost",
private$engines$rest$response(endpoint = endpoint)
},
error = function(e) {
if (!is.null(e$parent$message) && grepl("Could not resolve host", e$parent$message)) {
cli::cli_abort(
cli::col_red(e$parent$message),
call = NULL
)
}
if (grepl("404", e)) {
cli::cli_alert_danger("{type} you provided does not exist or its name was passed in a wrong way: {endpoint}")
cli::cli_alert_warning("Please type your {tolower(type)} name as you see it in `url`.")
cli::cli_alert_info("E.g. do not use spaces. {type} names as you see on the page may differ from their 'address' name.")
cli::cli_abort(
c(
"x" = "{type} you provided does not exist or its name was passed in a wrong way: {cli::col_red({endpoint})}",
"!" = "Please type your {tolower(type)} name as you see it in web URL.",
"i" = "E.g. do not use spaces. {type} names as you see on the page may differ from their web 'address'."
),
call = NULL
)
}
check <<- FALSE
}
)
return(check)
Expand All @@ -374,15 +401,17 @@ GitHost <- R6::R6Class("GitHost",
set_default_token = function() {
primary_token_name <- private$token_name
token <- Sys.getenv(primary_token_name)
if (private$test_token(token)) {
if (private$test_token(token) && private$verbose) {
cli::cli_alert_info("Using PAT from {primary_token_name} envar.")
} else {
pat_names <- names(Sys.getenv()[grepl(primary_token_name, names(Sys.getenv()))])
possible_tokens <- pat_names[pat_names != primary_token_name]
for (token_name in possible_tokens) {
if (private$test_token(Sys.getenv(token_name))) {
token <- Sys.getenv(token_name)
cli::cli_alert_info("Using PAT from {token_name} envar.")
if (private$verbose) {
cli::cli_alert_info("Using PAT from {token_name} envar.")
}
break
}
}
Expand Down Expand Up @@ -446,6 +475,7 @@ GitHost <- R6::R6Class("GitHost",
pull_all_repos = function(settings, verbose = private$verbose) {
graphql_engine <- private$engines$graphql
repos_table <- purrr::map(private$orgs, function(org) {
org <- utils::URLdecode(org)
if (!private$scan_all && verbose) {
show_message(
host = private$host_name,
Expand Down
11 changes: 7 additions & 4 deletions R/GitHostGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ GitHostGitHub <- R6::R6Class("GitHostGitHub",
initialize = function(orgs = NA,
repos = NA,
token = NA,
host = NA) {
host = NA,
verbose = NA) {
super$initialize(orgs = orgs,
repos = repos,
token = token,
host = host)
cli::cli_alert_success("Set connection to GitHub.")
host = host,
verbose = verbose)
if (private$verbose) {
cli::cli_alert_success("Set connection to GitHub.")
}
}

),
private = list(

Expand Down
12 changes: 8 additions & 4 deletions R/GitHostGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
initialize = function(orgs = NA,
repos = NA,
token = NA,
host = NA) {
host = NA,
verbose = NA) {
repos <- if (!is.null(repos)) {
url_encode(repos)
}
Expand All @@ -15,8 +16,11 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
super$initialize(orgs = orgs,
repos = repos,
token = token,
host = host)
cli::cli_alert_success("Set connection to GitLab.")
host = host,
verbose = verbose)
if (private$verbose) {
cli::cli_alert_success("Set connection to GitLab.")
}
}
),
private = list(
Expand Down Expand Up @@ -195,7 +199,7 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
show_message(
host = private$host_name,
engine = "rest",
scope = org,
scope = utils::URLdecode(org),
information = "Pulling commits"
)
}
Expand Down
16 changes: 12 additions & 4 deletions R/GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ GitStats <- R6::R6Class("GitStats",
#' @param repos An optional character vector of repositories full names
#' (organization and repository name, e.g. "r-world-devs/GitStats"). If
#' you pass it, `orgs` parameter should stay `NULL`.
#' @param verbose A logical, `TRUE` by default. If `FALSE` messages and printing
#' output is switched off.
#' @return Nothing, puts connection information into `$hosts` slot.
set_github_host = function(host,
token = NULL,
orgs = NULL,
repos = NULL) {
repos = NULL,
verbose = TRUE) {
new_host <- NULL
new_host <- GitHostGitHub$new(
orgs = orgs,
repos = repos,
token = token,
host = host
host = host,
verbose = verbose
)
private$add_new_host(new_host)
},
Expand All @@ -38,17 +42,21 @@ GitStats <- R6::R6Class("GitStats",
#' @param repos An optional character vector of repositories full names
#' (organization and repository name, e.g. "r-world-devs/GitStats"). If
#' you pass it, `orgs` parameter should stay `NULL`.
#' @param verbose A logical, `TRUE` by default. If `FALSE` messages and printing
#' output is switched off.
#' @return Nothing, puts connection information into `$hosts` slot.
set_gitlab_host = function(host,
token = NULL,
orgs = NULL,
repos = NULL) {
repos = NULL,
verbose = TRUE) {
new_host <- NULL
new_host <- GitHostGitLab$new(
orgs = orgs,
repos = repos,
token = token,
host = host
host = host,
verbose = verbose
)
private$add_new_host(new_host)
},
Expand Down
14 changes: 10 additions & 4 deletions R/gitstats_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ create_gitstats <- function() {
#' @param repos An optional character vector of repositories full names
#' (organization and repository name, e.g. "r-world-devs/GitStats"). If you
#' pass it, `orgs` parameter should stay `NULL`.
#' @param verbose A logical, `TRUE` by default. If `FALSE` messages and printing
#' output is switched off.
#' @details If you do not define `orgs` and `repos`, `GitStats` will be set to
#' scan whole Git platform (such as enterprise version of GitHub or GitLab),
#' unless it is a public platform. In case of a public one (like GitHub) you
Expand All @@ -37,12 +39,14 @@ set_github_host <- function(gitstats_object,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL) {
repos = NULL,
verbose = is_verbose(gitstats_object)) {
gitstats_object$set_github_host(
host = host,
token = token,
orgs = orgs,
repos = repos
repos = repos,
verbose = verbose
)

return(invisible(gitstats_object))
Expand Down Expand Up @@ -70,12 +74,14 @@ set_gitlab_host <- function(gitstats_object,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL) {
repos = NULL,
verbose = is_verbose(gitstats_object)) {
gitstats_object$set_gitlab_host(
host = host,
token = token,
orgs = orgs,
repos = repos
repos = repos,
verbose = verbose
)

return(invisible(gitstats_object))
Expand Down
20 changes: 12 additions & 8 deletions R/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ GitHostGitHubTest <- R6::R6Class("GitHostGitHubTest",
private$set_api_url(host)
private$set_endpoints()
private$check_if_public(host)
private$set_token(token)
private$set_graphql_url()
private$set_orgs_and_repos(orgs, repos)
private$setup_test_engines()
private$orgs <- orgs
private$repos <- repos
private$set_searching_scope(orgs, repos)
}
),
private = list(
Expand All @@ -46,7 +47,7 @@ GitHostGitHubTest <- R6::R6Class("GitHostGitHubTest",
)
private$engines$graphql <- EngineGraphQLGitHub$new(
token = private$token,
gql_api_url = private$set_gql_url(private$api_url)
gql_api_url = private$set_graphql_url()
)
}
)
Expand All @@ -65,10 +66,11 @@ GitHostGitLabTest <- R6::R6Class("GitHostGitLabTest",
private$set_api_url(host)
private$set_endpoints()
private$check_if_public(host)
private$set_token(token)
private$set_graphql_url()
private$set_orgs_and_repos(orgs, repos)
private$setup_test_engines()
private$orgs <- orgs
private$repos <- repos
private$set_searching_scope(orgs, repos)
}
),
private = list(
Expand All @@ -79,7 +81,7 @@ GitHostGitLabTest <- R6::R6Class("GitHostGitLabTest",
)
private$engines$graphql <- EngineGraphQLGitLab$new(
token = private$token,
gql_api_url = private$set_gql_url(private$api_url)
gql_api_url = private$set_graphql_url()
)
}
)
Expand All @@ -91,7 +93,7 @@ create_github_testhost <- function(host = NULL,
repos = NULL,
mode = "") {
suppressMessages(
test_host <- GitHostGitHub$new(
test_host <- GitHostGitHubTest$new(
host = NULL,
token = Sys.getenv("GITHUB_PAT"),
orgs = orgs,
Expand All @@ -110,7 +112,7 @@ create_gitlab_testhost <- function(host = NULL,
repos = NULL,
mode = "") {
suppressMessages(
test_host <- GitHostGitLab$new(
test_host <- GitHostGitLabTest$new(
host = NULL,
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = orgs,
Expand Down Expand Up @@ -145,6 +147,7 @@ TestEngineRestGitHub <- R6::R6Class("TestEngineRestGitHub",
rest_api_url) {
private$token <- token
self$rest_api_url <- rest_api_url
private$set_endpoints()
}
)
)
Expand All @@ -158,6 +161,7 @@ TestEngineRestGitLab <- R6::R6Class("TestEngineRestGitLab",
rest_api_url) {
private$token <- token
self$rest_api_url <- rest_api_url
private$set_endpoints()
}
)
)
Expand Down
Loading
Loading