Skip to content

Commit

Permalink
Merge pull request #382 from r-world-devs/378-manage-properly-storage…
Browse files Browse the repository at this point in the history
…-feature

Managing properly storage and improving verbose features
  • Loading branch information
maciekbanas authored Mar 22, 2024
2 parents ca815ce + 7f0049d commit 349c9c8
Show file tree
Hide file tree
Showing 37 changed files with 628 additions and 308 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: 1.1.0.9003
Version: 1.1.0.9004
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export(set_params)
export(set_team_member)
export(show_data)
export(show_orgs)
export(verbose_off)
export(verbose_on)
importFrom(R6,R6Class)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_danger)
Expand Down
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

- GitStats workflow is now simplified. To pull data on `repositories`, `commits`, `R_package_usage` you should use directly corresponding `get_*()` functions instead of `pull_*()` which are deprecated. These `get_*()` functions pull data from API, parse it into table, add some goodies (additional columns) if needed and return table instead of `GitStats` object, which in our opinion is more intuitive and user-friendly ([#345]((https://github.com/r-world-devs/GitStats/issues/345))). That means you do not need to run in pipe two or three additional function calls as before, e.g. `pull_repos(gitstats_object)` %>% `get_repos()` %>% get_repos_stats()`, but you just run
`get_repos(gitstats_object)` to get data you need.
- Moreover, if you run for the second time `get_repos()`, GitStats will pull the data from its storage and not from API as for the first time, unless you set parameters for not using storage (`set_params(use_storage = FALSE)`).
- Moreover, if you run for the second time `get_*()` function without changing any parameters, GitStats will pull the data from its storage and not from API as for the first time, unless you change parameters for the function (e.g. starting date with `since` in `get_commits()`) or change settings for not using storage (`set_params(use_storage = FALSE)`).
- Along with that changes `get_repos_stats()` function was deprecated as its role was unclear - unlike `get_commit_stats()` it did not aggregate repositories data into new stats table, but added only some new numeric columns, like number of contributors (`contributors_n`) or last activity in `difftime` format, which is now done within `get_repos()` function.
- Meanwhile `get_commits_stats()` is preserved as it brings additional value with aggregating commits data into new statistical form.
- `pull_repos_contributors()` as a separate function is deprecated. The parameter `add_contributors` is now set by default to `TRUE` in `get_repos()` which seems more reasonable as user gets all the data.
- Plot functions are now `plot_repos()` (takes `repositories` table as an input) and `plot_commits_stats()` (takes `commits stats` as an input) - `gitstats_plot()` is deprecated.
- A new parameter (`verbose`) in settings has been introduced for limiting messages to user when pulling data - you can turn it on/off with `set_params(verbose = TRUE/FALSE)`.
- In `get_commits()` old parameters (`date_from` and `date_until`) were replaced with new more concise (`since` and `until`).
- A new parameter (`verbose`) in settings has been introduced for limiting messages to user when pulling data - you can turn it on/off with `verbose_on()`/`verbose_off()` functions.

## New features:

Expand Down
8 changes: 4 additions & 4 deletions R/EngineGraphQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ EngineGraphQL <- R6::R6Class("EngineGraphQL",
#' @param pulled_repos Optional parameter to pass repository output object.
#' @param settings A list of `GitStats` settings.
#' @return A table.
pull_files = function(org, file_path, pulled_repos = NULL) {
if (!private$scan_all) {
pull_files = function(org, file_path, pulled_repos = NULL, settings) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling {file_path} files...")
}
files_table <- private$pull_file_from_org(
Expand Down Expand Up @@ -85,14 +85,14 @@ EngineGraphQL <- R6::R6Class("EngineGraphQL",
date_until = Sys.date(),
settings,
.storage = NULL) {
if (!private$scan_all) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling releases...")
}
if (is.null(repos)) {
if (is.null(.storage$repositories)) {
repos_table <- self$pull_repos(
org = org,
settings = list(search_param = "org")
settings = settings
)
} else {
repos_table <- .storage$repositories %>%
Expand Down
28 changes: 17 additions & 11 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
settings) {
if (settings$search_param %in% c("org", "team")) {
if (settings$search_param == "org") {
if (!private$scan_all) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling repositories...")
}
repos_table <- private$pull_repos_from_org(
Expand All @@ -63,8 +63,10 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
) %>%
private$prepare_repos_table()
} else {
if (!private$scan_all) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][team:{settings$team_name}] Pulling repositories...")
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][team:{settings$team_name}] Pulling repositories..."
)
}
repos_table <- private$pull_repos_from_team(
team = settings$team
Expand Down Expand Up @@ -112,10 +114,12 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
if (is.null(.storage$repositories)) {
repos_table <- self$pull_repos(
org = org,
settings = list(search_param = "org")
settings = settings
)
} else {
cli::cli_alert_info("Using repositories stored in `GitStats` object.")
if (settings$verbose) {
cli::cli_alert_info("Using repositories stored in `GitStats` object.")
}
repos_table <- .storage$repositories %>%
dplyr::filter(
organization == org
Expand All @@ -128,11 +132,13 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
}
if (settings$search_param %in% c("org", "repo")) {
if (!private$scan_all) {
if (settings$search_param == "org") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling commits...")
}
if (settings$search_param == "repo") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][custom repositories] Pulling commits...")
if (settings$verbose) {
if (settings$search_param == "org") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling commits...")
}
if (settings$search_param == "repo") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][custom repositories] Pulling commits...")
}
}
}
repos_list_with_commits <- private$pull_commits_from_repos(
Expand All @@ -143,7 +149,7 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
)
}
if (settings$search_param == "team") {
if (!private$scan_all) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][team:{settings$team_name}] Pulling commits...")
}
repos_list_with_commits <- private$pull_commits_from_repos(
Expand Down
6 changes: 5 additions & 1 deletion R/EngineGraphQLGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
org <- gsub("%2f", "/", org)
if (settings$search_param == "org") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling repositories...")
if (settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling repositories..."
)
}
}
repos_table <- private$pull_repos_from_org(
from = "org",
Expand Down
15 changes: 10 additions & 5 deletions R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
pull_repos = function(org,
settings) {
if (settings$search_param == "phrase") {
if (!private$scan_all) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][phrase:{settings$phrase}][org:{org}] Searching repositories...")
}
repos_table <- private$search_repos_by_phrase(
Expand Down Expand Up @@ -40,7 +40,9 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
repos_table <- NULL
if (settings$search_param %in% c("org")) {
if (!private$scan_all) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][org:{org}] Pulling repositories...")
if (settigs$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][org:{org}] Pulling repositories...")
}
}
repos_endpoint <- paste0(self$rest_api_url, "/orgs/", org, "/repos")
repos_table <- private$paginate_results(
Expand Down Expand Up @@ -74,11 +76,14 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",

#' @description A method to add information on repository contributors.
#' @param repos_table A table of repositories.
#' @param settings GitStats settings.
#' @return A table of repositories with added information on contributors.
pull_repos_contributors = function(repos_table) {
pull_repos_contributors = function(repos_table, settings) {
if (nrow(repos_table) > 0) {
if (!private$scan_all) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors...")
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitHub][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors..."
)
}
repo_iterator <- paste0(repos_table$organization, "/", repos_table$repo_name)
user_name <- rlang::expr(.$login)
Expand Down
54 changes: 35 additions & 19 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
pull_repos = function(org,
settings) {
if (settings$search_param == "phrase") {
if (!private$scan_all) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][phrase:{settings$phrase}][org:{gsub('%2f', '/', org)}] Searching repositories...")
}
repos_table <- private$search_repos_by_phrase(
Expand All @@ -26,8 +26,10 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
private$prepare_repos_table() %>%
private$pull_repos_issues()
} else if (settings$search_param == "team") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}][team:{settings$team_name}] Pulling repositories...")
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}][team:{settings$team_name}] Pulling repositories..."
)
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
Expand All @@ -36,7 +38,8 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
private$pull_repos_issues()
suppressMessages({
repos_table <- self$pull_repos_contributors(
repos_table = repos_table
repos_table = repos_table,
settings = settings
) %>%
private$filter_repos_by_team(team = settings$team)
})
Expand All @@ -54,8 +57,10 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
settings) {
repos_table <- NULL
if (settings$search_param == "org") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}] Pulling repositories...")
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}] Pulling repositories..."
)
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
Expand All @@ -68,11 +73,16 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",

#' @description A method to add information on repository contributors.
#' @param repos_table A table of repositories.
#' @param settings GitStats settings.
#' @return A table of repositories with added information on contributors.
pull_repos_contributors = function(repos_table) {
pull_repos_contributors = function(repos_table, settings) {
if (nrow(repos_table) > 0) {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors...")
if (settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors..."
)
}
}
repo_iterator <- repos_table$repo_id
user_name <- rlang::expr(.$name)
Expand Down Expand Up @@ -114,10 +124,12 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
if (is.null(.storage$repositories)) {
repos_table <- self$pull_repos_supportive(
org = org,
settings = list(search_param = "org")
settings = settings
)
} else {
cli::cli_alert_info("Using repositories stored in `GitStats` object.")
if (settings$verbose) {
cli::cli_alert_info("Using repositories stored in `GitStats` object.")
}
repos_table <- .storage$repositories %>%
dplyr::filter(
organization == org
Expand All @@ -131,12 +143,14 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
}
if (!private$scan_all) {
org_disp <- stringr::str_replace_all(org, "%2f", "/")
if (settings$search_param == "org") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}] Pulling commits...")
} else if (settings$search_param == "repo") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}][custom repositories] Pulling commits...")
} else if (settings$search_param == "team") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}][team:{settings$team_name}] Pulling commits...")
if (settings$verbose) {
if (settings$search_param == "org") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}] Pulling commits...")
} else if (settings$search_param == "repo") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}][custom repositories] Pulling commits...")
} else if (settings$search_param == "team") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}][team:{settings$team_name}] Pulling commits...")
}
}
}
repos_list_with_commits <- private$pull_commits_from_repos(
Expand All @@ -154,7 +168,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
commits_table <- repos_list_with_commits %>%
private$tailor_commits_info(org = org) %>%
private$prepare_commits_table() %>%
private$get_commits_authors_handles_and_names()
private$get_commits_authors_handles_and_names(settings)

return(commits_table)
}
Expand Down Expand Up @@ -445,9 +459,11 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
# @description A method to get separately GL logins and display names
# @param commits_table A table
# @return A data.frame
get_commits_authors_handles_and_names = function(commits_table) {
get_commits_authors_handles_and_names = function(commits_table, settings) {
if (nrow(commits_table) > 0) {
cli::cli_alert_info("Looking up for authors' names and logins...")
if (settings$verbose) {
cli::cli_alert_info("Looking up for authors' names and logins...")
}
authors_dict <- purrr::map(unique(commits_table$author), function(author) {
if (self$rest_api_url != "https://gitlab.com/api/v4") {
author <- stringr::str_replace_all(author, " ", "%20")
Expand Down
Loading

0 comments on commit 349c9c8

Please sign in to comment.