From 18182ac1268ba2a9cf31b90df468f723992e9075 Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Tue, 12 Nov 2024 10:50:55 +0000 Subject: [PATCH 1/3] Add new repo_url column to commits table. --- DESCRIPTION | 2 +- NEWS.md | 1 + R/EngineGraphQLGitHub.R | 6 ++++++ R/EngineRestGitLab.R | 3 ++- R/GQLQueryGitHub.R | 3 +++ R/GitHostGitHub.R | 4 +++- tests/testthat/_snaps/get_commits-GitHub.md | 2 +- tests/testthat/helper-expect-responses.R | 2 +- tests/testthat/helper-expect-tables.R | 4 ++-- tests/testthat/helper-fixtures.R | 7 +++++-- 10 files changed, 25 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 38060ea8..87b15594 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GitStats Title: Standardized Git Repository Statistics -Version: 2.1.1.9002 +Version: 2.1.1.9003 Authors@R: c( person(given = "Maciej", family = "Banas", email = "banasmaciek@gmail.com", role = c("aut", "cre")), person(given = "Kamil", family = "Koziej", email = "koziej.k@gmail.com", role = "aut"), diff --git a/NEWS.md b/NEWS.md index 362acee3..b6f7ca36 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # GitStats (development version) +- Added `repo_url` column to output of `get_commits()` function ([#535](https://github.com/r-world-devs/GitStats/issues/535)). - Fixed setting default tokens when `verbose` mode is set to `FALSE` ([#525](https://github.com/r-world-devs/GitStats/issues/525)) and fixed checking token scopes for GitLab ([#526](https://github.com/r-world-devs/GitStats/issues/526)). - Fixed `get_repos_urls()` output when individual repositories are set in `set_*_host()`([#529](https://github.com/r-world-devs/GitStats/issues/529)). Earlier the function pulled all repositories for an organization, even though, repositories were defined for the host, not whole organizations. This is similar to the solved earlier ([#439](https://github.com/r-world-devs/GitStats/issues/439)). - Fixed getting GitLab subgroups as organizations in repositories table output when pulling repositories with code ([#531](https://github.com/r-world-devs/GitStats/issues/531)). diff --git a/R/EngineGraphQLGitHub.R b/R/EngineGraphQLGitHub.R index aa20a36f..fce1bc26 100644 --- a/R/EngineGraphQLGitHub.R +++ b/R/EngineGraphQLGitHub.R @@ -143,6 +143,8 @@ EngineGraphQLGitHub <- R6::R6Class( NA } commit$node$committed_date <- gts_to_posixt(commit$node$committed_date) + commit$node$repo_url <- commit$node$repository$url + commit$node$repository <- NULL commit$node }) commits_row$repository <- repo_name @@ -159,6 +161,10 @@ EngineGraphQLGitHub <- R6::R6Class( dplyr::relocate( any_of(c("author_login", "author_name")), .after = author + ) %>% + dplyr::relocate( + repo_url, + .before = api_url ) } return(commits_table) diff --git a/R/EngineRestGitLab.R b/R/EngineRestGitLab.R index 4f73d640..1324df50 100644 --- a/R/EngineRestGitLab.R +++ b/R/EngineRestGitLab.R @@ -142,7 +142,8 @@ EngineRestGitLab <- R6::R6Class( replacement = "", x = gsub(paste0("(.*)", org, "/"), "", commit$web_url) ), - "organization" = org + "organization" = org, + "repo_url" = stringr::str_match(commit$web_url, "(.*)/-/commit/.*")[2] ) }) }) diff --git a/R/GQLQueryGitHub.R b/R/GQLQueryGitHub.R index 555d3269..439065d8 100644 --- a/R/GQLQueryGitHub.R +++ b/R/GQLQueryGitHub.R @@ -147,6 +147,9 @@ GQLQueryGitHub <- R6::R6Class("GQLQueryGitHub", } additions deletions + repository { + url + } } } } diff --git a/R/GitHostGitHub.R b/R/GitHostGitHub.R index f4e570df..4b0089c3 100644 --- a/R/GitHostGitHub.R +++ b/R/GitHostGitHub.R @@ -188,7 +188,9 @@ GitHostGitHub <- R6::R6Class( until = until, progress = progress ) %>% - graphql_engine$prepare_commits_table(org) + graphql_engine$prepare_commits_table( + org = org + ) return(commits_table_org) }, .progress = if (private$scan_all && progress) { "[GitHost:GitHub] Pulling commits..." diff --git a/tests/testthat/_snaps/get_commits-GitHub.md b/tests/testthat/_snaps/get_commits-GitHub.md index 637aace0..8a2b9065 100644 --- a/tests/testthat/_snaps/get_commits-GitHub.md +++ b/tests/testthat/_snaps/get_commits-GitHub.md @@ -3,5 +3,5 @@ Code gh_commits_by_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 }\n }\n }\n }\n }\n }\n }\n }\n }" + [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 }" diff --git a/tests/testthat/helper-expect-responses.R b/tests/testthat/helper-expect-responses.R index a0590472..32dba117 100644 --- a/tests/testthat/helper-expect-responses.R +++ b/tests/testthat/helper-expect-responses.R @@ -112,7 +112,7 @@ expect_gh_commit_gql_response <- function(object) { ) expect_list_contains( object$node, - c("id", "committed_date", "author", "additions", "deletions") + c("id", "committed_date", "author", "additions", "deletions", "repository") ) } diff --git a/tests/testthat/helper-expect-tables.R b/tests/testthat/helper-expect-tables.R index 74b555b9..12043a2e 100644 --- a/tests/testthat/helper-expect-tables.R +++ b/tests/testthat/helper-expect-tables.R @@ -40,12 +40,12 @@ expect_commits_table <- function(get_commits_object, with_stats = TRUE, exp_auth commit_cols <- if (exp_author) { c( "id", "committed_date", "author", "author_login", "author_name", "additions", "deletions", - "repository", "organization", "api_url" + "repository", "organization", "repo_url", "api_url" ) } else { c( "id", "committed_date", "author", "additions", "deletions", - "repository", "organization", "api_url" + "repository", "organization", "repo_url", "api_url" ) } expect_s3_class(get_commits_object, "data.frame") diff --git a/tests/testthat/helper-fixtures.R b/tests/testthat/helper-fixtures.R index bf7a473a..5fc10365 100644 --- a/tests/testthat/helper-fixtures.R +++ b/tests/testthat/helper-fixtures.R @@ -257,7 +257,10 @@ github_commit_edge <- list( ) ), "additions" = 5L, - "deletions" = 8L + "deletions" = 8L, + "repository" = list( + "url" = "test_url" + ) ) ) @@ -294,7 +297,7 @@ gitlab_commit <- list( "committed_date" = "2023-04-05T12:07:50.000+00:00", "trailers" = list(), "extedned_trailers" = list(), - "web_url" = "https://test_url.com", + "web_url" = "https://test_url.com/-/commit/sxsxsxsx", "stats" = list( "additions" = 1L, "deletions" = 0L, From a88c316cf42a4455703e46cc8268d820a8819881 Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Tue, 12 Nov 2024 11:09:48 +0000 Subject: [PATCH 2/3] Prettify GraphQL query for commits, remove unncecessary authors query. --- R/EngineGraphQLGitHub.R | 20 ++++++------- R/GQLQueryGitHub.R | 33 +++++++-------------- tests/testthat/_snaps/get_commits-GitHub.md | 4 +-- tests/testthat/test-get_commits-GitHub.R | 12 ++------ 4 files changed, 25 insertions(+), 44 deletions(-) diff --git a/R/EngineGraphQLGitHub.R b/R/EngineGraphQLGitHub.R index fce1bc26..f75f827a 100644 --- a/R/EngineGraphQLGitHub.R +++ b/R/EngineGraphQLGitHub.R @@ -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) }, diff --git a/R/GQLQueryGitHub.R b/R/GQLQueryGitHub.R index 439065d8..2c465b67 100644 --- a/R/GQLQueryGitHub.R +++ b/R/GQLQueryGitHub.R @@ -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 diff --git a/tests/testthat/_snaps/get_commits-GitHub.md b/tests/testthat/_snaps/get_commits-GitHub.md index 8a2b9065..2f631896 100644 --- a/tests/testthat/_snaps/get_commits-GitHub.md +++ b/tests/testthat/_snaps/get_commits-GitHub.md @@ -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 }" diff --git a/tests/testthat/test-get_commits-GitHub.R b/tests/testthat/test-get_commits-GitHub.R index c8b4e6e3..65433a53 100644 --- a/tests/testthat/test-get_commits-GitHub.R +++ b/tests/testthat/test-get_commits-GitHub.R @@ -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", { From b1f30a0c1c2ac41a1af3fddacd891a320005fa7a Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Tue, 12 Nov 2024 12:07:02 +0000 Subject: [PATCH 3/3] Try adjust test to GH Actions where 'Couldn't resolve host name [wrong.url]: Could not resolve host: wrong.url' is run instead of simple 'Could not resolve host: wrong.url' which shows locally. --- tests/testthat/_snaps/set_host.md | 9 --------- tests/testthat/test-set_host.R | 4 ++-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/testthat/_snaps/set_host.md b/tests/testthat/_snaps/set_host.md index e3e8c181..48d069fd 100644 --- a/tests/testthat/_snaps/set_host.md +++ b/tests/testthat/_snaps/set_host.md @@ -80,15 +80,6 @@ i Check if you use correct token. ! Scope that is needed: [read_api]. ---- - - Code - create_gitstats() %>% set_github_host(host = "wrong.url", orgs = c("openpharma", - "r_world_devs")) - Condition - Error: - ! Could not resolve host: wrong.url - # Error pops out, when two clients of the same url api are passed as input Code diff --git a/tests/testthat/test-set_host.R b/tests/testthat/test-set_host.R index 795c0cd4..85069b3a 100644 --- a/tests/testthat/test-set_host.R +++ b/tests/testthat/test-set_host.R @@ -113,14 +113,14 @@ test_that("Error shows, when wrong input is passed when setting connection and h token = Sys.getenv("GITLAB_PAT_PUBLIC") ) ) - expect_snapshot({ + expect_error({ create_gitstats() %>% set_github_host( host = "wrong.url", orgs = c("openpharma", "r_world_devs") ) }, - error = TRUE + "Could not resolve host." ) })