Skip to content

Commit

Permalink
Fix get_repos_urls() output when individual repositories are set in…
Browse files Browse the repository at this point in the history
… `set_*_host()`.
  • Loading branch information
maciekbanas committed Oct 31, 2024
1 parent f05f4de commit d438e9d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 22 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.9000
Version: 2.1.1.9001
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"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# GitStats (development version)

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

# GitStats 2.1.1

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
11 changes: 8 additions & 3 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,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
3 changes: 2 additions & 1 deletion R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -700,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
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")
14 changes: 14 additions & 0 deletions tests/testthat/helper-fixtures.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ test_fixtures$github_repositories_rest_response <- list(
test_fixtures$github_repository_rest_response,
list(
"id" = 2222222222222,
"name" = "testRepo2",
"html_url" = "https://testhost.com/test-org/TestRepo",
"url" = "https://testhost.com/api/v4/repos/test-org/TestRepo"
),
list(
"id" = 2222222222222,
"name" = "testRepo3",
"html_url" = "https://testhost.com/test-org/TestRepo",
"url" = "https://testhost.com/api/v4/repos/test-org/TestRepo"
)
Expand All @@ -73,10 +80,17 @@ test_fixtures$github_repositories_rest_response <- list(

test_fixtures$gitlab_repositories_rest_response <- list(
list(
"path" = "testRepo1",
"_links" = list("self" = "https://gitlab.com/api/v4/projects/43400864"),
"web_url" = "https://gitlab.com/mbtests/gitstats-testing-2"
),
list(
"path" = "testRepo2",
"_links" = list("self" = "https://gitlab.com/api/v4/projects/43398933"),
"web_url" = "https://gitlab.com/mbtests/gitstatstesting"
),
list(
"path" = "testRepo3",
"_links" = list("self" = "https://gitlab.com/api/v4/projects/43398933"),
"web_url" = "https://gitlab.com/mbtests/gitstatstesting"
)
Expand Down
30 changes: 25 additions & 5 deletions tests/testthat/test-get_urls_repos-GitHub.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
test_that("get_repos_urls() works", {
test_that("get_repos_urls() works for whole orgs", {
mockery::stub(
test_rest_github$get_repos_urls,
"self$response",
test_fixtures$github_repositories_rest_response
)
gh_repos_urls <- test_rest_github$get_repos_urls(
type = "web",
org = "test-org"
org = "test-org",
repos = NULL
)
expect_gt(
length(gh_repos_urls),
0
expect_length(
gh_repos_urls,
3
)
test_mocker$cache(gh_repos_urls)
})

test_that("get_repos_urls() works for individual repos", {
mockery::stub(
test_rest_github$get_repos_urls,
"self$response",
test_fixtures$github_repositories_rest_response
)
gh_repos_urls <- test_rest_github$get_repos_urls(
type = "web",
org = "test-org",
repos = c("testRepo", "testRepo2")
)
expect_length(
gh_repos_urls,
2
)
test_mocker$cache(gh_repos_urls)
})
Expand All @@ -24,6 +43,7 @@ test_that("get_all_repos_urls prepares api repo_urls vector", {
test_fixtures$github_repositories_rest_response
)
gh_api_repos_urls <- test_rest_github$get_repos_urls(
repos = NULL,
type = "api"
)
expect_gt(length(gh_api_repos_urls), 0)
Expand Down
48 changes: 39 additions & 9 deletions tests/testthat/test-get_urls_repos-GitLab.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
test_that("get_repos_urls() works", {
test_that("get_repos_urls() works for org", {
mockery::stub(
test_rest_gitlab$get_repos_urls,
"self$response",
test_fixtures$gitlab_repositories_rest_response
)
gl_api_repos_urls <- test_rest_gitlab$get_repos_urls(
type = "api",
org = "mbtests"
org = "mbtests",
repos = NULL
)
expect_gt(
length(gl_api_repos_urls),
0
expect_length(
gl_api_repos_urls,
3
)
test_mocker$cache(gl_api_repos_urls)
gl_web_repos_urls <- test_rest_gitlab$get_repos_urls(
type = "web",
org = "mbtests"
org = "mbtests",
repos = NULL
)
expect_gt(
length(gl_web_repos_urls),
0
expect_length(
gl_web_repos_urls,
3
)
test_mocker$cache(gl_web_repos_urls)
})

test_that("get_repos_urls() works for individual repos", {
mockery::stub(
test_rest_gitlab$get_repos_urls,
"self$response",
test_fixtures$gitlab_repositories_rest_response
)
gl_api_repos_urls <- test_rest_gitlab$get_repos_urls(
type = "api",
org = "mbtests",
repos = c("testRepo1", "testRepo2")
)
expect_length(
gl_api_repos_urls,
2
)
test_mocker$cache(gl_api_repos_urls)
gl_web_repos_urls <- test_rest_gitlab$get_repos_urls(
type = "web",
org = "mbtests",
repos = c("testRepo1", "testRepo2")
)
expect_length(
gl_web_repos_urls,
2
)
test_mocker$cache(gl_web_repos_urls)
})
Expand Down

0 comments on commit d438e9d

Please sign in to comment.