Skip to content

Commit 5ad52c8

Browse files
authored
Merge pull request #33 from ropensci-review-tools/metrics
cm_metrics_num_contribs for #23
2 parents 8829f04 + 271dbca commit 5ad52c8

20 files changed

+225
-647
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: repometrics
22
Title: Metrics for Your Code Repository
3-
Version: 0.1.1.077
3+
Version: 0.1.1.088
44
Authors@R:
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-2172-5265"))

R/chaoss-external.R

-65
This file was deleted.

R/chaoss-hybrid.R

-19
This file was deleted.

R/chaoss-internal-ci.R

-20
This file was deleted.

R/cm-data-gh-prs.R

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ cm_data_prs_from_gh_api_internal <- function (path, n_per_page = 30L) {
132132
}
133133
}
134134

135+
num_commits <- vapply (
136+
pr_data,
137+
function (i) length (i$commits$nodes),
138+
integer (1L)
139+
)
135140
commit_oids <- vapply (pr_data, function (i) {
136141
oids <- vapply (i$commits$nodes, function (j) {
137142
j$commit$oid
@@ -181,6 +186,7 @@ cm_data_prs_from_gh_api_internal <- function (path, n_per_page = 30L) {
181186
created_at = vapply (pr_data, function (i) i$createdAt, character (1L)),
182187
closed_at = vapply (pr_data, function (i) null2na_char (i$closedAt), character (1L)),
183188
updated_at = vapply (pr_data, function (i) i$updatedAt, character (1L)),
189+
num_commits = num_commits,
184190
additions = vapply (pr_data, function (i) i$additions, integer (1L)),
185191
deletions = vapply (pr_data, function (i) i$deletions, integer (1L)),
186192
changed_files = vapply (pr_data, function (i) i$changedFiles, integer (1L)),

R/cm-data-gh-workflow.R

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#' Retrieve latest GitHub workflow results from Rest API
2+
#'
3+
#' This uses default of 30 most recent results.
4+
#' @noRd
5+
#'
6+
#'
7+
cm_data_gh_repo_workflow_internal <- function (path, n = 30L) {
8+
9+
or <- org_repo_from_path (path)
10+
11+
checkmate::assert_integer (n, lower = 1L)
12+
u_wf <- gh_rest_api_endpoint (orgrepo = or, endpoint = "actions/runs")
13+
14+
req <- httr2::request (u_wf) |>
15+
add_gh_token_to_req () |>
16+
httr2::req_url_query (per_page = n)
17+
18+
resp <- httr2::req_perform (req)
19+
httr2::resp_check_status (resp)
20+
21+
body <- httr2::resp_body_json (resp)
22+
workflows <- body$workflow_runs
23+
24+
ids <- vapply (workflows, function (i) i$id, numeric (1L))
25+
names <- vapply (workflows, function (i) i$name, character (1L))
26+
shas <- vapply (workflows, function (i) i$head_sha, character (1L))
27+
titles <- vapply (
28+
workflows,
29+
function (i) null2na_char (i$display_title),
30+
character (1L)
31+
)
32+
status <- vapply (
33+
workflows,
34+
function (i) null2na_char (i$status),
35+
character (1L)
36+
)
37+
conclusion <- vapply (workflows, function (i) null2na_char (i$conclusion), character (1L))
38+
created <- vapply (workflows, function (i) i$created_at, character (1L))
39+
created <- to_posix (created)
40+
41+
data.frame (
42+
name = names,
43+
id = ids,
44+
sha = shas,
45+
title = titles,
46+
status = status,
47+
conclusion = conclusion,
48+
created = created
49+
)
50+
}
51+
cm_data_gh_repo_workflow <- memoise::memoise (cm_data_gh_repo_workflow_internal)

R/cm-metric-cran-downloads.R

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Extract total CRAN downloads for nominated package over period defined by
2+
#' `options("repometrics_period")`.
3+
#'
4+
#' @param pkg_name Name of package. For packages not on CRAN, the 'cranlogs'
5+
#' API returns download counts of 0.
6+
#' @param end_date The date up to which download counts are to be aggregated.
7+
#' @return A single integer counting the number of downloads.
8+
#' @noRd
9+
cm_metric_cran_downloads <- function (pkg_name, end_date = Sys.Date ()) {
10+
11+
checkmate::assert_character (pkg_name, len = 1L)
12+
checkmate::assert_date (end_date)
13+
period <- get_repometrics_period ()
14+
start_date <- as.Date (end_date - period)
15+
interval <- paste (start_date, sep = ":", end_date)
16+
17+
base_url <- "http://cranlogs.r-pkg.org/"
18+
daily_url <- paste0 (base_url, "downloads/total/")
19+
req_url <- paste0 (daily_url, interval, "/", pkg_name)
20+
21+
req <- httr2::request (req_url)
22+
resp <- httr2::req_perform (req)
23+
httr2::resp_check_status (resp)
24+
25+
body <- httr2::resp_body_json (resp)
26+
return (body [[1]]$downloads)
27+
}

R/cm-metric-has-ci.R

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Return names of CI services from corresponding configuration files contained
2+
#' in local repo.
3+
#' @noRd
4+
repo_has_ci_files <- function (path) {
5+
6+
flist <- fs::dir_ls (path, type = "file", all = TRUE, recurse = TRUE)
7+
ptns <- c (
8+
"\\.appveyor\\.yml",
9+
"\\.github\\/workflows",
10+
"\\.gitlab\\-ci\\.yml",
11+
"\\.circleci\\/config\\.yml",
12+
"codefresh\\.yml",
13+
"drone\\.yml",
14+
"\\.travis\\.yml"
15+
)
16+
has_it <- vapply (ptns, function (i) any (grepl (i, flist)), logical (1L))
17+
nms <- gsub ("\\\\.|\\\\.y.*$|\\\\/.*$|\\\\-.*$", "", ptns)
18+
19+
return (nms [which (has_it)])
20+
}
21+
22+
has_gh_ci_tests <- function (path) {
23+
24+
ci_data <- cm_data_repo_from_gh_api (path)
25+
h <- gert::git_log (repo = path, max = 1e6)
26+
any (ci_data$sha %in% h$commit)
27+
}
28+
29+
cm_metric_has_ci <- function (path) {
30+
31+
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
32+
has_ci <- ifelse (is_test_env, FALSE, has_gh_ci_tests (path))
33+
34+
if (!has_ci) {
35+
ci_files <- repo_has_ci_files (path)
36+
has_ci <- length (ci_files) > 0L
37+
if (has_ci) {
38+
cli::cli_alert_info (paste0 (
39+
"Unable to determine whether runs are ",
40+
"recent for CI service [{ci_files}]."
41+
))
42+
}
43+
}
44+
45+
return (has_ci)
46+
}

R/cm-metrics-num-commits.R R/cm-metric-num-commits.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cm_metrics_num_commits <- function (path, end_date = Sys.Date ()) {
1+
cm_metric_num_commits <- function (path, end_date = Sys.Date ()) {
22

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

R/chaoss-internal-num-ctb.R R/cm-metric-num-ctb.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
chaoss_internal_num_contributors <- function (path, end_date = Sys.Date ()) {
1+
cm_metric_num_contributors <- function (path, end_date = Sys.Date ()) {
22

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

R/chaoss-internal-change-req.R R/cm-metrics-change-req.R

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
#' @return NA if no commits made in given period, otherwise the proportion of
1313
#' commits which came from merged branches.
1414
#' @noRd
15-
chaoss_internal_change_req <- function (path, end_date = Sys.Date ()) {
15+
cm_metric_change_req <- function (path, end_date = Sys.Date ()) {
1616

1717
log <- git_log_in_period (path, end_date, get_repometrics_period ())
18+
if (nrow (log) == 0) {
19+
return (0)
20+
}
21+
1822
prs <- cm_data_prs_from_gh_api (path)
1923
prs <- prs [which (prs$merged), ]
2024
closed_dates <- as.Date (prs$closed_at)
2125
start_date <- end_date - get_repometrics_period ()
2226
index <- which (closed_dates >= start_date & closed_dates <= end_date)
2327
prs <- prs [index, ]
2428

25-
num_commits <- vapply (prs$commit_oids, function (i) {
26-
length (strsplit (i, ",") [[1]])
27-
}, integer (1L), USE.NAMES = FALSE)
28-
29-
res <- sum (num_commits) / nrow (log)
29+
res <- sum (prs$num_commits) / nrow (log)
3030

3131
return (res)
3232
}

0 commit comments

Comments
 (0)