Skip to content

Commit afab229

Browse files
authored
Merge pull request #57 from ropensci-review-tools/user
Update user commits fn to return all commits
2 parents 9222a55 + 2d9c93a commit afab229

File tree

8 files changed

+154
-61
lines changed

8 files changed

+154
-61
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.3.010
3+
Version: 0.1.3.016
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

+83-25
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ gh_user_commit_cmt_qry <- function (login = "",
198198
}
199199
createdAt
200200
repository {
201-
url
201+
nameWithOwner
202202
stargazerCount
203203
}
204204
url
@@ -238,7 +238,7 @@ gh_user_commit_cmt_internal <- function (login,
238238
)
239239
repourl <- c (
240240
repourl,
241-
vapply (nodes, function (i) i$repository$url, character (1L))
241+
vapply (nodes, function (i) i$repository$nameWithOwner, character (1L))
242242
)
243243
repo_stargazers <- c (
244244
repo_stargazers,
@@ -252,7 +252,6 @@ gh_user_commit_cmt_internal <- function (login,
252252
}
253253
}
254254

255-
repourl <- gsub ("https://github.com/", "", repourl, fixed = TRUE)
256255
repourl <- strsplit (repourl, "\\/")
257256
org <- vapply (repourl, function (i) i [1], character (1L))
258257
repo <- vapply (repourl, function (i) i [2], character (1L))
@@ -273,7 +272,8 @@ gh_user_commit_cmt <- memoise::memoise (gh_user_commit_cmt_internal)
273272
gh_user_commits_qry <- function (login = "",
274273
ended_at = Sys.time (),
275274
nyears = 1,
276-
n_per_page = 100L) {
275+
n_per_page = 100L,
276+
end_cursor = NULL) {
277277

278278
# GraphQL API here has restriction:
279279
# "The total time spanned by 'from' and 'to' must not exceed 1 year"
@@ -282,22 +282,30 @@ gh_user_commits_qry <- function (login = "",
282282
from <- format (ended_at - 60 * 60 * 24 * 365 * nyears, "%Y-%m-%dT%H:%M:%S")
283283
ended_at <- format (ended_at, "%Y-%m-%dT%H:%M:%S")
284284

285+
after_txt <- ""
286+
if (!is.null (end_cursor)) {
287+
after_txt <- paste0 (", after:\"", end_cursor, "\"")
288+
}
289+
285290
q <- paste0 ("{
286291
user(login:\"", login, "\") {
287292
login
288293
contributionsCollection (from: \"", from, "\", to: \"", ended_at, "\") {
289294
startedAt
290295
endedAt
296+
totalCommitContributions
291297
commitContributionsByRepository (maxRepositories: ", n_per_page, ") {
292-
contributions (first: 1) {
298+
contributions (first: ", n_per_page, after_txt, ") {
293299
pageInfo {
294300
hasNextPage
295301
endCursor
296302
}
297303
totalCount
298304
nodes {
305+
occurredAt
306+
commitCount
299307
repository {
300-
url
308+
nameWithOwner
301309
}
302310
}
303311
}
@@ -314,30 +322,80 @@ gh_user_commits_internal <- function (login,
314322
nyears = 1,
315323
n_per_page = 100L) {
316324

317-
q <- gh_user_commits_qry (
318-
login = login,
319-
ended_at = ended_at,
320-
nyears = nyears,
321-
n_per_page = n_per_page
322-
)
323-
dat <- gh::gh_gql (query = q)
325+
is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
326+
n_per_page <- n_per_page_in_tests (n_per_page)
324327

325-
started_at <- dat$data$user$contributionsCollection$startedAt
326-
ended_at <- dat$data$user$contributionsCollection$endedAt
328+
repos <- num_commits <- dates <- end_cursor <- end_cursors <- NULL
327329

328-
commits <- dat$data$user$contributionsCollection$commitContributionsByRepository
330+
has_next_page <- TRUE
329331

330-
repos <- vapply (
331-
commits,
332-
function (i) i$contributions$nodes [[1]]$repository$url,
333-
character (1L)
334-
)
335-
num_commits <- vapply (commits, function (i) i$contributions$totalCount, integer (1L))
332+
while (has_next_page) {
333+
334+
q <- gh_user_commits_qry (
335+
login = login,
336+
ended_at = ended_at,
337+
nyears = nyears,
338+
n_per_page = n_per_page,
339+
end_cursor = end_cursor
340+
)
341+
dat <- gh::gh_gql (query = q)
342+
343+
collection <- dat$data$user$contributionsCollection
344+
commits <- collection$commitContributionsByRepository
345+
346+
# Query always returns `n_per_page` items, even when empty, so empty
347+
# ones must first be removed:
348+
lens <- vapply (
349+
commits,
350+
function (i) length (i$contributions$nodes),
351+
integer (1L)
352+
)
353+
commits <- commits [which (lens > 0)]
336354

355+
repos_i <- vapply (
356+
commits,
357+
function (i) i$contributions$nodes [[1]]$repository$nameWithOwner,
358+
character (1L)
359+
)
360+
361+
dates_i <- lapply (commits, function (i) {
362+
vapply (i$contributions$nodes, function (j) j$occurredAt, character (1L))
363+
})
364+
n_i <- vapply (dates_i, length, integer (1L))
365+
dates <- c (dates, unlist (dates_i))
366+
commit_count_i <- lapply (commits, function (i) {
367+
vapply (i$contributions$nodes, function (j) j$commitCount, integer (1L))
368+
})
369+
num_commits <- c (num_commits, unlist (commit_count_i))
370+
371+
repos <- c (repos, rep (repos_i, times = n_i))
372+
373+
has_next_pages <- vapply (commits, function (i) {
374+
i$contributions$pageInfo$hasNextPage
375+
}, logical (1L))
376+
end_cursors_these <- vapply (commits, function (i) {
377+
i$contributions$pageInfo$endCursor
378+
}, character (1L))
379+
end_cursors_these <- unique (end_cursors_these [which (has_next_pages)])
380+
end_cursors <- c (end_cursors, end_cursors_these)
381+
has_next_page <- length (end_cursors) > 0L && !is_test_env
382+
if (has_next_page) {
383+
end_cursor <- end_cursors [1L]
384+
end_cursors <- end_cursors [-1L]
385+
}
386+
}
387+
388+
started_at <- dat$data$user$contributionsCollection$startedAt
389+
ended_at <- dat$data$user$contributionsCollection$endedAt
390+
391+
# suppress no visible binding note:
392+
repo <- date <- NULL
337393
res <- data.frame (
338-
repo = gsub ("https://github.com/", "", repos, fixed = TRUE),
339-
num_commits = num_commits
340-
)
394+
repo = repos,
395+
num_commits = num_commits,
396+
date = dates
397+
) |>
398+
dplyr::arrange (repo, date)
341399
attr (res, "started_at") <- started_at
342400
attr (res, "ended_at") <- ended_at
343401

R/data-user.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repometrics_data_user <- function (login,
3939
})
4040
} else {
4141
res <- pbapply::pblapply (data_fns, function (i) {
42-
do.call (i, list (login = login))
42+
do.call (i, pars)
4343
})
4444
}
4545
names (res) <- gsub ("^gh\\_user\\_", "", data_fns)

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.3.010",
11+
"version": "0.1.3.016",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"createdAt": "2016-07-31T20:32:28Z",
1717
"repository": {
18-
"url": "https://github.com/ropensci/osmplotr",
18+
"nameWithOwner": "ropensci/osmplotr",
1919
"stargazerCount": 136
2020
},
2121
"url": "https://github.com/ropensci/osmplotr/commit/d6d8ea8281a24526926a15b5938ad600c339af18#commitcomment-18464847"
@@ -26,7 +26,7 @@
2626
},
2727
"createdAt": "2016-10-12T07:52:33Z",
2828
"repository": {
29-
"url": "https://github.com/osmdatar/osmdatar",
29+
"nameWithOwner": "osmdatar/osmdatar",
3030
"stargazerCount": 10
3131
},
3232
"url": "https://github.com/osmdatar/osmdatar/commit/93240e6aa82be832475a4e574ed57bcd6b3ab9ba#commitcomment-19390596"

tests/testthat/gh_user_commits/api.github.com/graphql-3b9c2a-POST.json

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"data": {
3+
"user": {
4+
"login": "mpadge",
5+
"contributionsCollection": {
6+
"startedAt": "2023-01-01T08:00:00Z",
7+
"endedAt": "2024-01-01T08:00:00Z",
8+
"totalCommitContributions": 3188,
9+
"commitContributionsByRepository": [
10+
{
11+
"contributions": {
12+
"pageInfo": {
13+
"hasNextPage": true,
14+
"endCursor": "Mg"
15+
},
16+
"totalCount": 694,
17+
"nodes": [
18+
{
19+
"occurredAt": "2023-12-31T08:00:00Z",
20+
"commitCount": 8,
21+
"repository": {
22+
"nameWithOwner": "mpadge/UrbanAnalyst"
23+
}
24+
},
25+
{
26+
"occurredAt": "2023-12-29T08:00:00Z",
27+
"commitCount": 8,
28+
"repository": {
29+
"nameWithOwner": "mpadge/UrbanAnalyst"
30+
}
31+
}
32+
]
33+
}
34+
},
35+
{
36+
"contributions": {
37+
"pageInfo": {
38+
"hasNextPage": true,
39+
"endCursor": "Mg"
40+
},
41+
"totalCount": 547,
42+
"nodes": [
43+
{
44+
"occurredAt": "2023-12-05T08:00:00Z",
45+
"commitCount": 6,
46+
"repository": {
47+
"nameWithOwner": "ropenscilabs/deposits"
48+
}
49+
},
50+
{
51+
"occurredAt": "2023-11-06T08:00:00Z",
52+
"commitCount": 4,
53+
"repository": {
54+
"nameWithOwner": "ropenscilabs/deposits"
55+
}
56+
}
57+
]
58+
}
59+
}
60+
]
61+
}
62+
}
63+
}
64+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ test_that ("rm user data internal structures", {
4848

4949
expect_s3_class (dat$commits, "data.frame")
5050
expect_true (nrow (dat$commits) > 0L)
51-
expect_equal (ncol (dat$commits), 2L)
52-
nms <- c ("repo", "num_commits")
51+
expect_equal (ncol (dat$commits), 3L)
52+
nms <- c ("repo", "num_commits", "date")
5353
expect_named (dat$commits, nms)
5454

5555
expect_s3_class (dat$issues, "data.frame")

0 commit comments

Comments
 (0)