Skip to content

Commit

Permalink
Prettify GraphQL query for commits, remove unncecessary authors query.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekbanas committed Nov 12, 2024
1 parent 18182ac commit a88c316
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 44 deletions.
20 changes: 10 additions & 10 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,18 @@ EngineGraphQLGitHub <- R6::R6Class(
repo,
since,
until,
commits_cursor = "",
author_id = "") {
commits_by_org_query <- self$gql_query$commits_by_repo(
org = org,
repo = repo,
since = date_to_gts(since),
until = date_to_gts(until),
commits_cursor = commits_cursor,
author_id = author_id
commits_cursor = "") {
commits_by_org_query <- self$gql_query$commits_from_repo(
commits_cursor = commits_cursor
)
response <- self$gql_response(
gql_query = commits_by_org_query
gql_query = commits_by_org_query,
vars = list(
"org" = org,
"repo" = repo,
"since" = date_to_gts(since),
"until" = date_to_gts(until)
)
)
return(response)
},
Expand Down
33 changes: 10 additions & 23 deletions R/GQLQueryGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,21 @@ GQLQueryGitHub <- R6::R6Class("GQLQueryGitHub",
},

#' @description Prepare query to get commits on GitHub.
#' @param org A GitHub organization.
#' @param repo Name of a repository.
#' @param since Git Time Stamp of starting date of commits.
#' @param until Git Time Stamp of end date of commits.
#' @param commits_cursor An endCursor.
#' @param author_id An Id of an author.
#' @return A query.
commits_by_repo = function(org,
repo,
since,
until,
commits_cursor = "",
author_id = "") {
if (nchar(author_id) == 0) {
author_filter <- author_id
} else {
author_filter <- paste0('author: { id: "', author_id, '"}')
}

paste0('{
repository(name: "', repo, '", owner: "', org, '") {
commits_from_repo = function(commits_cursor = "") {
paste0('
query GetCommitsFromRepo($repo: String!
$org: String!
$since: GitTimestamp
$until: GitTimestamp){
repository(name: $repo, owner: $org) {
defaultBranchRef {
target {
... on Commit {
history(since: "', since, '"
until: "', until, '"
', private$add_cursor(commits_cursor), "
", author_filter, ") {
history(since: $since
until: $until
', private$add_cursor(commits_cursor), ") {
pageInfo {
hasNextPage
endCursor
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/get_commits-GitHub.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# commits_by_repo GitHub query is built properly

Code
gh_commits_by_repo_query
gh_commits_from_repo_query
Output
[1] "{\n repository(name: \"GitStats\", owner: \"r-world-devs\") {\n defaultBranchRef {\n target {\n ... on Commit {\n history(since: \"2023-01-01T00:00:00Z\"\n until: \"2023-02-28T00:00:00Z\"\n \n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n ... on Commit {\n id\n committed_date: committedDate\n author {\n name\n user {\n name\n login\n }\n }\n additions\n deletions\n repository {\n url\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }"
[1] "\n query GetCommitsFromRepo($repo: String!\n $org: String!\n $since: GitTimestamp\n $until: GitTimestamp){\n repository(name: $repo, owner: $org) {\n defaultBranchRef {\n target {\n ... on Commit {\n history(since: $since\n until: $until\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n ... on Commit {\n id\n committed_date: committedDate\n author {\n name\n user {\n name\n login\n }\n }\n additions\n deletions\n repository {\n url\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }"

12 changes: 3 additions & 9 deletions tests/testthat/test-get_commits-GitHub.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
test_that("commits_by_repo GitHub query is built properly", {
gh_commits_by_repo_query <-
test_gqlquery_gh$commits_by_repo(
org = "r-world-devs",
repo = "GitStats",
since = "2023-01-01T00:00:00Z",
until = "2023-02-28T00:00:00Z"
)
gh_commits_from_repo_query <-
test_gqlquery_gh$commits_from_repo()
expect_snapshot(
gh_commits_by_repo_query
gh_commits_from_repo_query
)
test_mocker$cache(gh_commits_by_repo_query)
})

test_that("`get_commits_page_from_repo()` pulls commits page from repository", {
Expand Down

0 comments on commit a88c316

Please sign in to comment.