Skip to content

Commit

Permalink
Merge pull request #304 from r-world-devs/295-add-version-to-repo-api…
Browse files Browse the repository at this point in the history
…-url

295 add version to repo api url
  • Loading branch information
maciekbanas authored Sep 27, 2023
2 parents e28412d + ca370be commit 18e811e
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 61 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.901
Version: 0.1.0.9011
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
4 changes: 0 additions & 4 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
repo$organization <- repo$organization$login
data.frame(repo)
})
repos_table <- dplyr::mutate(
repos_table,
api_url = paste0(gsub("/graphql", "", self$gql_api_url), "/repositories/", organization, "/", name),
)
return(repos_table)
},

Expand Down
8 changes: 2 additions & 6 deletions R/EngineGraphQLGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,10 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
repo$organization <- repo$group$name
repo$group <- NULL
data.frame(repo)
})
repos_table <- dplyr::mutate(
repos_table,
api_url = paste0(gsub("/graphql", "", self$gql_api_url), "/projects/", gsub("gid://gitlab/Project/", "", id))
) %>%
}) %>%
dplyr::relocate(
repo_url,
.before = api_url
.after = organization
)
return(repos_table)
},
Expand Down
4 changes: 1 addition & 3 deletions R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,9 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
"issues_open" = repo$issues_open,
"issues_closed" = repo$issues_closed,
"organization" = repo$owner$login,
"repo_url" = repo$html_url,
"api_url" = repo$url
"repo_url" = repo$html_url
)
})

repos_list
},

Expand Down
3 changes: 1 addition & 2 deletions R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
"issues_open" = project$issues_open,
"issues_closed" = project$issues_closed,
"organization" = project$namespace$path,
"repo_url" = project$web_url,
"api_url" = paste0(self$rest_api_url, "/projects/", project$id)
"repo_url" = project$web_url
)
})
projects_list
Expand Down
23 changes: 19 additions & 4 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,19 @@ GitHost <- R6::R6Class("GitHost",
repos_table <- private$pull_repos_from_orgs(
settings = settings
)

if (settings$search_param == "team") {
add_contributors <- TRUE
}

repos_table <- private$add_repo_api_url(repos_table)
if (add_contributors) {
repos_table <- self$pull_repos_contributors(repos_table)
}

if (nrow(repos_table) > 0 && settings$language != "All") {
repos_table <- private$filter_repos_by_language(
repos_table = repos_table,
language = settings$language
)
}

return(repos_table)
},

Expand Down Expand Up @@ -337,6 +334,24 @@ GitHost <- R6::R6Class("GitHost",
purrr::list_rbind()
},

# add do repos table `api_url` column
add_repo_api_url = function(repos_table){
if (length(repos_table) > 0) {
repos_table <- if (private$host == "GitHub") {
dplyr::mutate(
repos_table,
api_url = paste0(private$api_url, "/repos/", organization, "/", name),
)
} else if (private$host == "GitLab") {
dplyr::mutate(
repos_table,
api_url = paste0(private$api_url, "/projects/", gsub("gid://gitlab/Project/", "", id))
)
}
}
return(repos_table)
},

# @description Filter repositories by contributors.
# @details If at least one member of a team is a contributor than a project
# passes through the filter.
Expand Down
2 changes: 1 addition & 1 deletion R/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TestHost <- R6::R6Class("TestHost",
gql_api_url = private$set_gql_url(api_url)
)
} else if (grepl("https://", api_url) && grepl("gitlab|code", api_url)) {
private$engines$rest <- TestEngineRest$new(
private$engines$rest <- TestEngineRestGitLab$new(
token = token,
rest_api_url = api_url
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/03-EngineRestGitLab.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# `pull_repos_contributors()` adds contributors to repos table

Code
gl_repos_table <- test_rest$pull_repos_contributors(test_mocker$use(
"gl_repos_table"))
gl_repos_table_with_contributors <- test_rest$pull_repos_contributors(
test_mocker$use("gl_repos_table"))
Message
i [GitLab][Engine:REST][org:MB Tests] Pulling contributors...

Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/_snaps/05-GitHost.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@
i [GitHub][Engine:GraphQL][org:openpharma] Pulling repositories...
i [GitHub][Engine:GraphQL][org:r-world-devs] Pulling repositories...

# pull_repos_contributors returns table with contributors
# pull_repos_contributors returns table with contributors for GitHub

Code
repos_table_2 <- test_host$pull_repos_contributors(repos_table_1)
Message
i [GitHub][Engine:REST][org:r-world-devs] Pulling contributors...

# pull_repos_contributors returns table with contributors for GitLab

Code
repos_table_2 <- test_gl_host$pull_repos_contributors(repos_table_1)
Message
i [GitLab][Engine:REST][org:MB Tests] Pulling contributors...

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/06-GitStats.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
Code
test_gitstats$pull_repos_contributors()
Message
i [GitHub][Engine:REST][org:r-world-devs and openpharma] Pulling contributors...
i [GitHub][Engine:REST][org:r-world-devs] Pulling contributors...
i [GitLab][Engine:REST][org:MB Tests] Pulling contributors...

# subgroups are cleanly printed in GitStats
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/07-pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Code
pull_repos_contributors(test_gitstats)
Message
i [GitHub][Engine:REST][org:r-world-devs and openpharma] Pulling contributors...
i [GitHub][Engine:REST][org:r-world-devs] Pulling contributors...
i [GitLab][Engine:REST][org:MB Tests] Pulling contributors...

# pull_users shows error when no hosts are defined
Expand Down
22 changes: 15 additions & 7 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,36 @@ expect_users_table <- function(get_user_object) {
expect_gt(nrow(get_user_object), 1)
}

repo_table_colnames <- c(
"id", "name", "stars", "forks", "created_at",
"last_activity_at", "languages", "issues_open", "issues_closed",
"organization", "repo_url"
)

expect_repos_table_with_contributors <- function(pull_repos_object) {
repo_cols <- c(
"id", "name", "stars", "forks", "created_at",
"last_activity_at", "languages", "issues_open", "issues_closed",
"organization", "repo_url", "api_url", "contributors"
repo_table_colnames, "contributors"
)
expect_s3_class(pull_repos_object, "data.frame")
expect_named(pull_repos_object, repo_cols)
expect_gt(nrow(pull_repos_object), 0)
}

expect_repos_table <- function(pull_repos_object) {
expect_repos_table_with_api_url <- function(pull_repos_object) {
repo_cols <- c(
"id", "name", "stars", "forks", "created_at",
"last_activity_at", "languages", "issues_open", "issues_closed",
"organization", "repo_url", "api_url"
repo_table_colnames, "api_url"
)
expect_s3_class(pull_repos_object, "data.frame")
expect_named(pull_repos_object, repo_cols)
expect_gt(nrow(pull_repos_object), 0)
}

expect_repos_table <- function(pull_repos_object) {
expect_s3_class(pull_repos_object, "data.frame")
expect_named(pull_repos_object, repo_table_colnames)
expect_gt(nrow(pull_repos_object), 0)
}

expect_commits_table <- function(pull_commits_object, with_stats = TRUE) {
commit_cols <- c(
"id", "committed_date", "author", "additions", "deletions",
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-03-EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ test_that("`prepare_repos_table()` prepares repos table", {
gh_repos_by_phrase_table <- test_rest_priv$prepare_repos_table(
repos_list = test_mocker$use("gh_repos_by_phrase_tailored")
)

expect_repos_table(
gh_repos_by_phrase_table
)
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-03-EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,18 @@ test_that("`pull_repos_issues()` adds issues to repos table", {

test_that("`pull_repos_contributors()` adds contributors to repos table", {
expect_snapshot(
gl_repos_table <- test_rest$pull_repos_contributors(
gl_repos_table_with_contributors <- test_rest$pull_repos_contributors(
test_mocker$use("gl_repos_table")
)
)
expect_repos_table_with_contributors(
gl_repos_table
gl_repos_table_with_contributors
)
expect_gt(
length(gl_repos_table$contributors),
length(gl_repos_table_with_contributors$contributors),
0
)
test_mocker$cache(gl_repos_table)
test_mocker$cache(gl_repos_table_with_contributors)
})

test_that("`pull_repos_by_phrase()` works", {
Expand Down Expand Up @@ -233,7 +233,7 @@ test_that("`pull_commits()` works as expected", {
# private methods

test_that("Engine filters GitLab repositories' table by team members", {
gl_repos_table <- test_mocker$use("gl_repos_table")
gl_repos_table <- test_mocker$use("gl_repos_table_with_contributors")

gl_repos_table_team <- test_rest_priv$filter_repos_by_team(
gl_repos_table,
Expand Down
47 changes: 42 additions & 5 deletions tests/testthat/test-05-GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ test_that("GitHost pulls repos from orgs", {
)
})

test_that("GitHost adds `repo_api_url` column to GitHub repos table", {
repos_table <- test_mocker$use("gh_repos_table")
gh_repos_table_with_api_url <- test_host$add_repo_api_url(repos_table)
expect_true(all(grepl("api.github.com", gh_repos_table_with_api_url$api_url)))
test_mocker$cache(gh_repos_table_with_api_url)
})

suppressMessages(
test_gl_host <- GitHost$new(
api_url = "https://gitlab.com/api/v4",
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = "mbtests"
)
)
test_gl_host <- test_gl_host$.__enclos_env__$private

test_that("GitHost adds `repo_api_url` column to GitLab repos table", {
repos_table <- test_mocker$use("gl_repos_table")
gl_repos_table_with_api_url <- test_gl_host$add_repo_api_url(repos_table)
expect_true(all(grepl("gitlab.com/api/v4", gl_repos_table_with_api_url$api_url)))
test_mocker$cache(gl_repos_table_with_api_url)
})

test_that("GitHost filters GitHub repositories' (pulled by org) table by languages", {
repos_table <- test_mocker$use("gh_repos_table")
expect_snapshot(
Expand Down Expand Up @@ -304,22 +327,36 @@ test_that("pull_repos returns table of repositories", {
language = "All")
)
)
expect_repos_table(
expect_repos_table_with_api_url(
repos_table
)
})

test_that("pull_repos_contributors returns table with contributors", {

repos_table_1 <- test_mocker$use("gh_repos_table")
test_that("pull_repos_contributors returns table with contributors for GitHub", {
repos_table_1 <- test_mocker$use("gh_repos_table_with_api_url")
expect_snapshot(
repos_table_2 <- test_host$pull_repos_contributors(repos_table_1)
)
expect_repos_table_with_contributors(repos_table_2)
expect_gt(
length(repos_table_2$contributors),
0
)
expect_equal(nrow(repos_table_1), nrow(repos_table_2))
})

test_that("pull_repos_contributors returns table with contributors for GitLab", {
test_gl_host <- create_testhost(
api_url = "https://gitlab.com/api/v4",
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = c("mbtests")
)
repos_table_1 <- test_mocker$use("gl_repos_table_with_api_url")
expect_snapshot(
repos_table_2 <- test_gl_host$pull_repos_contributors(repos_table_1)
)
expect_gt(
length(repos_table_2$contributors),
0
)
expect_equal(nrow(repos_table_1), nrow(repos_table_2))
})
27 changes: 16 additions & 11 deletions tests/testthat/test-06-GitStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,31 @@ test_that("GitStats get users info", {
)
})

test_gitstats <- create_test_gitstats(hosts = 2)

test_that("GitStats throws error when pull_repos_contributors is run with empty repos field", {

test_gitstats_empty <- create_test_gitstats(hosts = 2)
expect_snapshot_error(
test_gitstats$pull_repos_contributors()
test_gitstats_empty$pull_repos_contributors()
)
})

test_that("Add_repos_contributors adds repos contributors to repos table", {
suppressMessages({
test_gitstats$pull_repos()
})
repos_without_contributors <- test_gitstats$get_repos()
repos_table_without_contributors <- data.table::rbindlist(
list(
test_mocker$use("gh_repos_table_with_api_url"),
test_mocker$use("gl_repos_table_with_api_url")
)
)
test_mocker$cache(repos_table_without_contributors)
test_gitstats <- create_test_gitstats(
hosts = 2,
inject_repos = "repos_table_without_contributors"
)
expect_snapshot(
test_gitstats$pull_repos_contributors()
)
repos_with_contributors <- test_gitstats$get_repos()
expect_repos_table_with_contributors(repos_with_contributors)
expect_equal(nrow(repos_without_contributors), nrow(repos_with_contributors))
repos_table_with_contributors <- test_gitstats$get_repos()
expect_true("contributors" %in% names(repos_table_with_contributors))
expect_equal(nrow(repos_table_without_contributors), nrow(repos_table_with_contributors))
})

test_that("get_orgs print orgs properly", {
Expand Down
12 changes: 7 additions & 5 deletions tests/testthat/test-07-pull.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
test_gitstats <- create_test_gitstats(hosts = 2)

test_that("pull_repos pulls repos in the table format", {
test_gitstats <- create_test_gitstats(hosts = 2)
expect_snapshot(
pull_repos(test_gitstats)
)
repos_table <- test_gitstats$get_repos()
expect_repos_table(repos_table)
test_mocker$cache(repos_table)
expect_repos_table_with_api_url(repos_table)
})

test_that("pull_repos_contributors adds contributors column to repos table", {
test_gitstats <- create_test_gitstats(
hosts = 2,
inject_repos = "repos_table_without_contributors"
)
expect_snapshot(
pull_repos_contributors(
test_gitstats
)
)
repos_table_with_contributors <- test_gitstats$get_repos()
expect_repos_table_with_contributors(repos_table_with_contributors)
expect_true("contributors" %in% names(repos_table_with_contributors))
test_mocker$cache(repos_table_with_contributors)
})

Expand Down
Loading

0 comments on commit 18e811e

Please sign in to comment.