From 9acf98fbf65d18e659e297854976227b78f0d865 Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Tue, 30 Jan 2024 17:02:27 +0100 Subject: [PATCH 1/4] Factor out `add_contributors_one_repo` --- R/add-contributors.R | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/R/add-contributors.R b/R/add-contributors.R index 79b2288..42046e2 100644 --- a/R/add-contributors.R +++ b/R/add-contributors.R @@ -2,8 +2,8 @@ #' #' Add contributors to README.Rmd #' -#' @param repo Location of repository for which contributions are to be -#' extracted. This must be a git project with a github remote. +#' @param repo Vector of repository locations for which contributions are to be +#' extracted. Each location must be a git project with a github remote. #' @param ncols Number of columns for contributors in 'README' #' @param files Names of files in which to add contributors #' @param type Type of contributions to include: 'code' for direct code @@ -81,7 +81,36 @@ add_contributors <- function (repo = ".", alphabetical = FALSE, open_issue = FALSE, force_update = FALSE) { + ctbs <- add_contributors_one_repo( + repo, + type, + exclude_label, + exclude_issues, + exclude_not_planned, + num_sections, + section_names, + format, + alphabetical + ) + chk <- add_contribs_to_files ( + ctbs$ctbs, ctbs$or, ncols, format, files, + open_issue, force_update + ) + + invisible (unlist (chk)) +} + +add_contributors_one_repo <- function ( + repo, + type, + exclude_label, + exclude_issues, + exclude_not_planned, + num_sections, + section_names, + format, + alphabetical) { if (!in_git_repository (repo)) { stop ("The path [", repo, "] does not appear to be a git repository") } @@ -136,12 +165,7 @@ add_contributors <- function (repo = ".", ctbs <- rename_default_sections (ctbs) - chk <- add_contribs_to_files ( - ctbs, or, ncols, format, files, - open_issue, force_update - ) - - invisible (unlist (chk)) + return(list(ctbs, or)) } match_type_arg <- function (type) { From 75725f22bcca3419641f7fa79ab463f403379df9 Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Wed, 31 Jan 2024 12:26:15 +0100 Subject: [PATCH 2/4] Add loop across vector of repo paths --- R/add-contributors.R | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/R/add-contributors.R b/R/add-contributors.R index 42046e2..f9d8d79 100644 --- a/R/add-contributors.R +++ b/R/add-contributors.R @@ -81,8 +81,12 @@ add_contributors <- function (repo = ".", alphabetical = FALSE, open_issue = FALSE, force_update = FALSE) { - ctbs <- add_contributors_one_repo( - repo, + # Init list + all_repos <- list(ctbs = data.frame()) + + for (rep in repo) { + one_repo <- add_contributors_one_repo( + repo = rep, type, exclude_label, exclude_issues, @@ -90,11 +94,18 @@ add_contributors <- function (repo = ".", num_sections, section_names, format, - alphabetical - ) + alphabetical) + all_repos$ctbs <- rbind(all_repos$ctbs, one_repo$ctbs) + all_repos$or <- one_repo$or + } + + # Deduplicate if multiple repositories + if (length(repo) > 1) { + all_repos$ctbs <- all_repos$ctbs[!duplicated(all_repos$ctbs[c("logins")]), ] + } chk <- add_contribs_to_files ( - ctbs$ctbs, ctbs$or, ncols, format, files, + all_repos$ctbs, all_repos$or, ncols, format, files, open_issue, force_update ) @@ -165,7 +176,7 @@ add_contributors_one_repo <- function ( ctbs <- rename_default_sections (ctbs) - return(list(ctbs, or)) + return(list(ctbs = ctbs, or = or)) } match_type_arg <- function (type) { From beedc8a2bad9503257995a65dbca404a8ed95484 Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Mon, 5 Feb 2024 13:34:37 +0100 Subject: [PATCH 3/4] Rename to --- R/add-contributors.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/add-contributors.R b/R/add-contributors.R index f9d8d79..ed1c993 100644 --- a/R/add-contributors.R +++ b/R/add-contributors.R @@ -85,7 +85,7 @@ add_contributors <- function (repo = ".", all_repos <- list(ctbs = data.frame()) for (rep in repo) { - one_repo <- add_contributors_one_repo( + one_repo <- get_contributors_one_repo( repo = rep, type, exclude_label, @@ -112,7 +112,7 @@ add_contributors <- function (repo = ".", invisible (unlist (chk)) } -add_contributors_one_repo <- function ( +get_contributors_one_repo <- function ( repo, type, exclude_label, From ae48da5493562fadbb99b6e985a05f0a18f0901d Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Mon, 5 Feb 2024 14:11:13 +0100 Subject: [PATCH 4/4] Refactor loop to apply, sum contribution counts --- R/add-contributors.R | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/R/add-contributors.R b/R/add-contributors.R index ed1c993..0abe35b 100644 --- a/R/add-contributors.R +++ b/R/add-contributors.R @@ -81,31 +81,29 @@ add_contributors <- function (repo = ".", alphabetical = FALSE, open_issue = FALSE, force_update = FALSE) { - # Init list - all_repos <- list(ctbs = data.frame()) - - for (rep in repo) { + all_repos <- do.call(rbind, lapply(repo, function(rep) { one_repo <- get_contributors_one_repo( - repo = rep, - type, - exclude_label, - exclude_issues, - exclude_not_planned, - num_sections, - section_names, - format, - alphabetical) - all_repos$ctbs <- rbind(all_repos$ctbs, one_repo$ctbs) - all_repos$or <- one_repo$or - } + repo = rep, + type, + exclude_label, + exclude_issues, + exclude_not_planned, + num_sections, + section_names, + format, + alphabetical) - # Deduplicate if multiple repositories - if (length(repo) > 1) { - all_repos$ctbs <- all_repos$ctbs[!duplicated(all_repos$ctbs[c("logins")]), ] - } + return(one_repo) + })) + + combined_df <- do.call(rbind, all_repos[, 'ctbs']) + combined_df$contributions <- ave(combined_df$contributions, combined_df$login, FUN = sum) + + # Remove duplicate rows + result <- combined_df[!duplicated(combined_df[c("logins")]), ] chk <- add_contribs_to_files ( - all_repos$ctbs, all_repos$or, ncols, format, files, + result, all_repos[, 'or'][[1]], ncols, format, files, open_issue, force_update )