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

Simplifying GitStats: removing set_params() function #390

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1.1.0.9006
Version: 1.1.0.9007
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"),
Expand Down
16 changes: 0 additions & 16 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,22 @@ export(get_files)
export(get_release_logs)
export(get_repos)
export(get_users)
export(reset)
export(set_github_host)
export(set_gitlab_host)
export(set_params)
export(show_data)
export(show_orgs)
export(verbose_off)
export(verbose_on)
importFrom(R6,R6Class)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_danger)
importFrom(cli,cli_alert_info)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_alert_warning)
importFrom(cli,col_green)
importFrom(cli,col_yellow)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,glimpse)
importFrom(dplyr,mutate)
importFrom(dplyr,relocate)
importFrom(httr2,req_headers)
importFrom(httr2,req_perform)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(lubridate,floor_date)
importFrom(magrittr,"%>%")
importFrom(purrr,keep)
importFrom(purrr,map)
importFrom(purrr,map_chr)
importFrom(rlang,"%||%")
importFrom(rlang,expr)
importFrom(stringr,str_length)
importFrom(stringr,str_replace)
importFrom(utils,URLdecode)
Expand Down
46 changes: 21 additions & 25 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
#' @description A method to retrieve all repositories for an organization in
#' a table format.
#' @param org An organization.
#' @param code A character, code to search for.
#' @param with_code A character, code to search for.
#' @param settings A list of `GitStats` settings.
#' @return A table.
pull_repos = function(org,
code = NULL,
with_code = NULL,
settings) {
if (settings$search_mode == "org") {
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(
org = org
) %>%
private$prepare_repos_table()
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(
org = org
) %>%
private$prepare_repos_table()
return(repos_table)
},

Expand All @@ -86,24 +84,22 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
settings = settings,
storage = storage
)
if (settings$search_mode %in% c("org", "repo")) {
if (!private$scan_all) {
if (settings$verbose) {
if (settings$search_mode == "org") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling commits...")
}
if (settings$search_mode == "repo") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}][custom repositories] Pulling commits...")
}
if (!private$scan_all) {
if (settings$verbose) {
if (settings$searching_scope == "org") {
cli::cli_alert_info("[GitHub][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling commits...")
}
if (settings$searching_scope == "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(
org = org,
repos = repos_names,
date_from = date_from,
date_until = date_until
)
}
repos_list_with_commits <- private$pull_commits_from_repos(
org = org,
repos = repos_names,
date_from = date_from,
date_until = date_until
)
names(repos_list_with_commits) <- repos_names

commits_table <- repos_list_with_commits %>%
Expand Down
58 changes: 21 additions & 37 deletions R/EngineGraphQLGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,22 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
#' @description A method to retrieve all repositories for an organization in
#' a table format.
#' @param org An organization.
#' @param code A character, code to search for.
#' @param with_code A character, code to search for.
#' @param settings A list of `GitStats` settings.
#' @return A table.
pull_repos = function(org,
code = NULL,
with_code = NULL,
settings) {
org <- URLdecode(org)
if (settings$search_mode == "org") {
if (!private$scan_all) {
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",
org = org
) %>%
private$prepare_repos_table()
} else {
repos_table <- NULL
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling repositories..."
)
}
repos_table <- private$pull_repos_from_org(
org = org
) %>%
private$prepare_repos_table()
return(repos_table)
}

Expand All @@ -82,30 +75,25 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
private = list(

# @description Iterator over pulling pages of repositories.
# @param from A character specifying if organization or user.
# @param org An organization.
# @param user A user.
# @return A list of repositories from organization.
pull_repos_from_org = function(from,
org = NULL,
pull_repos_from_org = function(org = NULL,
users = NULL) {
full_repos_list <- list()
next_page <- TRUE
repo_cursor <- ""
while (next_page) {
repos_response <- private$pull_repos_page(
from = from,
org = org,
users = users,
repo_cursor = repo_cursor
)
if (length(repos_response$data$group) == 0) {
cli::cli_abort("Empty")
}
if (from == "org") {
core_response <- repos_response$data$group$projects
repos_list <- core_response$edges
}
core_response <- repos_response$data$group$projects
repos_list <- core_response$edges
next_page <- core_response$pageInfo$hasNextPage
if (is.null(next_page)) next_page <- FALSE
if (is.null(repos_list)) repos_list <- list()
Expand All @@ -121,24 +109,20 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
},

# @description Wrapper over building GraphQL query and response.
# @param from A character specifying if organization or user
# @param org An organization.
# @param user A user.
# @param repo_cursor An end cursor for repos page.
# @return A list of repositories.
pull_repos_page = function(from,
org = NULL,
pull_repos_page = function(org = NULL,
users = NULL,
repo_cursor = "") {
if (from == "org") {
repos_query <- self$gql_query$repos_by_org(
repo_cursor = repo_cursor
)
response <- self$gql_response(
gql_query = repos_query,
vars = list("org" = org)
)
}
repos_query <- self$gql_query$repos_by_org(
repo_cursor = repo_cursor
)
response <- self$gql_response(
gql_query = repos_query,
vars = list("org" = org)
)
return(response)
},

Expand Down
44 changes: 25 additions & 19 deletions R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",

#' @description Method to get repositories with a specific code blob.
#' @param org An organization
#' @param code A character, code to search for.
#' @param with_code A character, code to search for.
#' @param settings A list of `GitStats` settings.
#' @return Table of repositories.
pull_repos = function(org,
code = NULL,
with_code = NULL,
settings) {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][code:{code}][org:{org}] Pulling repositories...")
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][code:{with_code}][org:{org}] Pulling repositories...")
}
repos_table <- private$pull_repos_by_code(
org = org,
code = code,
code = with_code,
files = settings$files
) %>%
private$tailor_repos_info() %>%
Expand All @@ -35,20 +35,16 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
pull_repos_supportive = function(org,
settings) {
repos_table <- NULL
if (settings$search_mode %in% c("org")) {
if (!private$scan_all) {
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(
repos_endpoint = repos_endpoint
) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
if (!private$scan_all && settigs$verbose) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][org:{org}] Pulling repositories...")
}
repos_endpoint <- paste0(private$endpoints[["organizations"]], org, "/repos")
repos_table <- private$paginate_results(
repos_endpoint = repos_endpoint
) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
return(repos_table)
},

Expand All @@ -67,7 +63,7 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
user_name <- rlang::expr(.$login)
repos_table$contributors <- purrr::map_chr(repo_iterator, function(repos_id) {
tryCatch({
contributors_endpoint <- paste0(self$rest_api_url, "/repos/", repos_id, "/contributors")
contributors_endpoint <- paste0(private$endpoints[["repositories"]], repos_id, "/contributors")
contributors_vec <- private$pull_contributors_from_repo(
contributors_endpoint = contributors_endpoint,
user_name = user_name
Expand All @@ -87,7 +83,9 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",

# List of endpoints
endpoints = list(
search = NULL
search = NULL,
organizations = NULL,
repositories = NULL
),

# Set endpoints for the API
Expand All @@ -96,6 +94,14 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
self$rest_api_url,
'/search/code?q='
)
private$endpoints[["organization"]] <- paste0(
self$rest_api_url,
"/orgs/"
)
private$endpoints[["repositories"]] <- paste0(
self$rest_api_url,
"/repos/"
)
},

# @description Pulling repositories where code appears
Expand Down
53 changes: 24 additions & 29 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
#' @description A method to retrieve all repositories for an organization in
#' a table format.
#' @param org A character, a group of projects.
#' @param code A character, code to search for.
#' @param with_code A character, code to search for.
#' @param settings A list of `GitStats` settings.
#' @return A table.
pull_repos = function(org,
code = NULL,
with_code = NULL,
settings) {
if (settings$search_mode == "code") {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][code:{code}][org:{URLdecode(org)}] Searching repositories...")
}
repos_table <- private$pull_repos_by_code(
org = org,
code = code,
files = settings$files
) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
} else {
repos_table <- NULL

if (!private$scan_all && settings$verbose) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][code:{with_code}][org:{URLdecode(org)}] Searching repositories...")
}
repos_table <- private$pull_repos_by_code(
org = org,
code = with_code,
files = settings$files
) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
return(repos_table)
},

Expand All @@ -39,18 +36,16 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
pull_repos_supportive = function(org,
settings) {
repos_table <- NULL
if (settings$search_mode == "org") {
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_green('REST')}][org:{URLdecode(org)}] Pulling repositories..."
)
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
if (!private$scan_all && settings$verbose) {
cli::cli_alert_info(
"[GitLab][Engine:{cli::col_green('REST')}][org:{URLdecode(org)}] Pulling repositories..."
)
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
private$tailor_repos_info() %>%
private$prepare_repos_table() %>%
private$pull_repos_issues()
return(repos_table)
},

Expand Down Expand Up @@ -116,9 +111,9 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
if (!private$scan_all) {
org_disp <- stringr::str_replace_all(org, "%2f", "/")
if (settings$verbose) {
if (settings$search_mode == "org") {
if (settings$searching_scope == "org") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}] Pulling commits...")
} else if (settings$search_mode == "repo") {
} else if (settings$searching_scope == "repo") {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org_disp}][custom repositories] Pulling commits...")
}
}
Expand Down
Loading
Loading