Skip to content

Commit

Permalink
Merge pull request #284 from r-world-devs/176-manage-subgroups-in-gitlab
Browse files Browse the repository at this point in the history
Handle GitLab subgroups and add `show_orgs` function.
  • Loading branch information
maciekbanas authored Sep 5, 2023
2 parents edc0cb7 + a408bf0 commit a0fe7cf
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 9 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: Get Statistics from Git Hosting Services
Version: 0.1.0.9001
Version: 0.1.0.9002
Authors@R: c(
person(given = "Maciej", family = "Banaś", 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 NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(reset_language)
export(set_host)
export(set_team_member)
export(setup)
export(show_orgs)
importFrom(R6,R6Class)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_danger)
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
GitStats 0.1.0.9001

GitStats 0.1.0.9002

- set new name for `set_connection` function: `set_host` as it is more informative (and shorter) (I: #271),
- changed name of a function: `add_team_member` to `set_team_member` (I: #271),
- added setting tokens by default - if a user does have all the PATs set up in environment variables (as e.g. `GITHUB_PAT` or `GITLAB_PAT`), there is no need to pass them as an arugment to `set_connection` (I: #120 PR: #268),
- added `get_users()` function to pull information on users (I: #199 PR: #238),
- added possibility of scanning whole internal git platforms if no `orgs` are passed (I: #258),
- added switching to REST engine in case GraphQL fails with 502 error (I: #225 PRs: #227 (for repos) #261 (for commits))
- added `show_orgs()` function to print all organizations (I: #283),
- added GraphQL engine for getting GitLab repos by organization (I: #218 PR: #233)
- removed `contributors` as basic stat when pulling `repos` by `org` and by `phrase` to improve speed of pulling repositories data. Added `get_repos_contributors()` user function and `add_contributors` parameter to `get_repos()` function to add conditionally information on contributors to repositories table (I: #235 PRs: #243 #264)
- added resetting all settings to default with `reset()` function (I: #270)
- added resetting language in your search preferences with `reset_language()` or setting `language` parameter to `All` in `setup()` function (I: #231 PR: #265)
- OOP optimization: moved method on adding issues do repository table via REST to privates (I: #235 PR: #243)
- handled errors when tokens do not grant access (I: #242 PR: #247)
- in repositories output set `api_url` column as an address to the repo, not the host (I: #201 PR: #249)

- fixed adding GitLab subgroups (I: #176)

GitStats 0.1.0

Expand Down
1 change: 1 addition & 0 deletions R/EngineGraphQLGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
#' @return A table.
get_repos = function(org,
settings) {
org <- gsub("%2f", "/", org)
if (settings$search_param == "org") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_yellow('GraphQL')}][org:{org}] Pulling repositories...")
Expand Down
3 changes: 3 additions & 0 deletions R/EngineRest.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ EngineRest <- R6::R6Class("EngineRest",
check_organizations = function(orgs) {
orgs <- purrr::map(orgs, function(org) {
org_endpoint <- if(grepl("github", self$rest_api_url)) "/orgs/" else "/groups/"
if (grepl("/", org)) {
org <- gsub("/", "%2f", org)
}
withCallingHandlers(
{
self$response(endpoint = paste0(self$rest_api_url, org_endpoint, org))
Expand Down
6 changes: 3 additions & 3 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
settings) {
if (settings$search_param == "phrase") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][phrase:{settings$phrase}][org:{org}] Searching repositories...")
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][phrase:{settings$phrase}][org:{gsub('%2f', '/', org)}] Searching repositories...")
}
repos_table <- private$search_repos_by_phrase(
org = org,
Expand All @@ -25,7 +25,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
private$get_repos_issues()
} else if (settings$search_param == "team") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org}][team:{settings$team_name}] Pulling repositories...")
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}][team:{settings$team_name}] Pulling repositories...")
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
Expand Down Expand Up @@ -53,7 +53,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
repos_table <- NULL
if (settings$search_param == "org") {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{org}] Pulling repositories...")
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{gsub('%2f', '/', org)}] Pulling repositories...")
}
org <- private$get_group_id(org)
repos_table <- private$pull_repos_from_org(org) %>%
Expand Down
12 changes: 11 additions & 1 deletion R/GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ GitStats <- R6::R6Class("GitStats",
return(users_table)
},

#' @description Print organizations.
show_orgs = function() {
purrr::map(private$hosts, function(host) {
orgs <- host$.__enclos_env__$private$orgs
purrr::map_vec(orgs, ~ gsub("%2f", "/", .))
}) %>% unlist()
},

#' @description Print repositories output.
show_repos = function() {
private$repos
Expand Down Expand Up @@ -326,9 +334,11 @@ GitStats <- R6::R6Class("GitStats",
item_to_print = item_to_check) {
if (item_name == "Organisations") {
item_to_print <- unlist(item_to_print)
item_to_print <- purrr::map_vec(item_to_print, function(org) {
gsub("%2f", "/", org)
})
if (length(item_to_print) < 10) {
list_items <- paste0(item_to_print, collapse = ", ")

} else {
item_to_print_cut <- item_to_print[1:10]
list_items <- paste0(item_to_print_cut, collapse = ", ") %>%
Expand Down
12 changes: 12 additions & 0 deletions R/gitstats_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,15 @@ reset_language <- function(gitstats_obj){
cli::cli_alert_info("Setting language parameter to 'All'.")
return(gitstats_obj)
}

#' @title Show organizations
#' @name show_orgs
#' @description Prints organizations downloaded in `GitStats`. Especially
#' helpful when user is scanning whole git platform and want to have a glimpse
#' at organizations.
#' @param gitstats_obj A GitStats object.
#' @return A vector of organizations.
#' @export
show_orgs <- function(gitstats_obj){
return(gitstats_obj$show_orgs())
}
10 changes: 10 additions & 0 deletions devel/example_workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,13 @@ git_stats <- create_gitstats() %>%
orgs = c("mbtests", "public health")
)
git_stats

## add gitlab subgroups

git_stats <- create_gitstats() %>%
set_host(
api_url = "https://gitlab.com/api/v4",
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = "mbtests/subgroup"
)
git_stats
11 changes: 11 additions & 0 deletions man/GitStats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/show_orgs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/_snaps/02-EngineRest.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@
Error <rlang_error>
i No token provided.

# check_organizations returns NULL if orgs are wrong

Code
orgs <- test_rest$check_organizations("does_not_exist")
Message <cliMessage>
x Organization you provided does not exist or its name was passed in a wrong way: does_not_exist
! Please type your organization name as you see it in `url`.
i E.g. do not use spaces. Organization names as you see on the page may differ from their 'address' name.
Message <simpleMessage>
HTTP 404 No such address

15 changes: 15 additions & 0 deletions tests/testthat/_snaps/GitStats.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@
i [GitHub][Engine:REST] Pulling contributors...
i [GitLab][Engine:REST] Pulling contributors...

# subgroups are cleanly printed in GitStats

Code
test_gitstats
Output
A <GitStats> object for 1 hosts:
Hosts: https://gitlab.com/api/v4
Organisations: [1] mbtests/subgroup
Search preference: org
Team: <not defined>
Phrase: <not defined>
Language: All
Repositories output: <not defined>
Commits output: <not defined>

23 changes: 21 additions & 2 deletions tests/testthat/test-02-EngineRest.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ test_rest <- TestEngineRest$new(

test_rest_priv <- environment(test_rest$response)$private



test_that("When token is empty throw error", {
expect_snapshot(
error = TRUE,
Expand Down Expand Up @@ -96,3 +94,24 @@ test_that("`response()` returns commits response from GitLab's REST API", {
)
test_mocker$cache(gl_commits_rest_response_repo_2)
})

test_that("check_organizations returns orgs if they are correct", {
expect_equal(
test_rest$check_organizations("mbtests"),
"mbtests"
)
})

test_that("check_organizations returns orgs if GitLab subroups are passed", {
expect_equal(
test_rest$check_organizations("mbtests/subgroup"),
"mbtests%2fsubgroup"
)
})

test_that("check_organizations returns NULL if orgs are wrong", {
expect_snapshot(
orgs <- test_rest$check_organizations("does_not_exist")
)
expect_null(orgs)
})
28 changes: 28 additions & 0 deletions tests/testthat/test-GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ test_that("Add_repos_contributors adds repos contributors to repos table", {
expect_repos_table_with_contributors(repos_with_contributors)
expect_equal(nrow(repos_without_contributors), nrow(repos_with_contributors))
})

test_that("show_orgs print orgs properly", {
expect_equal(
test_gitstats$show_orgs(),
c("r-world-devs", "openpharma", "mbtests")
)
})

suppressMessages(
test_gitstats <- create_gitstats() %>%
set_host(
api_url = "https://gitlab.com/api/v4",
orgs = "mbtests/subgroup"
)
)

test_that("show_orgs print subgroups properly", {
expect_equal(
test_gitstats$show_orgs(),
"mbtests/subgroup"
)
})

test_that("subgroups are cleanly printed in GitStats", {
expect_snapshot(
test_gitstats
)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-show.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("show_orgs() shows orgs", {
test_gitstats <- create_test_gitstats(hosts = 2)
expect_equal(
show_orgs(test_gitstats),
c("r-world-devs", "openpharma", "mbtests")
)
})

0 comments on commit a0fe7cf

Please sign in to comment.