Skip to content

Commit

Permalink
add gh_user_contrib_collect_issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Dec 9, 2024
1 parent 5a0f906 commit 96060e7
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: repometrics
Title: Metrics for Your Code Repository
Version: 0.1.2.045
Version: 0.1.2.046
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
183 changes: 179 additions & 4 deletions R/cm-data-gh-user.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ gh_user_contrib_collect_commits_qry <- function (login = "",
contributionsCollection (from: \"", from, "\", to: \"", ended_at, "\") {
startedAt
endedAt
contributionYears
commitContributionsByRepository (maxRepositories: ", n_per_page, ") {
contributions (first: 1) {
pageInfo {
Expand All @@ -201,9 +200,9 @@ gh_user_contrib_collect_commits_qry <- function (login = "",
}

gh_user_contrib_collect_commits_internal <- function (login,
ended_at = Sys.time (),
nyears = 1,
n_per_page = 100L) {
ended_at = Sys.time (),
nyears = 1,
n_per_page = 100L) {

q <- gh_user_contrib_collect_commits_qry (
login = login,
Expand Down Expand Up @@ -236,3 +235,179 @@ gh_user_contrib_collect_commits_internal <- function (login,
}
gh_user_contrib_collect_commits <-
memoise::memoise (gh_user_contrib_collect_commits_internal)

gh_user_contrib_collect_issues_qry <- function (login = "",
ended_at = Sys.time (),
nyears = 1,
n_per_page = 100L,
end_cursor = NULL) {

# GraphQL API here has restriction:
# "The total time spanned by 'from' and 'to' must not exceed 1 year"
checkmate::assert_numeric (nyears, len = 1L, upper = 1)

Check warning on line 247 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L247

Added line #L247 was not covered by tests

from <- format (ended_at - 60 * 60 * 24 * 365 * nyears, "%Y-%m-%dT%H:%M:%S")
ended_at <- format (ended_at, "%Y-%m-%dT%H:%M:%S")

Check warning on line 250 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L249-L250

Added lines #L249 - L250 were not covered by tests

after_txt <- ""
if (!is.null (end_cursor)) {
after_txt <- paste0 (", after:\"", end_cursor, "\"")

Check warning on line 254 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L252-L254

Added lines #L252 - L254 were not covered by tests
}

q <- paste0 ("{
user(login:\"", login, "\") {
login
contributionsCollection (from: \"", from, "\", to: \"", ended_at, "\") {
startedAt
endedAt
issueContributions (first: ", n_per_page, after_txt, ") {
pageInfo {
hasNextPage
endCursor
}
totalCount
nodes {
issue {
createdAt
closedAt
number
comments {
totalCount
}
participants {
totalCount
}
repository {
nameWithOwner
languages (first: ", n_per_page, ") {
totalCount
edges {
size
node {
name
}
}
}
}
}
}
}
}
}
}
}")

Check warning on line 298 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L257-L298

Added lines #L257 - L298 were not covered by tests

return (q)

Check warning on line 300 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L300

Added line #L300 was not covered by tests
}

gh_user_contrib_collect_issues_internal <- function (login,
ended_at = Sys.time (),
nyears = 1,
n_per_page = 100L) {

is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true"
n_per_page <- n_per_page_in_tests (n_per_page)

Check warning on line 309 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L308-L309

Added lines #L308 - L309 were not covered by tests

created_at <- closed_at <- org_repo <- issue_num <- end_cursor <-
num_issue_comments <- num_issue_participants <- num_repo_languages <- NULL
repo_languages <- list ()
total_issue_contribs <- 0L
has_next_page <- TRUE

Check warning on line 315 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L311-L315

Added lines #L311 - L315 were not covered by tests

while (has_next_page) {

Check warning on line 317 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L317

Added line #L317 was not covered by tests

q <- gh_user_contrib_collect_issues_qry (
login = login,
ended_at = ended_at,
nyears = nyears,
n_per_page = n_per_page,
end_cursor = end_cursor
)
dat <- gh::gh_gql (query = q)

Check warning on line 326 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L319-L326

Added lines #L319 - L326 were not covered by tests

collection <- dat$data$user$contributionsCollection
collection_started_at <- collection$startedAt
collection_ended_at <- collection$endedAt

Check warning on line 330 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L328-L330

Added lines #L328 - L330 were not covered by tests

has_next_page <- collection$issueContributions$pageInfo$hasNextPage
end_cursor <- collection$issueContributions$pageInfo$endCursor

Check warning on line 333 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L332-L333

Added lines #L332 - L333 were not covered by tests

total_issue_contribs <-
total_issue_contribs + collection$issueContributions$totalCount
issues <- collection$issueContributions$nodes

Check warning on line 337 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L335-L337

Added lines #L335 - L337 were not covered by tests

created_at <- c (
created_at,
vapply (issues, function (i) i$issue$createdAt, character (1L))
)
closed_at <- c (
closed_at,
vapply (
issues,
function (i) null2na_char (i$issue$closedAt),
character (1L)
)
)
org_repo <- c (
org_repo,
vapply (
issues,
function (i) i$issue$repository$nameWithOwner,
character (1L)
)
)
issue_num <- c (
issue_num,
vapply (issues, function (i) i$issue$number, integer (1L))
)
num_issue_comments <- c (
num_issue_comments,
vapply (
issues,
function (i) i$issue$comments$totalCount,
integer (1L)
)
)
num_issue_participants <- c (
num_issue_participants,
vapply (
issues,
function (i) i$issue$participants$totalCount,
integer (1L)
)
)
num_repo_languages <- c (
num_repo_languages,
vapply (
issues,
function (i) i$issue$repository$languages$totalCount,
integer (1L)
)
)

Check warning on line 386 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L339-L386

Added lines #L339 - L386 were not covered by tests

repo_languages <- c (repo_languages, lapply (issues, function (i) {
langs <- i$issue$repository$languages$edges
names <- vapply (langs, function (j) j$node$name, character (1L))
sizes <- vapply (langs, function (j) j$size, integer (1L))
data.frame (name = names, size = sizes)
}))
}

Check warning on line 394 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L388-L394

Added lines #L388 - L394 were not covered by tests

res <- data.frame (
opened_at = created_at,
closed_at = closed_at,
org_repo = org_repo,
issue_num = issue_num,
num_issue_comments = num_issue_comments,
num_issue_participants = num_issue_participants,
num_repo_languages = num_repo_languages,
repo_languages = I (repo_languages)
)

Check warning on line 405 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L396-L405

Added lines #L396 - L405 were not covered by tests

attr (res, "started_at") <- collection_started_at
attr (res, "ended_at") <- collection_ended_at

Check warning on line 408 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L407-L408

Added lines #L407 - L408 were not covered by tests

return (res)

Check warning on line 410 in R/cm-data-gh-user.R

View check run for this annotation

Codecov / codecov/patch

R/cm-data-gh-user.R#L410

Added line #L410 was not covered by tests
}
gh_user_contrib_collect_issues <-
memoise::memoise (gh_user_contrib_collect_issues_internal)
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/repometrics",
"issueTracker": "https://github.com/ropensci-review-tools/repometrics/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.2.045",
"version": "0.1.2.046",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down

0 comments on commit 96060e7

Please sign in to comment.