Skip to content

Commit 5380fbc

Browse files
authored
Merge pull request #55 from ropensci-review-tools/user
rearrange rm user fns to all accept same params
2 parents cc6a3ef + df45b8b commit 5380fbc

11 files changed

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

R/data-gh-user.R

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# All functions accept same parameters, even through not all parameters are
2+
# used in all functions.
3+
14
gh_user_general_qry <- function (login = "") {
25

36
q <- paste0 ("{
@@ -36,7 +39,11 @@ gh_user_general_qry <- function (login = "") {
3639
return (q)
3740
}
3841

39-
gh_user_general_internal <- function (login = "") {
42+
# Only uses `login` param:
43+
gh_user_general_internal <- function (login = "",
44+
ended_at = Sys.time (),
45+
nyears = 1,
46+
n_per_page = 100L) {
4047

4148
q <- gh_user_general_qry (login = login)
4249
dat <- gh::gh_gql (query = q)
@@ -90,6 +97,7 @@ gh_user_general_internal <- function (login = "") {
9097
gh_user_general <- memoise::memoise (gh_user_general_internal)
9198

9299
#' Query for both followers and following
100+
#'
93101
#' @noRd
94102
gh_user_follow_qry <- function (login = "",
95103
followers = TRUE,
@@ -124,7 +132,12 @@ gh_user_follow_qry <- function (login = "",
124132
return (q)
125133
}
126134

127-
gh_user_follow_internal <- function (login, followers = TRUE, n_per_page = 100L) {
135+
# Uses all parameters except `ended_at` at `nyears`.
136+
gh_user_follow_internal <- function (login,
137+
ended_at = Sys.time (),
138+
nyears = 1,
139+
n_per_page = 100L,
140+
followers = TRUE) {
128141

129142
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
130143
n_per_page <- n_per_page_in_tests (n_per_page)
@@ -197,7 +210,11 @@ gh_user_commit_cmt_qry <- function (login = "",
197210
return (q)
198211
}
199212

200-
gh_user_commit_cmt_internal <- function (login, n_per_page = 100L) {
213+
# Uses all params except `ended_at` and `nyears`:
214+
gh_user_commit_cmt_internal <- function (login,
215+
ended_at = Sys.time (),
216+
nyears = 1,
217+
n_per_page = 100L) {
201218

202219
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
203220
n_per_page <- n_per_page_in_tests (n_per_page)
@@ -251,6 +268,8 @@ gh_user_commit_cmt <- memoise::memoise (gh_user_commit_cmt_internal)
251268

252269
# These are aggregated per repository, so no page cursors needed. Only
253270
# restriction is maxRepositories, but that also does not allow further paging.
271+
#
272+
# Uses all parameters
254273
gh_user_commits_qry <- function (login = "",
255274
ended_at = Sys.time (),
256275
nyears = 1,
@@ -326,6 +345,7 @@ gh_user_commits_internal <- function (login,
326345
}
327346
gh_user_commits <- memoise::memoise (gh_user_commits_internal)
328347

348+
# Uses all parameters
329349
gh_user_issues_qry <- function (login = "",
330350
ended_at = Sys.time (),
331351
nyears = 1,
@@ -390,6 +410,7 @@ gh_user_issues_qry <- function (login = "",
390410
return (q)
391411
}
392412

413+
# Uses all parameters
393414
gh_user_issues_internal <- function (login,
394415
ended_at = Sys.time (),
395416
nyears = 1,
@@ -545,7 +566,9 @@ gh_user_issue_cmts_qry <- function (login = "",
545566
return (q)
546567
}
547568

569+
# Uses all parameters except `ended_at`
548570
gh_user_issue_cmts_internal <- function (login,
571+
ended_at = Sys.time (),
549572
nyears = 1,
550573
n_per_page = 100L) {
551574

R/data-user.R

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#' Extract and combine all user data
22
#'
33
#' @param login GitHub login of user
4+
#' @param ended_at Parameter used in some aspects of resultant data to limit
5+
#' the end date of data collection. Defaults to `Sys.time()`.
6+
#' @param nyears Parameter <= 1 determining fraction of a year over which data
7+
#' up until `end_date` are collected.
8+
#' @param n_per_page Number of items per page to pass to GitHub GraphQL API
9+
#' requests. This should never need to be changed.
410
#' @return A list of the following `data.frame` objects:
511
#' \enumerate{
612
#' \item `commit_cmt` with details of commits made on commits
@@ -12,23 +18,37 @@
1218
#' \item `issues` with information on all issues opened by user
1319
#' }
1420
#' @export
15-
rm_data_user <- function (login) {
21+
rm_data_user <- function (login,
22+
ended_at = Sys.time (),
23+
nyears = 1,
24+
n_per_page = 100) {
1625

1726
checkmate::assert_character (login, len = 1L)
1827

1928
data_fns <- get_rm_gh_user_fns ()
29+
pars <- list (
30+
login = login,
31+
n_per_page = n_per_page,
32+
ended_at = ended_at,
33+
nyears = nyears
34+
)
2035

21-
if (all_gh_user_fns_memoised (data_fns, login)) {
36+
if (all_gh_user_fns_memoised (data_fns, pars)) {
2237
res <- lapply (data_fns, function (i) {
23-
do.call (i, list (login = login))
38+
do.call (i, pars)
2439
})
2540
} else {
2641
res <- pbapply::pblapply (data_fns, function (i) {
2742
do.call (i, list (login = login))
2843
})
2944
}
3045
names (res) <- gsub ("^gh\\_user\\_", "", data_fns)
31-
gsub ("^gh\\_user\\_", "", data_fns)
46+
47+
names (res) <- gsub ("follow", "followers", names (res))
48+
res$following <- do.call (gh_user_follow, c (pars, followers = FALSE))
49+
50+
i <- grep ("general", names (res))
51+
res <- c (res [i], res [-i] [order (names (res) [-i])])
3252

3353
return (res)
3454
}
@@ -42,10 +62,15 @@ get_rm_gh_user_fns <- function () {
4262
return (data_fns)
4363
}
4464

45-
all_gh_user_fns_memoised <- function (data_fns, login) {
65+
all_gh_user_fns_memoised <- function (data_fns, pars) {
4666
is_memoised <- vapply (data_fns, function (i) {
4767
tryCatch (
48-
memoise::has_cache (get (i)) (login),
68+
memoise::has_cache (get (i)) (
69+
login = pars$login,
70+
n_per_page = pars$n_per_page,
71+
ended_at = pars$ended_at,
72+
nyears = pars$nyears
73+
),
4974
error = function (e) FALSE
5075
)
5176
}, logical (1L))

codemeta.json

+2-2
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.2.059",
11+
"version": "0.1.3.003",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",
@@ -237,7 +237,7 @@
237237
},
238238
"SystemRequirements": {}
239239
},
240-
"fileSize": "834.886KB",
240+
"fileSize": "1502.07KB",
241241
"readme": "https://github.com/ropensci-review-tools/repometrics/blob/main/README.md",
242242
"contIntegration": [
243243
"https://github.com/ropensci-review-tools/repometrics/actions?query=workflow%3AR-CMD-check",

man/rm_data_user.Rd

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/gh_user_commit_cmt/api.github.com/graphql-93c966-POST.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"createdAt": "2016-07-31T20:32:28Z",
1717
"repository": {
1818
"url": "https://github.com/ropensci/osmplotr",
19-
"stargazerCount": 135
19+
"stargazerCount": 136
2020
},
2121
"url": "https://github.com/ropensci/osmplotr/commit/d6d8ea8281a24526926a15b5938ad600c339af18#commitcomment-18464847"
2222
},

tests/testthat/helper-rm-data.R

+28-24
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,32 @@ mock_rm_data <- function (repo = TRUE) {
4242
# rm-data-user:
4343
login <- "mpadge"
4444
ended_at <- as.POSIXct ("2024-01-01T00:00:00")
45+
pars <- list (
46+
login = login,
47+
n_per_page = 1,
48+
ended_at = ended_at,
49+
nyears = 1
50+
)
4551
general <- httptest2::with_mock_dir ("gh_user_general", {
46-
gh_user_general (login)
52+
do.call (gh_user_general, pars)
4753
})
4854
followers <- httptest2::with_mock_dir ("gh_user_followers", {
49-
gh_user_follow (login, followers = TRUE, n_per_page = 1)
55+
do.call (gh_user_follow, c (pars, followers = TRUE))
5056
})
5157
following <- httptest2::with_mock_dir ("gh_user_following", {
52-
gh_user_follow (login, followers = FALSE, n_per_page = 1)
58+
do.call (gh_user_follow, c (pars, followers = FALSE))
5359
})
5460
user_commit_cmt <- httptest2::with_mock_dir ("gh_user_commit_cmt", {
55-
gh_user_commit_cmt (login, n_per_page = 1)
61+
do.call (gh_user_commit_cmt, pars)
5662
})
5763
user_commits <- httptest2::with_mock_dir ("gh_user_commits", {
58-
gh_user_commits (login, n_per_page = 1, ended_at = ended_at)
64+
do.call (gh_user_commits, pars)
5965
})
6066
user_issues <- httptest2::with_mock_dir ("gh_user_issues", {
61-
gh_user_issues (login, n_per_page = 1, ended_at = ended_at)
67+
do.call (gh_user_issues, pars)
6268
})
6369
user_issue_cmts <- httptest2::with_mock_dir ("gh_user_issue_cmts", {
64-
gh_user_issue_cmts (login, n_per_page = 1)
70+
do.call (gh_user_issue_cmts, pars)
6571
})
6672

6773
# cran_downloads fn needs modified DESC:
@@ -86,26 +92,24 @@ mock_rm_data <- function (repo = TRUE) {
8692
res$contribs_from_gh_api
8793
)
8894
} else {
89-
data_fns <- get_rm_gh_user_fns () # length = 6
95+
data_fns <- get_rm_gh_user_fns ()
9096
pars <- list (
91-
gh_user_general = list (login = login),
92-
gh_user_followers =
93-
list (login = login, followers = TRUE, n_per_page = 1),
94-
gh_user_following =
95-
list (login = login, followers = FALSE, n_per_page = 1),
96-
gh_user_commit_cmt = list (login = login, n_per_page = 1),
97-
gh_user_commits =
98-
list (login = login, n_per_page = 1, ended_at = ended_at),
99-
gh_user_issues =
100-
list (login = login, n_per_page = 1, ended_at = ended_at),
101-
gh_user_issue_cmts = list (login = login, n_per_page = 1)
102-
) # length = 7
97+
login = login,
98+
n_per_page = 1,
99+
ended_at = ended_at,
100+
nyears = 1
101+
)
103102

104-
res <- lapply (seq_along (pars), function (i) {
105-
fn_i <- gsub ("follow.*$", "follow", names (pars) [i])
106-
do.call (fn_i, pars [[i]])
103+
res <- lapply (data_fns, function (i) {
104+
do.call (i, pars)
107105
})
108-
names (res) <- gsub ("^gh\\_user\\_", "", names (pars))
106+
names (res) <- gsub ("^gh\\_user\\_", "", data_fns)
107+
108+
names (res) <- gsub ("follow", "followers", names (res))
109+
res$following <- do.call (gh_user_follow, c (pars, followers = FALSE))
110+
111+
i <- grep ("general", names (res))
112+
res <- c (res [i], res [-i] [order (names (res) [-i])])
109113
}
110114

111115
fs::dir_delete (path)

tests/testthat/test-rm-data-gh-user.R

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Extends from 'cm-data-github.R' to test the user-specific data
22

3-
test_that ("cm data gh general", {
3+
test_that ("rm user data internal structures", {
44

55
Sys.setenv ("REPOMETRICS_TESTS" = "true")
66
dat <- mock_rm_data (repo = FALSE)
77

88
expect_type (dat, "list")
99
expect_length (dat, 7L)
10-
nms <- c ("general", "followers", "following", "commit_cmt", "commits", "issues", "issue_cmts")
10+
nms <- c (
11+
"general", "commit_cmt", "commits", "followers", "following",
12+
"issue_cmts", "issues"
13+
)
1114
expect_named (dat, nms)
1215

1316
expect_type (dat$general, "list")
@@ -65,3 +68,20 @@ test_that ("cm data gh general", {
6568
nms <- c ("org_repo", "issue_num", "created_at", "num_comments", "num_participants")
6669
expect_named (dat$issue_cmts, nms)
6770
})
71+
72+
test_that ("rm_data_user fn", {
73+
74+
Sys.setenv ("REPOMETRICS_TESTS" = "true")
75+
dat_mocked <- mock_rm_data (repo = FALSE)
76+
77+
login <- "mpadge"
78+
ended_at <- as.POSIXct ("2024-01-01T00:00:00")
79+
80+
dat <- rm_data_user (
81+
login = login,
82+
n_per_page = 1,
83+
ended_at = ended_at,
84+
nyears = 1
85+
)
86+
expect_identical (dat, dat_mocked)
87+
})

tests/testthat/test-rm-data-git.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that ("cm data git", {
1+
test_that ("rm data git", {
22

33
path <- generate_test_pkg ()
44

@@ -33,7 +33,7 @@ skip_on_cran ()
3333
# triggered with `rm_data_libyears()`?
3434
skip_on_os ("mac")
3535

36-
test_that ("cm data libyears", {
36+
test_that ("rm data libyears", {
3737

3838
dat <- mock_rm_data ()
3939

0 commit comments

Comments
 (0)