Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting default tokens when verbose mode is FALSE and fix checks for GitLab token scopes #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Statistics
Version: 2.1.1.9000
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"),
person(given = "Karolina", family = "Marcinkowska", email = "karolina_marcinkowska@onet.pl", role = "aut"),
person(given = "Matt", family = "Secrest", email = "secrestm@gene.com", 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 statistics 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 metadata.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE, r6 = TRUE)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +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)).

# 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
6 changes: 4 additions & 2 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
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
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
24 changes: 24 additions & 0 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ test_that("`test_token` works properly", {
)
})

test_that("`test_token` works properly", {
skip_on_cran()
expect_true(
gitlab_testhost_priv$test_token(Sys.getenv("GITLAB_PAT_PUBLIC"))
)
expect_false(
gitlab_testhost_priv$test_token("false_token")
)
})

test_that("`check_token_scopes` works for GitHub", {
skip_on_cran()
github_pat <- Sys.getenv("GITHUB_PAT")
github_response <- httr2::request("https://api.github.com") |>
httr2::req_headers("Authorization" = paste0("Bearer ", github_pat)) |>
httr2::req_perform()
expect_true(
github_testhost_priv$check_token_scopes(
response = github_response,
token = github_pat
)
)
})

test_that("`set_default_token` sets default token for GitLab", {
skip_on_cran()
expect_snapshot(
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-set_host.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,20 @@ test_that("Error pops out when `org` does not exist", {
error = TRUE
)
})

test_that("Setting verbose for set_*_host() to FALSE works fine", {
expect_no_error(
create_gitstats() %>%
set_github_host(
orgs = c("openpharma", "r-world-devs"),
verbose = FALSE
)
)
expect_no_error(
create_gitstats() %>%
set_gitlab_host(
orgs = "mbtests",
verbose = FALSE
)
)
})
Loading