Skip to content

Commit

Permalink
Merge pull request #507 from r-world-devs/474-optimize-get_r_package_…
Browse files Browse the repository at this point in the history
…usage

474 optimize get r package usage
  • Loading branch information
maciekbanas authored Oct 17, 2024
2 parents ecb8a9f + 498eb4e commit e19f470
Show file tree
Hide file tree
Showing 17 changed files with 749 additions and 291 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.9004
Version: 2.1.0.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
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# GitStats (development version)

## Features:

- Optimized `get_R_package_usage()` function:
- you can now pass a vector of packages names (new `packages` parameter replacing old `package_name`) ([#494](https://github.com/r-world-devs/GitStats/issues/494)),
- on the other hand, output of the function has been limited to contain only most necessary data (removing all repository stats), making thus process of obtaining package usage faster ([#474](https://github.com/r-world-devs/GitStats/issues/474)).
- new `split_output` parameter has been added - when set to `TRUE` a `list` with `tibbles` (every element of the `list` for every package) instead of one `tibble` is returned.
- Added possibility to get repositories for individual users with `get_repos()` ([#492](https://github.com/r-world-devs/GitStats/issues/492)). Earlier this was only possible for GitHub organizations and GitLab groups.

## Fixes:

- 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)).
Expand Down
29 changes: 19 additions & 10 deletions R/EngineRest.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ EngineRest <- R6::R6Class("EngineRest",
},

# Prepare table for repositories content
prepare_repos_table = function(repos_list, verbose = TRUE) {
prepare_repos_table = function(repos_list, output = "table_full", verbose = TRUE) {
repos_dt <- purrr::map(repos_list, function(repo) {
repo <- purrr::map(repo, function(attr) {
attr <- attr %||% ""
Expand All @@ -51,15 +51,24 @@ EngineRest <- R6::R6Class("EngineRest",
cli::cli_alert_info("Preparing repositories table...")
}
if (length(repos_dt) > 0) {
repos_dt <- dplyr::mutate(
repos_dt,
repo_id = as.character(repo_id),
created_at = as.POSIXct(created_at),
last_activity_at = as.POSIXct(last_activity_at),
forks = as.integer(forks),
issues_open = as.integer(issues_open),
issues_closed = as.integer(issues_closed)
)
if (output == "table_full") {
repos_dt <- dplyr::mutate(
repos_dt,
repo_id = as.character(repo_id),
created_at = as.POSIXct(created_at),
last_activity_at = as.POSIXct(last_activity_at),
forks = as.integer(forks),
issues_open = as.integer(issues_open),
issues_closed = as.integer(issues_closed)
)
}
if (output == "table_min") {
repos_dt <- dplyr::mutate(
repos_dt,
repo_id = as.character(repo_id),
created_at = as.POSIXct(created_at)
)
}
}
return(repos_dt)
}
Expand Down
57 changes: 35 additions & 22 deletions R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ EngineRestGitHub <- R6::R6Class(
org = NULL,
filename = NULL,
in_path = FALSE,
raw_output = FALSE,
output = "table_full",
verbose = TRUE,
progress = TRUE) {
user_query <- if (!is.null(org)) {
Expand All @@ -53,12 +53,12 @@ EngineRestGitHub <- R6::R6Class(
search_endpoint = search_endpoint,
total_n = total_n
)
if (!raw_output) {
if (output == "table_full" || output == "table_min") {
search_output <- private$map_search_into_repos(
search_response = search_result,
progress = progress
)
} else {
} else if (output == "raw") {
search_output <- search_result
}
} else {
Expand All @@ -68,26 +68,39 @@ EngineRestGitHub <- R6::R6Class(
},

# Retrieve only important info from repositories response
tailor_repos_response = function(repos_response) {
tailor_repos_response = function(repos_response, output = "table_full") {
repos_list <- purrr::map(repos_response, function(repo) {
list(
"repo_id" = repo$id,
"repo_name" = repo$name,
"default_branch" = repo$default_branch,
"stars" = repo$stargazers_count,
"forks" = repo$forks_count,
"created_at" = gts_to_posixt(repo$created_at),
"last_activity_at" = if (!is.null(repo$pushed_at)) {
gts_to_posixt(repo$pushed_at)
} else {
gts_to_posixt(repo$created_at)
},
"languages" = repo$language,
"issues_open" = repo$issues_open,
"issues_closed" = repo$issues_closed,
"organization" = repo$owner$login,
"repo_url" = repo$html_url
)
if (output == "table_full") {
repo_data <- list(
"repo_id" = repo$id,
"repo_name" = repo$name,
"default_branch" = repo$default_branch,
"stars" = repo$stargazers_count,
"forks" = repo$forks_count,
"created_at" = gts_to_posixt(repo$created_at),
"last_activity_at" = if (!is.null(repo$pushed_at)) {
gts_to_posixt(repo$pushed_at)
} else {
gts_to_posixt(repo$created_at)
},
"languages" = repo$language,
"issues_open" = repo$issues_open,
"issues_closed" = repo$issues_closed,
"organization" = repo$owner$login,
"repo_url" = repo$html_url
)
}
if (output == "table_min") {
repo_data <- list(
"repo_id" = repo$id,
"repo_name" = repo$name,
"default_branch" = repo$default_branch,
"created_at" = gts_to_posixt(repo$created_at),
"organization" = repo$owner$login,
"repo_url" = repo$html_url
)
}
return(repo_data)
})
return(repos_list)
},
Expand Down
64 changes: 40 additions & 24 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ EngineRestGitLab <- R6::R6Class(
# filtering by language. For more information look here:
# https://gitlab.com/gitlab-org/gitlab/-/issues/340333
get_repos_by_code = function(code,
org = NULL,
org = NULL,
filename = NULL,
in_path = FALSE,
raw_output = FALSE,
verbose = TRUE,
in_path = FALSE,
output = "table_full",
verbose = TRUE,
progress = TRUE) {
search_response <- private$search_for_code(
code = code,
Expand All @@ -75,37 +75,53 @@ EngineRestGitLab <- R6::R6Class(
org = org,
verbose = verbose
)
if (raw_output) {
if (output == "raw") {
search_output <- search_response
} else {
} else if (output == "table_full" || output == "table_min") {
search_output <- search_response %>%
private$map_search_into_repos(
progress = progress
) %>%
private$get_repos_languages(
progress = progress
)
if (output == "table_full") {
search_output <- search_output %>%
private$get_repos_languages(
progress = progress
)
}
}
return(search_output)
},

# Retrieve only important info from repositories response
tailor_repos_response = function(repos_response) {
tailor_repos_response = function(repos_response, output = "table_full") {
repos_list <- purrr::map(repos_response, function(project) {
list(
"repo_id" = project$id,
"repo_name" = project$name,
"default_branch" = project$default_branch,
"stars" = project$star_count,
"forks" = project$fork_count,
"created_at" = project$created_at,
"last_activity_at" = project$last_activity_at,
"languages" = paste0(project$languages, collapse = ", "),
"issues_open" = project$issues_open,
"issues_closed" = project$issues_closed,
"organization" = project$namespace$path,
"repo_url" = project$web_url
)
if (output == "table_full") {
repo_data <- list(
"repo_id" = project$id,
"repo_name" = project$name,
"default_branch" = project$default_branch,
"stars" = project$star_count,
"forks" = project$fork_count,
"created_at" = project$created_at,
"last_activity_at" = project$last_activity_at,
"languages" = paste0(project$languages, collapse = ", "),
"issues_open" = project$issues_open,
"issues_closed" = project$issues_closed,
"organization" = project$namespace$path,
"repo_url" = project$web_url
)
}
if (output == "table_min") {
repo_data <- list(
"repo_id" = project$id,
"repo_name" = project$name,
"default_branch" = project$default_branch,
"created_at" = project$created_at,
"organization" = project$namespace$path,
"repo_url" = project$web_url
)
}
return(repo_data)
})
return(repos_list)
},
Expand Down
Loading

0 comments on commit e19f470

Please sign in to comment.