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 cm-collate-orgs for #87 #98

Merged
merged 6 commits into from
Feb 5, 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: repometrics
Title: Metrics for Your Code Repository
Version: 0.1.5.030
Version: 0.1.6.005
Authors@R:
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
63 changes: 63 additions & 0 deletions R/cm-collate-orgs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
repometrics_collate_org_data <- function (org_paths, end_date = Sys.Date, num_years = 3) {

pkgs <- fs::dir_ls (org_paths, type = "directory")
data <- lapply (seq_along (pkgs), function (i) {
cli::cli_alert_info ("[{i} / {length(pkgs)}]: {pkgs[i]}")
path_i <- pkgs [i]
pkg_i <- basename (path_i)
f_tmp <- fs::path (fs::path_temp (), paste0 (pkg_i, ".Rds"))
if (fs::file_exists (f_tmp)) {
dat_i <- readRDS (f_tmp)
} else {
dat_i <- list (
repo = repometrics_data_repo (path_i),
metrics = metrics_over_end_dates (
path_i,
end_date = end_date,
num_years = num_years
),
models = models_over_end_dates (
path_i,
end_date = end_date,
num_years = num_years
)
)
saveRDS (dat_i, f_tmp)

Check warning on line 25 in R/cm-collate-orgs.R

View check run for this annotation

Codecov / codecov/patch

R/cm-collate-orgs.R#L12-L25

Added lines #L12 - L25 were not covered by tests
}
return (dat_i)
})

pkgs_repos <- lapply (data, function (i) i$repo)
pkgs_metrics <- lapply (data, function (i) i$metrics)
pkgs_models <- lapply (data, function (i) i$models)

or <- vapply (pkgs, function (i) {
paste0 (org_repo_from_path (i), collapse = "/")
}, character (1L), USE.NAMES = FALSE)
names (pkgs_repos) <- names (pkgs_metrics) <- names (pkgs_models) <- or

pkgs_models <- lapply (seq_along (pkgs_models), function (i) {
pkgs_models [[i]] |>
dplyr::mutate (package = names (pkgs_models) [i], .before = 1)
})
pkgs_models <- do.call (rbind, pkgs_models)

rm_tmp_pkg_files (or)

data <- list (
repos = pkgs_repos,
metrics = pkgs_metrics,
models = pkgs_models
)

return (data)
}

rm_tmp_pkg_files <- function (pkgs) {
pkgs <- gsub ("^.*\\/", "", unique (pkgs))
f_tmp_list <- fs::path (fs::path_temp (), paste0 (pkgs, ".Rds"))
f_tmp_list <- f_tmp_list [which (fs::file_exists (f_tmp_list))]
if (length (f_tmp_list) > 0L) {
fs::file_delete (f_tmp_list)

Check warning on line 61 in R/cm-collate-orgs.R

View check run for this annotation

Codecov / codecov/patch

R/cm-collate-orgs.R#L61

Added line #L61 was not covered by tests
}
}
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
if (num_cores < 1L) {
cli::cli_abort ("Number of cores must be at least 1")
}
if (!nzchar (Sys.getenv ("GITHUB_WORKFLOW", ""))) {
# Force single core on all gha workflows:
num_cores <- 1L

Check warning on line 41 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L41

Added line #L41 was not covered by tests
}
return (num_cores)
}

Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/repometrics",
"issueTracker": "https://github.com/ropensci-review-tools/repometrics/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.5.030",
"version": "0.1.6.005",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -285,7 +285,7 @@
},
"SystemRequirements": {}
},
"fileSize": "634.4KB",
"fileSize": "12626.955KB",
"readme": "https://github.com/ropensci-review-tools/repometrics/blob/main/README.md",
"contIntegration": [
"https://github.com/ropensci-review-tools/repometrics/actions?query=workflow%3AR-CMD-check",
Expand Down
55 changes: 55 additions & 0 deletions tests/testthat/test-cm-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,58 @@ test_that ("collate all models", {
model_fns <- get_cm_fns ("model")
expect_named (models_all, c ("date", gsub ("cm\\_model\\_", "", model_fns)))
})

test_that ("collate across orgs", {

Sys.setenv ("REPOMETRICS_TESTS" = "true")
dat <- mock_rm_data ()
path <- generate_test_pkg ()

# Then mock one set of org data:
metrics_all <- withr::with_options (
list ("repometrics_period" = 365.25 / 2),
metrics_over_end_dates (path, end_date = end_date, num_years = 1)
)
models_all <- withr::with_options (
list ("repometrics_period" = 365.25 / 2),
models_over_end_dates (path, end_date = end_date, num_years = 1)
)
dat <- list (
repo = repometrics_data_repo (path, num_cores = 1L),
metrics = metrics_all,
models = models_all
)

org_dir <- fs::path (fs::path_temp (), "org")
if (!fs::dir_exists (org_dir)) {
fs::dir_create (org_dir)
}
path1 <- fs::dir_copy (path, fs::path (org_dir, "testpkg1"))
path2 <- fs::dir_copy (path, fs::path (org_dir, "testpkg2"))
fs::dir_delete (path)

d1 <- desc::desc_set ("Package" = "testpkg1", file = path1)
d2 <- desc::desc_set ("Package" = "testpkg2", file = path2)

saveRDS (dat, fs::path (fs::path_temp (), "testpkg1.Rds"))
saveRDS (dat, fs::path (fs::path_temp (), "testpkg2.Rds"))

# Then the main call, which loads those pre-saved data rather than
# re-generating:
org_data <- repometrics_collate_org_data (
org_dir,
end_date = end_date,
num_years = 1
)

fs::dir_delete (c (path1, path2))

expect_type (org_data, "list")
expect_named (org_data, c ("repos", "metrics", "models"))
npkgs <- 2L
expect_length (org_data$repos, npkgs)
expect_length (org_data$metrics, npkgs)
expect_s3_class (org_data$models, "data.frame")
n_periods <- length (get_end_date_seq (end_date = end_date, num_years = 1))
expect_equal (nrow (org_data$models), n_periods)
})
Loading