Skip to content

Commit

Permalink
Merge pull request #543 from r-world-devs/devel
Browse files Browse the repository at this point in the history
Patch release: 2.1.2
  • Loading branch information
maciekbanas authored Nov 13, 2024
2 parents 45a26d6 + 95d6300 commit 87cbfed
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 114 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: GitStats
Title: Get Statistics from 'GitHub' and 'GitLab'
Version: 2.1.1
Title: Standardized Git Repository Data
Version: 2.1.2
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"),
person(given = "Karolina", family = "Marcinkowska", email = "[email protected]", role = "aut"),
person(given = "Matt", family = "Secrest", email = "[email protected]", role = "aut")
)
Description: Obtain statistics in a standardized way from multiple 'Git' services: 'GitHub' and 'GitLab' for the time-being.
Its main purpose is to help teams, whose activities are spread across multiple git platforms, get their repository metadata
in a standardized way from all these platforms.
Description: Obtain standardized data from multiple 'Git' services, including 'GitHub' and 'GitLab'.
Designed to be 'Git' service-agnostic, this package assists teams with activities spread across various
'Git' platforms by providing a unified way to access repository data.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE, r6 = TRUE)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# GitStats 2.1.2

This is a patch release which introduces some hot fixes and new data in `get_commits()` output.

- 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)).

# GitStats 2.1.1

This is a patch release which introduces some improvements in `get_R_package_usage()` on speed and possibility to pull at once data on multiple R packages, new `get_storage()` function and some fixes for checking token scopes and setting hosts.
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
11 changes: 8 additions & 3 deletions R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ EngineRestGitHub <- R6::R6Class(
},

#' Pull all repositories URLS from organization
get_repos_urls = function(type, org) {
repos_urls <- self$response(
get_repos_urls = function(type, org, repos) {
repos_response <- self$response(
endpoint = paste0(private$endpoints[["organizations"]], org, "/repos")
) %>%
)
if (!is.null(repos)) {
repos_response <- repos_response %>%
purrr::keep(~ .$name %in% repos)
}
repos_urls <- repos_response %>%
purrr::map_vec(function(repository) {
if (type == "api") {
repository$url
Expand Down
16 changes: 11 additions & 5 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ EngineRestGitLab <- R6::R6Class(
"languages" = paste0(project$languages, collapse = ", "),
"issues_open" = project$issues_open,
"issues_closed" = project$issues_closed,
"organization" = project$namespace$path,
"organization" = project$namespace$full_path,
"repo_url" = project$web_url
)
}
Expand Down 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 All @@ -166,10 +167,15 @@ EngineRestGitLab <- R6::R6Class(
},

# Pull all repositories URLs from organization
get_repos_urls = function(type, org) {
repos_urls <- self$response(
get_repos_urls = function(type, org, repos) {
repos_response <- self$response(
endpoint = paste0(private$endpoints[["organizations"]], utils::URLencode(org, reserved = TRUE), "/projects")
) %>%
)
if (!is.null(repos)) {
repos_response <- repos_response %>%
purrr::keep(~ .$path %in% repos)
}
repos_urls <- repos_response %>%
purrr::map_vec(function(project) {
if (type == "api") {
project$`_links`$self
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
9 changes: 6 additions & 3 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,10 @@ GitHost <- R6::R6Class(
set_default_token = function(verbose) {
primary_token_name <- private$token_name
token <- Sys.getenv(primary_token_name)
if (private$test_token(token) && verbose) {
cli::cli_alert_info("Using PAT from {primary_token_name} envar.")
if (private$test_token(token)) {
if (verbose) {
cli::cli_alert_info("Using PAT from {primary_token_name} envar.")
}
} else {
pat_names <- names(Sys.getenv()[grepl(primary_token_name, names(Sys.getenv()))])
possible_tokens <- pat_names[pat_names != primary_token_name]
Expand Down Expand Up @@ -698,7 +700,8 @@ GitHost <- R6::R6Class(
}
repos_urls <- rest_engine$get_repos_urls(
type = type,
org = org
org = org,
repos = private$set_repos(org)
)
return(repos_urls)
}, .progress = progress) %>%
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
22 changes: 2 additions & 20 deletions R/GitHostGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,9 @@ GitHostGitLab <- R6::R6Class("GitHostGitLab",
)
},

# check token scopes
# response parameter only for need of super method
# An empty method to fullfill call from super class.
check_token_scopes = function(response = NULL, token) {
private$token_scopes <- try({
httr2::request(private$endpoints$tokens) %>%
httr2::req_headers("Authorization" = paste0("Bearer ", token)) %>%
httr2::req_perform() %>%
httr2::resp_body_json() %>%
purrr::keep(~ .$active) %>%
purrr::map(function(pat) {
data.frame(scopes = unlist(pat$scopes), date = pat$last_used_at)
}) %>%
purrr::list_rbind() %>%
dplyr::filter(
date == max(date)
) %>%
dplyr::select(scopes) %>%
unlist()
},
silent = TRUE)
any(private$access_scopes %in% private$token_scopes)
TRUE
},

# Add `api_url` column to table.
Expand Down
2 changes: 1 addition & 1 deletion R/GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ GitStats <- R6::R6Class(
progress = progress
)
} else if (!is.null(with_files)) {
privater$get_repos_from_host_with_files(
private$get_repos_from_host_with_files(
host = host,
add_contributors = add_contributors,
with_files = with_files,
Expand Down
4 changes: 3 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ With GitStats you can pull git data in a uniform way (table format) from GitHub

## Installation

```r
From CRAN:

```r
install.packages("GitStats")
```

Or development version:

```r
devtools::install_github("r-world-devs/GitStats")
```

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ GitHub and GitLab. For the time-being you can get data on:

## Installation

``` r
From CRAN:

``` r
install.packages("GitStats")
```

Or development version:

``` r
devtools::install_github("r-world-devs/GitStats")
```

Expand Down
27 changes: 27 additions & 0 deletions inst/get_repos_urls_workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,30 @@ test_gitstats <- create_gitstats() |>
)

get_repos_urls(test_gitstats, with_code = "shiny")

# should return 2 repos URLs
create_gitstats() %>%
set_github_host(
repos = c("r-world-devs/GitStats", "r-world-devs/shinyCohortBuilder")
) %>%
get_repos_urls()

# should return 2 repos URLs
create_gitstats() %>%
set_github_host(
repos = c("r-world-devs/GitStats", "r-world-devs/shinyCohortBuilder")
) %>%
get_repos_urls(type = "api")

create_gitstats() %>%
set_gitlab_host(
orgs = "mbtests"
) %>%
get_repos_urls()

# should return 1 repo URL
create_gitstats() %>%
set_gitlab_host(
repos = "mbtests/gitstatstesting"
) %>%
get_repos_urls(type = "api")
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
Loading

0 comments on commit 87cbfed

Please sign in to comment.