Skip to content

Commit

Permalink
Merge pull request #268 from r-world-devs/120-set-default-token
Browse files Browse the repository at this point in the history
120 set default token
  • Loading branch information
maciekbanas authored Aug 10, 2023
2 parents 1d1232a + f4915d3 commit 00854e9
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 9 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Suggests:
knitr,
mockery,
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GitStats 0.1.0.9000

- 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 switching to REST engine in case GraphQL fails with 502 error (I: #225 PRs: #227 (for repos) #261 (for commits))
- added GraphQL engine for getting GitLab repos by organization (I: #218 PR: #233)
Expand Down
45 changes: 45 additions & 0 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ GitHost <- R6::R6Class("GitHost",
token = NA,
api_url = NA) {
private$api_url <- api_url
if (is.null(token)){
token <- private$set_default_token()
}
if (grepl("https://", api_url) && grepl("github", api_url)) {
private$engines$rest <- EngineRestGitHub$new(
token = token,
Expand Down Expand Up @@ -190,6 +193,48 @@ GitHost <- R6::R6Class("GitHost",
# @field engines A placeholder for REST and GraphQL Engine classes.
engines = list(),

# @description Set default token if none exists.
set_default_token = function() {
if (grepl("github", private$api_url)) {
primary_token_name <- "GITHUB_PAT"
} else {
primary_token_name <- "GITLAB_PAT"
}
token <- Sys.getenv(primary_token_name)
if (private$test_token(token)) {
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]
for (token_name in possible_tokens) {
if (private$test_token(Sys.getenv(token_name))) {
token <- Sys.getenv(token_name)
cli::cli_alert_info("Using PAT from {token_name} envar.")
break
}
}
}
return(token)
},

# @description Helper to test if a token works
test_token = function(token) {
response <- NULL
test_endpoint <- if (grepl("github", private$api_url)) {
private$api_url
} else {
paste0(private$api_url, "/projects")
}
try(response <- httr2::request(test_endpoint) %>%
httr2::req_headers("Authorization" = paste0("Bearer ", token)) %>%
httr2::req_perform(), silent = TRUE)
if (!is.null(response)) {
TRUE
} else {
FALSE
}
},

# @description GraphQL url handler (if not provided).
# @param rest_api_url REST API url.
# @return GraphQL API url.
Expand Down
3 changes: 1 addition & 2 deletions R/gitstats_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ create_gitstats <- function() {
#' my_gitstats <- create_gitstats() %>%
#' set_connection(
#' api_url = "https://api.github.com",
#' token = Sys.getenv("GITHUB_PAT"),
#' orgs = c("r-world-devs", "openpharma", "pharmaverse")
#' ) %>%
#' set_connection(
Expand All @@ -34,7 +33,7 @@ create_gitstats <- function() {
#' @export
set_connection <- function(gitstats_obj,
api_url,
token,
token = NULL,
orgs) {
gitstats_obj$add_host(
api_url = api_url,
Expand Down
14 changes: 14 additions & 0 deletions R/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ TestEngineRest <- R6::R6Class("TestEngineRest",
}
)
)

#' @noRd
create_testrest <- function(rest_api_url = "https://api.github.com",
token,
mode = "") {
test_rest <- TestEngineRest$new(
token = token,
rest_api_url = rest_api_url
)
if (mode == "private") {
test_rest <- environment(test_rest$initialize)$private
}
return(test_rest)
}
5 changes: 2 additions & 3 deletions devel/example_workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ pkgload::load_all()
git_stats <- create_gitstats() %>%
set_connection(
api_url = "https://api.github.com",
token = Sys.getenv("GITHUB_PAT"),
orgs = c("r-world-devs", "openpharma")
) %>%
set_connection(
api_url = "https://gitlab.com/api/v4",
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = c("mbtests", "gitlab-org")
)

Expand All @@ -19,7 +17,8 @@ git_stats
# examples for getting repos (default search parameter is 'org')
get_repos(git_stats)
add_repos_contributors(git_stats)
git_stats$show_repos()
dplyr::glimpse(git_stats$show_repos())

get_repos(git_stats, add_contributors = TRUE)

get_commits(git_stats, date_from = "2022-01-01")
Expand Down
3 changes: 1 addition & 2 deletions man/set_connection.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/GitHost.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# `set_default_token` sets default token for public GitHub

Code
default_token <- test_host$set_default_token()
Message <cliMessage>
i Using PAT from GITHUB_PAT envar.

# `set_default_token` sets default token for GitLab

Code
withr::with_envvar(new = c(GITLAB_PAT = Sys.getenv("GITLAB_PAT_PUBLIC")), {
default_token <- test_gl_host$set_default_token()
})
Message <cliMessage>
i Using PAT from GITLAB_PAT envar.

# GitHost filters GitHub repositories' (pulled by org) table by languages

Code
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/set_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
Message <cliMessage>
v Set connection to GitLab.

# When empty token for GitHub, GitStats pulls default token

Code
test_gitstats <- create_gitstats() %>% set_connection(api_url = "https://api.github.com",
orgs = c("openpharma", "r-world-devs"))
Message <cliMessage>
i Using PAT from GITHUB_PAT envar.
v Set connection to GitHub.

# When empty token for GitLab, GitStats pulls default token

Code
withr::with_envvar(new = c(GITLAB_PAT = Sys.getenv("GITLAB_PAT_PUBLIC")), {
test_gitstats <- create_gitstats() %>% set_connection(api_url = "https://gitlab.com/api/v4",
orgs = "mbtests")
})
Message <cliMessage>
i Using PAT from GITLAB_PAT envar.
v Set connection to GitLab.

# Warning shows if organizations are not specified and host is not passed

Code
Expand Down
56 changes: 55 additions & 1 deletion tests/testthat/test-GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,67 @@ test_host <- create_testhost(
mode = "private"
)

test_that("`set_gql_url()` correctly sets gql api url - for public and private github", {
test_that("`set_default_token` sets default token for public GitHub", {
expect_snapshot(
default_token <- test_host$set_default_token()
)
test_rest <- create_testrest(token = default_token,
mode = "private")
expect_equal(
test_rest$perform_request(
endpoint = "https://api.github.com",
token = default_token
)$status,
200
)
})

test_that("`set_default_token` sets default token for GitLab", {
test_gl_host <- create_testhost(
api_url = "https://gitlab.com/api/v4",
token = Sys.getenv("GITLAB_PAT_PUBLIC"),
orgs = c("openpharma", "r-world-devs"),
mode = "private"
)
expect_snapshot(
withr::with_envvar(new = c("GITLAB_PAT" = Sys.getenv("GITLAB_PAT_PUBLIC")), {
default_token <- test_gl_host$set_default_token()
})
)
test_rest <- create_testrest(token = default_token,
mode = "private")
expect_equal(
test_rest$perform_request(
endpoint = "https://gitlab.com/api/v4/projects",
token = default_token
)$status,
200
)
})

test_that("`test_token` works properly", {
expect_true(
test_host$test_token(Sys.getenv("GITHUB_PAT"))
)
expect_false(
test_host$test_token("false_token")
)
})

test_that("`set_gql_url()` correctly sets gql api url - for public GitHub", {
expect_equal(
test_host$set_gql_url("https://api.github.com"),
"https://api.github.com/graphql"
)
})

test_that("`set_gql_url()` correctly sets gql api url - for public GitLab", {
expect_equal(
test_host$set_gql_url("https://gitlab.com/api/v4"),
"https://gitlab.com/api/graphql"
)
})

test_that("GitHost filters GitHub repositories' (pulled by org) table by languages", {
repos_table <- test_mocker$use("gh_repos_table")
expect_snapshot(
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-set_connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ test_that("Set connection returns appropriate messages", {
)
})

test_that("When empty token for GitHub, GitStats pulls default token", {
expect_snapshot(
test_gitstats <- create_gitstats() %>%
set_connection(
api_url = "https://api.github.com",
orgs = c("openpharma", "r-world-devs")
)
)
})

test_that("When empty token for GitLab, GitStats pulls default token", {
expect_snapshot(
withr::with_envvar(new = c("GITLAB_PAT" = Sys.getenv("GITLAB_PAT_PUBLIC")), {
test_gitstats <- create_gitstats() %>%
set_connection(
api_url = "https://gitlab.com/api/v4",
orgs = "mbtests"
)
})
)
})

test_that("Warning shows if organizations are not specified and host is not passed", {
test_gitstats <- create_gitstats()
expect_snapshot(
Expand Down

0 comments on commit 00854e9

Please sign in to comment.