Skip to content

Commit 49d0803

Browse files
authored
Merge pull request #38 from ropensci-review-tools/forks
add cm-metric-num-forks for #11
2 parents 9e4bd9b + 5a4ba2f commit 49d0803

8 files changed

+122
-4
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.110
3+
Version: 0.1.1.114
44
Authors@R:
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-2172-5265"))

R/cm-data-gh-forks.R

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
gh_forks_qry <- function (org = "ropensci-review-tools",
2+
repo = "repometrics",
3+
n_per_page = 100L,
4+
end_cursor = NULL) {
5+
6+
checkmate::assert_integerish (n_per_page)
7+
8+
after_txt <- ""
9+
if (!is.null (end_cursor)) {
10+
after_txt <- paste0 (", after:\"", end_cursor, "\"")
11+
}
12+
13+
q <- paste0 ("{
14+
repository(owner:\"", org, "\", name:\"", repo, "\") {
15+
forks (first: ", n_per_page, after_txt, ") {
16+
pageInfo {
17+
hasNextPage
18+
endCursor
19+
}
20+
nodes {
21+
createdAt
22+
nameWithOwner
23+
url
24+
}
25+
}
26+
}
27+
}")
28+
29+
return (q)
30+
}
31+
32+
cm_data_repo_forks <- function (path, n_per_page = 100L) {
33+
34+
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
35+
n_per_page <- n_per_page_in_tests (n_per_page)
36+
37+
or <- org_repo_from_path (path)
38+
end_cursor <- fork_data <- NULL
39+
has_next_page <- TRUE
40+
41+
while (has_next_page) {
42+
43+
q <- gh_forks_qry (
44+
org = or [1],
45+
repo = or [2],
46+
end_cursor = end_cursor,
47+
n_per_page = n_per_page
48+
)
49+
dat <- gh::gh_gql (query = q)
50+
51+
fork_data <- c (fork_data, dat$data$repository$forks$nodes)
52+
53+
has_next_page <- dat$data$repository$forks$pageInfo$hasNextPage
54+
end_cursor <- dat$data$repository$forks$pageInfo$endCursor
55+
if (is_test_env) {
56+
has_next_page <- FALSE
57+
}
58+
}
59+
60+
org_repo <- vapply (fork_data, function (i) i$nameWithOwner, character (1L))
61+
created <- vapply (fork_data, function (i) i$createdAt, character (1L))
62+
63+
data.frame (
64+
org_repo = org_repo,
65+
created = as.Date (created)
66+
)
67+
}

R/cm-metric-num-forks.R

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cm_metric_num_forks <- function (path, end_date = Sys.Date ()) {
2+
3+
forks <- cm_data_repo_forks (path)
4+
5+
start_date <- end_date - get_repometrics_period ()
6+
index <- which (forks$created >= start_date & forks$created <= end_date)
7+
8+
return (c (num_in_period = length (index), num_total = nrow (forks)))
9+
}

codemeta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"codeRepository": "https://github.com/ropensci-review-tools/repometrics",
99
"issueTracker": "https://github.com/ropensci-review-tools/repometrics/issues",
1010
"license": "https://spdx.org/licenses/GPL-3.0",
11-
"version": "0.1.1.110",
11+
"version": "0.1.1.114",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"forks": {
5+
"pageInfo": {
6+
"hasNextPage": true,
7+
"endCursor": "Y3Vyc29yOnYyOpHOBDjiGQ=="
8+
},
9+
"nodes": [
10+
{
11+
"createdAt": "2016-08-17T17:52:57Z",
12+
"nameWithOwner": "richelbilderbeek/goodpractice",
13+
"url": "https://github.com/richelbilderbeek/goodpractice"
14+
},
15+
{
16+
"createdAt": "2016-10-13T18:37:09Z",
17+
"nameWithOwner": "jlehtoma/goodpractice",
18+
"url": "https://github.com/jlehtoma/goodpractice"
19+
}
20+
]
21+
}
22+
}
23+
}
24+
}

tests/testthat/helper-cm-data.R

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ mock_cm_data <- function () {
1414
repo <- httptest2::with_mock_dir ("gh_api_repo", {
1515
cm_data_repo_from_gh_api (path)
1616
})
17+
forks <- httptest2::with_mock_dir ("gh_api_forks", {
18+
cm_data_repo_forks (path)
19+
})
1720
issues <- httptest2::with_mock_dir ("gh_api_issues", {
1821
cm_data_issues_from_gh_api (path)
1922
})

tests/testthat/test-cm-data.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ test_that ("cm data full", {
99
fs::dir_delete (path)
1010

1111
expect_type (dat, "list")
12-
expect_length (dat, 11L)
12+
expect_length (dat, 12L)
1313
nms <- c (
1414
"contribs_from_gh_api", "contribs_from_log", "dependencies",
1515
"gh_repo_workflow", "gitlog", "issue_comments_from_gh_api",
1616
"issues_from_gh_api", "libyears", "prs_from_gh_api",
17-
"releases_from_gh_api", "repo_from_gh_api"
17+
"releases_from_gh_api", "repo_forks", "repo_from_gh_api"
1818
)
1919
expect_equal (names (dat), nms)
2020

tests/testthat/test-cm-metrics.R

+15
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ test_that ("cm metrics pr-reviews", {
130130
)
131131
expect_equal (names (revs), nms)
132132
})
133+
134+
test_that ("cm metrics num forks", {
135+
136+
Sys.setenv ("REPOMETRICS_TESTS" = "true")
137+
mock_cm_data ()
138+
path <- generate_test_pkg ()
139+
forks <- cm_metric_num_forks (path, end_date = end_date)
140+
fs::dir_delete (path)
141+
142+
expect_type (forks, "integer")
143+
expect_length (forks, 2L)
144+
expect_named (forks)
145+
expect_equal (names (forks), c ("num_in_period", "num_total"))
146+
expect_true (forks [["num_total"]] > 0)
147+
})

0 commit comments

Comments
 (0)