Skip to content

Commit

Permalink
Merge pull request #31 from ropensci-review-tools/libyears
Browse files Browse the repository at this point in the history
libyears for #23
  • Loading branch information
mpadge authored Nov 21, 2024
2 parents 00d7b99 + 768eb0f commit 3c2b739
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
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.1.065
Version: 0.1.1.068
Authors@R:
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
18 changes: 18 additions & 0 deletions R/cm-data-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ cm_data_dependencies <- function (path) {

data.frame (do.call (rbind, deps))
}

cm_data_libyears <- function (path) {

deps <- cm_data_dependencies (path)
cran_db <- data.frame (tools::CRAN_package_db ())
index <- match (deps$name, cran_db$Package)
deps$cran_version <- cran_db$Version [index]
deps$published <- as.Date (cran_db$Published [index])
deps <- deps [which (!is.na (deps$published)), ]

rel <- releases_from_gh_api (path, latest_only = TRUE)
rel_date <- as.Date (strftime (rel$published_at, format = "%Y-%m-%d"))

dt <- difftime (deps$published, rel_date, units = "days")
dt <- as.numeric (dt) / 365.25 # In years

c (mean = mean (dt), median = stats::median (dt))
}
11 changes: 9 additions & 2 deletions R/cm-data-gh-releases.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
releases_from_gh_api <- function (path, n_per_page = 100L) {
releases_from_gh_api <- function (path, n_per_page = 100L, latest_only = FALSE) {

checkmate::assert_integerish (n_per_page)
checkmate::assert_logical (latest_only)

if (latest_only) {
n_per_page <- 1L
}

is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"

Expand All @@ -19,7 +26,7 @@ releases_from_gh_api <- function (path, n_per_page = 100L) {
body <- c (body, httr2::resp_body_json (resp))

next_page <- gh_next_page (resp)
if (is_test_env) {
if (is_test_env || latest_only) {
next_page <- NULL
}

Expand Down
2 changes: 1 addition & 1 deletion 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.1.065",
"version": "0.1.1.068",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-cm-data-git.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ test_that ("cm data dependencies", {
expect_type (deps [[n]], "character")
}
})

skip_on_cran ()

test_that ("cm data libyears", {

path <- generate_test_pkg ()
libyears <- with_mock_dir ("gh_libyears", {
cm_data_libyears (path)
})
fs::dir_delete (path)

expect_type (libyears, "double")
expect_length (libyears, 2L)
expect_named (libyears)
expect_equal (names (libyears), c ("mean", "median"))
expect_true (all (libyears > 0))
})

0 comments on commit 3c2b739

Please sign in to comment.