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

cm data #32

Merged
merged 8 commits into from
Nov 25, 2024
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.1.069
Version: 0.1.1.077
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
17 changes: 13 additions & 4 deletions R/chaoss-internal-change-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
chaoss_internal_change_req <- function (path, end_date = Sys.Date ()) {

log <- git_log_in_period (path, end_date, get_repometrics_period ())
res <- NA_integer_
if (nrow (log) > 0L) {
res <- length (which (log$merge)) / nrow (log)
}
prs <- cm_data_prs_from_gh_api (path)
prs <- prs [which (prs$merged), ]
closed_dates <- as.Date (prs$closed_at)
start_date <- end_date - get_repometrics_period ()
index <- which (closed_dates >= start_date & closed_dates <= end_date)
prs <- prs [index, ]

Check warning on line 23 in R/chaoss-internal-change-req.R

View check run for this annotation

Codecov / codecov/patch

R/chaoss-internal-change-req.R#L18-L23

Added lines #L18 - L23 were not covered by tests

num_commits <- vapply (prs$commit_oids, function (i) {
length (strsplit (i, ",") [[1]])
}, integer (1L), USE.NAMES = FALSE)

Check warning on line 27 in R/chaoss-internal-change-req.R

View check run for this annotation

Codecov / codecov/patch

R/chaoss-internal-change-req.R#L25-L27

Added lines #L25 - L27 were not covered by tests

res <- sum (num_commits) / nrow (log)

Check warning on line 29 in R/chaoss-internal-change-req.R

View check run for this annotation

Codecov / codecov/patch

R/chaoss-internal-change-req.R#L29

Added line #L29 was not covered by tests

return (res)
}
19 changes: 4 additions & 15 deletions R/chaoss-internal-num-ctb.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ chaoss_internal_num_contributors <- function (path, end_date = Sys.Date ()) {

log <- git_log_in_period (path, end_date, get_repometrics_period ())

auths_un <- unique (log$author)

# separate handles from emails:
emails <- regmatches (auths_un, gregexpr ("<.*>", auths_un))
emails <- vapply (emails, function (i) {
ifelse (length (i) == 0L, "", gsub ("<|>", "", i))
}, character (1L))
handles <- gsub ("<.*$", "", auths_un)

# Remove any duplicates of either, but excluding non-entries:
# Remove any duplicates of either names or emails, but excluding non-entries:
rm_dup_rows <- function (x) {
x <- gsub ("\\s+", "", x)
index <- seq_along (x)
Expand All @@ -21,15 +12,13 @@ chaoss_internal_num_contributors <- function (path, end_date = Sys.Date ()) {
}
return (index)
}
index1 <- rm_dup_rows (handles)
index2 <- rm_dup_rows (emails)
index1 <- rm_dup_rows (log$aut_name)
index2 <- rm_dup_rows (log$aut_email)

# Then extract only instances where neither handles nor emails are
# duplicated:
index_table <- table (c (index1, index2))
index <- as.integer (names (index_table) [which (index_table == 2L)])

auths_un <- auths_un [index]

return (length (auths_un))
return (length (index))
}
28 changes: 0 additions & 28 deletions R/chaoss-internal.R

This file was deleted.

6 changes: 4 additions & 2 deletions R/cm-data-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ cm_data_dependencies <- function (path) {
cm_data_libyears <- function (path) {

deps <- cm_data_dependencies (path)
cran_db <- data.frame (tools::CRAN_package_db ())
cran_db <- data.frame (cran_pkg_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 <- cm_data_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))
}

cran_pkg_db <- memoise::memoise (tools::CRAN_package_db)
8 changes: 6 additions & 2 deletions R/cm-data-gh-contribs.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
contribs_from_log <- function (log) {
cm_data_contribs_from_log <- function (path) {

log <- cm_data_gitlog (path)

Check warning on line 3 in R/cm-data-gh-contribs.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-contribs.R#L3

Added line #L3 was not covered by tests

gh_handle <- unique (log$aut_name)
gh_email <- log$aut_email [match (gh_handle, log$aut_name)]
Expand Down Expand Up @@ -27,7 +29,7 @@
) [index, ]
}

contribs_from_gh_api <- function (path, n_per_page = 100) {
cm_data_contribs_from_gh_api_internal <- function (path, n_per_page = 100) {

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

Expand Down Expand Up @@ -80,6 +82,8 @@

return (ctbs)
}
cm_data_contribs_from_gh_api <-
memoise::memoise (cm_data_contribs_from_gh_api_internal)

user_from_gh_api <- function (user) {

Expand Down
8 changes: 6 additions & 2 deletions R/cm-data-gh-issues.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issues_from_gh_api <- function (path, n_per_page = 100) {
cm_data_issues_from_gh_api_internal <- function (path, n_per_page = 100) {

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

Expand Down Expand Up @@ -69,6 +69,8 @@ issues_from_gh_api <- function (path, n_per_page = 100) {

return (issues)
}
cm_data_issues_from_gh_api <-
memoise::memoise (cm_data_issues_from_gh_api_internal)

get_issue_reactions <- function (body) {

Expand All @@ -87,7 +89,7 @@ get_issue_reactions <- function (body) {
return (reaction_counts)
}

issue_comments_from_gh_api <- function (path, n_per_page = 100) {
cm_data_issue_comments_from_gh_api_internal <- function (path, n_per_page = 100) {

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

Expand Down Expand Up @@ -138,3 +140,5 @@ issue_comments_from_gh_api <- function (path, n_per_page = 100) {
issue_body = issue_body
)
}
cm_data_issue_comments_from_gh_api <-
memoise::memoise (cm_data_issue_comments_from_gh_api_internal)
5 changes: 3 additions & 2 deletions R/cm-data-gh-prs.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ gh_prs_qry <- function (org = "ropensci-review-tools",
return (q)
}

prs_from_gh_api <- function (path, n_per_page = 30L) {
cm_data_prs_from_gh_api_internal <- function (path, n_per_page = 30L) {

is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
if (is_test_env) {
Expand Down Expand Up @@ -158,7 +158,7 @@ prs_from_gh_api <- function (path, n_per_page = 30L) {
reviews <- lapply (pr_data, function (i) {
login <- vapply (i$reviews$nodes, function (j) j$author$login, character (1L))
state <- vapply (i$reviews$nodes, function (j) j$state, character (1L))
submitted_at <- vapply (i$reviews$nodes, function (j) j$submittedAt, character (1L))
submitted_at <- vapply (i$reviews$nodes, function (j) null2na_char (j$submittedAt), character (1L))
body <- vapply (i$reviews$nodes, function (j) j$body, character (1L))
data.frame (
login = login,
Expand Down Expand Up @@ -193,3 +193,4 @@ prs_from_gh_api <- function (path, n_per_page = 30L) {
reviews = I (reviews)
)
}
cm_data_prs_from_gh_api <- memoise::memoise (cm_data_prs_from_gh_api_internal)
3 changes: 2 additions & 1 deletion R/cm-data-gh-releases.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
releases_from_gh_api <- function (path, n_per_page = 100L, latest_only = FALSE) {
cm_data_releases_from_gh_api_internal <- function (path, n_per_page = 100L, latest_only = FALSE) {

checkmate::assert_integerish (n_per_page)
checkmate::assert_logical (latest_only)
Expand Down Expand Up @@ -48,3 +48,4 @@ releases_from_gh_api <- function (path, n_per_page = 100L, latest_only = FALSE)
published_at = vapply (body, function (i) i$published_at, character (1L))
)
}
cm_data_releases_from_gh_api <- memoise::memoise (cm_data_releases_from_gh_api_internal)
3 changes: 2 additions & 1 deletion R/cm-data-gh-repo.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
repo_from_gh_api <- function (path) {
cm_data_repo_from_gh_api_internal <- function (path) {

or <- org_repo_from_path (path)

Expand Down Expand Up @@ -36,3 +36,4 @@ repo_from_gh_api <- function (path) {
default_branch = null2na_char (body$default_branch)
)
}
cm_data_repo_from_gh_api <- memoise::memoise (cm_data_repo_from_gh_api_internal)
23 changes: 23 additions & 0 deletions R/cm-data-git.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,26 @@
)
}
cm_data_gitlog <- memoise::memoise (cm_data_gitlog_internal)

git_log_in_period <- function (path, end_date = Sys.Date (), period = 90) {

checkmate::assert_character (path, len = 1L)
checkmate::assert_directory (path)
checkmate::assert_date (end_date)

log <- cm_data_gitlog (path)

if (nrow (log) == 0) {
return (log)

Check warning on line 63 in R/cm-data-git.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-git.R#L63

Added line #L63 was not covered by tests
}
dates <- as.Date (log$time)
today_minus_period <- as.Date (end_date - period)
index <- which (dates >= today_minus_period)
log <- log [index, ]

if (dates [1] > end_date) {
log <- log [which (dates <= end_date), ]
}

return (log)
}
35 changes: 35 additions & 0 deletions R/cm-data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cm_data <- function (path) {

data_fns <- get_cm_data_fns ()

Check warning on line 3 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L3

Added line #L3 was not covered by tests

if (all_cm_data_fns_memoised (data_fns, path)) {
res <- lapply (data_fns, function (i) {
do.call (i, list (path))
})

Check warning on line 8 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L5-L8

Added lines #L5 - L8 were not covered by tests
} else {
res <- pbapply::pblapply (data_fns, function (i) {
do.call (i, list (path))
})

Check warning on line 12 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L10-L12

Added lines #L10 - L12 were not covered by tests
}
names (res) <- gsub ("^cm\\_data\\_", "", data_fns)

Check warning on line 14 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L14

Added line #L14 was not covered by tests

return (res)

Check warning on line 16 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L16

Added line #L16 was not covered by tests
}

get_cm_data_fns <- function () {

pkg_fns <- ls (envir = asNamespace ("repometrics"))
data_fns <- grep ("^cm\\_data\\_", pkg_fns, value = TRUE)
data_fns [which (!grepl ("\\_internal$", data_fns))]

Check warning on line 23 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L21-L23

Added lines #L21 - L23 were not covered by tests
}

all_cm_data_fns_memoised <- function (data_fns, path) {
is_memoised <- vapply (data_fns, function (i) {
tryCatch (
memoise::has_cache (get (i)) (path),
error = function (e) FALSE
)
}, logical (1L))

Check warning on line 32 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L27-L32

Added lines #L27 - L32 were not covered by tests

length (which (is_memoised)) > (length (data_fns) / 2)

Check warning on line 34 in R/cm-data.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data.R#L34

Added line #L34 was not covered by tests
}
6 changes: 6 additions & 0 deletions R/cm-metrics-num-commits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cm_metrics_num_commits <- function (path, end_date = Sys.Date ()) {

log <- git_log_in_period (path, end_date, get_repometrics_period ())

return (nrow (log))
}
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.069",
"version": "0.1.1.077",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/gh_libyears/ghrepos/repo/releases-4aabf7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"url": "ghrepos/repo//releases/158953460",
"assets_url": "ghrepos/repo//releases/158953460/assets",
"upload_url": "https://uploads.github.com/repos/repo//releases/158953460/assets{?name,label}",
"html_url": "https://github.com/repo//releases/tag/v1.0.5",
"id": 158953460,
"author": {
"login": "mpadge",
"id": 6697851,
"node_id": "MDQ6VXNlcjY2OTc4NTE=",
"avatar_url": "https://avatars.githubusercontent.com/u/6697851?v=4",
"gravatar_id": "",
"url": "ghusers/mpadge",
"html_url": "https://github.com/mpadge",
"followers_url": "ghusers/mpadge/followers",
"following_url": "ghusers/mpadge/following{/other_user}",
"gists_url": "ghusers/mpadge/gists{/gist_id}",
"starred_url": "ghusers/mpadge/starred{/owner}{/repo}",
"subscriptions_url": "ghusers/mpadge/subscriptions",
"organizations_url": "ghusers/mpadge/orgs",
"repos_url": "ghusers/mpadge/repos",
"events_url": "ghusers/mpadge/events{/privacy}",
"received_events_url": "ghusers/mpadge/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"node_id": "RE_kwDOAwboEs4JeW_0",
"tag_name": "v1.0.5",
"target_commitish": "main",
"name": "goodpractice 1.0.5",
"draft": false,
"prerelease": false,
"created_at": "2024-06-05T09:31:17Z",
"published_at": "2024-06-05T09:34:42Z",
"assets": [

],
"tarball_url": "ghrepos/repo//tarball/v1.0.5",
"zipball_url": "ghrepos/repo//zipball/v1.0.5",
"body": "* New maintainer: rOpenSci\r\n* Package reinstated on CRAN, after archiving of previous version.\r\n* CRAN fixes - skipping failing test and adding \\alias{goodpractice} to package Rd\r\n* Adding docs.ropensci site to DESCRIPTION",
"reactions": {
"url": "ghrepos/repo//releases/158953460/reactions",
"total_count": 1,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 1,
"rocket": 0,
"eyes": 0
}
}
]
14 changes: 1 addition & 13 deletions tests/testthat/test-chaoss-metrics-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that ("chaoss internal num_commits", {

path <- generate_test_pkg ()

n <- chaoss_internal_num_commits (path, end_date = end_date)
n <- cm_metrics_num_commits (path, end_date = end_date)
expect_equal (n, 4L)

n <- chaoss_internal_num_contributors (path, end_date = end_date)
Expand Down Expand Up @@ -39,15 +39,3 @@ test_that ("chaoss has CI internal", {

fs::dir_delete (path)
})

test_that ("chaoss internal change requests", {

path <- generate_test_pkg ()

x <- chaoss_internal_change_req (path, end_date = end_date)
expect_equal (x, 0)
x <- chaoss_internal_change_req (path, end_date = Sys.Date ())
expect_equal (x, NA_integer_) # no commits, so NA returned

fs::dir_delete (path)
})
Loading
Loading