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

Add parameter to control error when wrong repos/orgs are passed. #566

Merged
merged 1 commit into from
Jan 2, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GitStats
Title: Standardized Git Repository Data
Version: 2.1.2.9004
Version: 2.1.2.9005
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- changing name of the `time_interval` parameter to `time_aggregation`,
- adding `yearly` aggregation to `time_aggregation` parameter,
- changing basic input from `GitStats` to `commits_data` object which allows to build workflow in one pipeline (`create_gitstats() |> set_*_host() |> get_commits() |> get_commits_stats()`).
- Add `.show_error` parameter to the `set_*_host()` functins to control if error should pop up when wrong input is passed ([#547](https://github.com/r-world-devs/GitStats/issues/547)).

## Fixes:

Expand Down
56 changes: 38 additions & 18 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ GitHost <- R6::R6Class(
repos = NA,
token = NA,
host = NA,
verbose = NA) {
verbose = NA,
.error = TRUE) {
private$set_api_url(host)
private$set_web_url(host)
private$set_endpoints()
Expand All @@ -36,7 +37,8 @@ GitHost <- R6::R6Class(
private$set_orgs_and_repos(
orgs = orgs,
repos = repos,
verbose = verbose
verbose = verbose,
.error = .error
)
},

Expand Down Expand Up @@ -413,18 +415,20 @@ GitHost <- R6::R6Class(
},

# Set organization or repositories
set_orgs_and_repos = function(orgs, repos, verbose) {
set_orgs_and_repos = function(orgs, repos, verbose, .error) {
if (!private$scan_all) {
if (!is.null(orgs)) {
private$orgs <- private$check_organizations(
orgs = orgs,
verbose = verbose
verbose = verbose,
.error = .error
)
}
if (!is.null(repos)) {
repos <- private$check_repositories(
repos = repos,
verbose = verbose
verbose = verbose,
.error = .error
)
private$repos_fullnames <- repos
orgs_repos <- private$extract_repos_and_orgs(private$repos_fullnames)
Expand All @@ -435,15 +439,17 @@ GitHost <- R6::R6Class(
},

# Check if repositories exist
check_repositories = function(repos, verbose) {
check_repositories = function(repos, verbose, .error) {
if (verbose) {
cli::cli_alert_info(cli::col_grey("Checking repositories..."))
}
repos <- purrr::map(repos, function(repo) {
repo_endpoint <- glue::glue("{private$endpoints$repositories}/{repo}")
check <- private$check_endpoint(
endpoint = repo_endpoint,
type = "Repository"
type = "Repository",
verbose = verbose,
.error = .error
)
if (!check) {
repo <- NULL
Expand All @@ -459,15 +465,17 @@ GitHost <- R6::R6Class(
},

# Check if organizations exist
check_organizations = function(orgs, verbose) {
check_organizations = function(orgs, verbose, .error) {
if (verbose) {
cli::cli_alert_info(cli::col_grey("Checking organizations..."))
}
orgs <- purrr::map(orgs, function(org) {
org_endpoint <- glue::glue("{private$endpoints$orgs}/{org}")
check <- private$check_endpoint(
endpoint = org_endpoint,
type = "Organization"
type = "Organization",
verbose = verbose,
.error = .error
)
if (!check) {
org <- NULL
Expand All @@ -483,25 +491,37 @@ GitHost <- R6::R6Class(
},

# Check whether the endpoint exists.
check_endpoint = function(endpoint, type) {
check_endpoint = function(endpoint, type, verbose, .error) {
check <- TRUE
tryCatch(
{
private$engines$rest$response(endpoint = endpoint)
},
error = function(e) {
if (grepl("404", e)) {
cli::cli_abort(
c(
"x" = "{type} you provided does not exist or its name was passed
if (.error) {
cli::cli_abort(
c(
"x" = "{type} you provided does not exist or its name was passed
in a wrong way: {cli::col_red({utils::URLdecode(endpoint)})}",
"!" = "Please type your {tolower(type)} name as you see it in
"!" = "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
"i" = "E.g. do not use spaces. {type} names as you see on the
page may differ from their web 'address'."
),
call = NULL
)
),
call = NULL
)
} else {
if (verbose) {
cli::cli_alert_warning(
cli::col_yellow(
"{type} you provided does not exist: {cli::col_red({utils::URLdecode(endpoint)})}"
)
)
}
check <<- FALSE
}

}
}
)
Expand Down
6 changes: 4 additions & 2 deletions R/GitHostGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ GitHostGitHub <- R6::R6Class(
repos = NA,
token = NA,
host = NA,
verbose = NA) {
verbose = NA,
.error = TRUE) {
super$initialize(orgs = orgs,
repos = repos,
token = token,
host = host,
verbose = verbose)
verbose = verbose,
.error = .error)
if (verbose) {
cli::cli_alert_success("Set connection to GitHub.")
}
Expand Down
6 changes: 4 additions & 2 deletions R/GitHostGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
repos = NA,
token = NA,
host = NA,
verbose = NA) {
verbose = NA,
.error = TRUE) {
repos <- if (!is.null(repos)) {
url_encode(repos)
}
Expand All @@ -17,7 +18,8 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
repos = repos,
token = token,
host = host,
verbose = verbose)
verbose = verbose,
.error = .error)
if (verbose) {
cli::cli_alert_success("Set connection to GitLab.")
}
Expand Down
12 changes: 8 additions & 4 deletions R/GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ GitStats <- R6::R6Class(
token = NULL,
orgs = NULL,
repos = NULL,
verbose = TRUE) {
verbose = TRUE,
.show_error = TRUE) {
new_host <- NULL
new_host <- GitHostGitHub$new(
orgs = orgs,
repos = repos,
token = token,
host = host,
verbose = verbose
verbose = verbose,
.error = .show_error
)
private$add_new_host(new_host)
},
Expand All @@ -24,14 +26,16 @@ GitStats <- R6::R6Class(
token = NULL,
orgs = NULL,
repos = NULL,
verbose = TRUE) {
verbose = TRUE,
.show_error = TRUE) {
new_host <- NULL
new_host <- GitHostGitLab$new(
orgs = orgs,
repos = repos,
token = token,
host = host,
verbose = verbose
verbose = verbose,
.error = .show_error
)
private$add_new_host(new_host)
},
Expand Down
79 changes: 0 additions & 79 deletions R/gitstats_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,6 @@ create_gitstats <- function() {
GitStats$new()
}

#' @title Set GitHub host
#' @name set_github_host
#' @param gitstats A GitStats object.
#' @param host A character, optional, URL name of the host. If not passed, a
#' public host will be used.
#' @param token A token.
#' @param orgs An optional character vector of organisations. If you pass it,
#' `repos` parameter should stay `NULL`.
#' @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
#' need to define `orgs` or `repos` as scanning through all organizations may
#' take large amount of time.
#' @return A `GitStats` object with added information on host.
#' @examples
#' \dontrun{
#' my_gitstats <- create_gitstats() %>%
#' set_github_host(
#' orgs = c("r-world-devs", "openpharma", "pharmaverse")
#' )
#' }
#' @export
set_github_host <- function(gitstats,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL,
verbose = is_verbose(gitstats)) {
gitstats$set_github_host(
host = host,
token = token,
orgs = orgs,
repos = repos,
verbose = verbose
)

return(invisible(gitstats))
}

#' @title Set GitLab host
#' @name set_gitlab_host
#' @inheritParams set_github_host
#' @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
#' need to define `orgs` or `repos` as scanning through all organizations may
#' take large amount of time.
#' @return A `GitStats` object with added information on host.
#' @examples
#' \dontrun{
#' my_gitstats <- create_gitstats() %>%
#' set_gitlab_host(
#' token = Sys.getenv("GITLAB_PAT_PUBLIC"),
#' orgs = "mbtests"
#' )
#' }
#' @export
set_gitlab_host <- function(gitstats,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL,
verbose = is_verbose(gitstats)) {
gitstats$set_gitlab_host(
host = host,
token = token,
orgs = orgs,
repos = repos,
verbose = verbose
)

return(invisible(gitstats))
}

#' @title Get data on repositories
#' @name get_repos
#' @description Pulls data on all repositories for an organization, individual
Expand Down
84 changes: 84 additions & 0 deletions R/set_host.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#' @title Set GitHub host
#' @name set_github_host
#' @param gitstats A GitStats object.
#' @param host A character, optional, URL name of the host. If not passed, a
#' public host will be used.
#' @param token A token.
#' @param orgs An optional character vector of organisations. If you pass it,
#' `repos` parameter should stay `NULL`.
#' @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.
#' @param .show_error A logical to control if passing wrong input
#' (`repositories` and `organizations`) should end with an error or not.
#' @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
#' need to define `orgs` or `repos` as scanning through all organizations may
#' take large amount of time.
#' @return A `GitStats` object with added information on host.
#' @examples
#' \dontrun{
#' my_gitstats <- create_gitstats() %>%
#' set_github_host(
#' orgs = c("r-world-devs", "openpharma", "pharmaverse")
#' )
#' }
#' @export
set_github_host <- function(gitstats,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL,
verbose = is_verbose(gitstats),
.show_error = TRUE) {
gitstats$set_github_host(
host = host,
token = token,
orgs = orgs,
repos = repos,
verbose = verbose,
.show_error = .show_error
)

return(invisible(gitstats))
}

#' @title Set GitLab host
#' @name set_gitlab_host
#' @inheritParams set_github_host
#' @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
#' need to define `orgs` or `repos` as scanning through all organizations may
#' take large amount of time.
#' @return A `GitStats` object with added information on host.
#' @examples
#' \dontrun{
#' my_gitstats <- create_gitstats() %>%
#' set_gitlab_host(
#' token = Sys.getenv("GITLAB_PAT_PUBLIC"),
#' orgs = "mbtests"
#' )
#' }
#' @export
set_gitlab_host <- function(gitstats,
host = NULL,
token = NULL,
orgs = NULL,
repos = NULL,
verbose = is_verbose(gitstats),
.show_error = TRUE) {
gitstats$set_gitlab_host(
host = host,
token = token,
orgs = orgs,
repos = repos,
verbose = verbose,
.show_error = .show_error
)

return(invisible(gitstats))
}
Loading
Loading