Skip to content

Commit

Permalink
Merge pull request #536 from r-world-devs/535-add-identification-of-r…
Browse files Browse the repository at this point in the history
…epository-to-commits-table

535 add identification of repository to commits table
  • Loading branch information
maciekbanas authored Nov 12, 2024
2 parents 01f3285 + b1f30a0 commit 7c74303
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 63 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut", "cre")),
person(given = "Kamil", family = "Koziej", email = "[email protected]", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)).
Expand Down
26 changes: 16 additions & 10 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -386,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
3 changes: 2 additions & 1 deletion R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
})
})
Expand Down
36 changes: 13 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 All @@ -147,6 +134,9 @@ GQLQueryGitHub <- R6::R6Class("GQLQueryGitHub",
}
additions
deletions
repository {
url
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion R/GitHostGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
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 }\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 }"

9 changes: 0 additions & 9 deletions tests/testthat/_snaps/set_host.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper-expect-responses.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-expect-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/helper-fixtures.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ github_commit_edge <- list(
)
),
"additions" = 5L,
"deletions" = 8L
"deletions" = 8L,
"repository" = list(
"url" = "test_url"
)
)
)

Expand Down Expand Up @@ -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,
Expand Down
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
4 changes: 2 additions & 2 deletions tests/testthat/test-set_host.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
})

Expand Down

0 comments on commit 7c74303

Please sign in to comment.