Skip to content

Commit c712e11

Browse files
authored
Merge pull request #40 from ropensci-review-tools/stars
stargazer data
2 parents b8d959a + bc96403 commit c712e11

8 files changed

+141
-5
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.115
3+
Version: 0.1.1.117
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ gh_forks_qry <- function (org = "ropensci-review-tools",
2929
return (q)
3030
}
3131

32-
cm_data_repo_forks <- function (path, n_per_page = 100L) {
32+
cm_data_repo_forks_internal <- function (path, n_per_page = 100L) {
3333

3434
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
3535
n_per_page <- n_per_page_in_tests (n_per_page)
@@ -65,3 +65,4 @@ cm_data_repo_forks <- function (path, n_per_page = 100L) {
6565
created = as.Date (created)
6666
)
6767
}
68+
cm_data_repo_forks <- memoise::memoise (cm_data_repo_forks_internal)

R/cm-data-gh-stargazers.R

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
gh_stargazers_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+
stargazers (first: ", n_per_page, after_txt, ") {
16+
pageInfo {
17+
hasNextPage
18+
endCursor
19+
}
20+
edges {
21+
starredAt
22+
node {
23+
login
24+
}
25+
}
26+
}
27+
}
28+
}")
29+
30+
return (q)
31+
}
32+
33+
cm_data_repo_stargazers_internal <- function (path, n_per_page = 100L) {
34+
35+
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
36+
n_per_page <- n_per_page_in_tests (n_per_page)
37+
38+
or <- org_repo_from_path (path)
39+
end_cursor <- stargazers <- NULL
40+
has_next_page <- TRUE
41+
42+
while (has_next_page) {
43+
44+
q <- gh_stargazers_qry (
45+
org = or [1],
46+
repo = or [2],
47+
end_cursor = end_cursor,
48+
n_per_page = n_per_page
49+
)
50+
dat <- gh::gh_gql (query = q)
51+
52+
stargazers <- c (stargazers, dat$data$repository$stargazers$edges)
53+
54+
has_next_page <- dat$data$repository$stargazers$pageInfo$hasNextPage
55+
end_cursor <- dat$data$repository$stargazers$pageInfo$endCursor
56+
if (is_test_env) {
57+
has_next_page <- FALSE
58+
}
59+
}
60+
61+
login <- vapply (stargazers, function (i) i$node$login, character (1L))
62+
starred_at <- vapply (stargazers, function (i) i$starredAt, character (1L))
63+
64+
data.frame (
65+
login = login,
66+
starred_at = as.Date (starred_at)
67+
)
68+
}
69+
cm_data_repo_stargazers <- memoise::memoise (cm_data_repo_stargazers_internal)

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.115",
11+
"version": "0.1.1.117",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"stargazers": {
5+
"pageInfo": {
6+
"hasNextPage": true,
7+
"endCursor": "Y3Vyc29yOnYyOpK5MjAxNi0wMi0wMlQwOToxODoxOSswMTowMM4DFLwY"
8+
},
9+
"edges": [
10+
{
11+
"starredAt": "2016-02-01T10:52:19Z",
12+
"node": {
13+
"login": "Pakillo"
14+
}
15+
},
16+
{
17+
"starredAt": "2016-02-02T08:18:19Z",
18+
"node": {
19+
"login": "uribo"
20+
}
21+
}
22+
]
23+
}
24+
}
25+
}
26+
}

tests/testthat/helper-cm-data.R

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ mock_cm_data <- function () {
1717
forks <- httptest2::with_mock_dir ("gh_api_forks", {
1818
cm_data_repo_forks (path)
1919
})
20+
stargazers <- httptest2::with_mock_dir ("gh_api_stars", {
21+
cm_data_repo_stargazers (path)
22+
})
2023
issues <- httptest2::with_mock_dir ("gh_api_issues", {
2124
cm_data_issues_from_gh_api (path)
2225
})

tests/testthat/test-cm-data-github.R

+36
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,39 @@ test_that ("cm data gh repo", {
248248
expect_type (repo [[n]], type)
249249
}
250250
})
251+
252+
test_that ("cm data gh forks", {
253+
254+
Sys.setenv ("REPOMETRICS_TESTS" = "true")
255+
mock_cm_data ()
256+
path <- generate_test_pkg ()
257+
forks <- cm_data_repo_forks (path)
258+
fs::dir_delete (path)
259+
260+
expect_s3_class (forks, "data.frame")
261+
expect_equal (nrow (forks), 2L)
262+
expect_equal (ncol (forks), 2L)
263+
nms <- c ("org_repo", "created")
264+
expect_equal (names (forks), nms)
265+
266+
expect_type (forks$org_repo, "character")
267+
expect_type (forks$created, "double")
268+
})
269+
270+
test_that ("cm data gh stars", {
271+
272+
Sys.setenv ("REPOMETRICS_TESTS" = "true")
273+
mock_cm_data ()
274+
path <- generate_test_pkg ()
275+
stars <- cm_data_repo_stargazers (path)
276+
fs::dir_delete (path)
277+
278+
expect_s3_class (stars, "data.frame")
279+
expect_equal (nrow (stars), 2L)
280+
expect_equal (ncol (stars), 2L)
281+
nms <- c ("login", "starred_at")
282+
expect_equal (names (stars), nms)
283+
284+
expect_type (stars$login, "character")
285+
expect_type (stars$starred_at, "double")
286+
})

tests/testthat/test-cm-data.R

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

1111
expect_type (dat, "list")
12-
expect_length (dat, 12L)
12+
expect_length (dat, 13L)
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_forks", "repo_from_gh_api"
17+
"releases_from_gh_api", "repo_forks", "repo_from_gh_api",
18+
"repo_stargazers"
1819
)
1920
expect_equal (names (dat), nms)
2021

0 commit comments

Comments
 (0)