r-hub / rhub

R-hub API client
https://r-hub.github.io/rhub/
Other
353 stars 51 forks source link

Error due to gh_rest_post() response returning raw data instead of character #611

Open tylermorganwall opened 4 months ago

tylermorganwall commented 4 months ago

I have been trying to set up rhub on my package rayimage, but I was getting an odd error when calling both rhub_check() and rhub_doctor:

> rhub::rhub_doctor()
✔ Found R package at /Users/tyler/Desktop/R/rayimage.
✔ Found git repository at /Users/tyler/Desktop/R/rayimage.
✔ Found GitHub PAT.                                 
Error in `resp$content$errors`:
! $ operator is invalid for atomic vectors
Type .Last.error to see the more details.

I used trace() to pinpoint the issue to the resp variable:

resp <- gh_rest_post(url$api, ep, token = pat, data = jsondata)

Specifically, the content was the following:

Browse[1]> resp
$url
[1] "https://github.com/api/v3/repos/tylermorganwall/rayimage/actions/workflows/rhub.yaml/dispatches"

$status_code
[1] 403

$type
[1] "text/plain; charset=utf-8"

$headers
$headers$server
[1] "GitHub.com"

$headers$date
[1] "Thu, 30 May 2024 02:52:52 GMT"

$headers$`content-type`
[1] "text/plain; charset=utf-8"

$headers$vary
[1] "X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame"

$headers$`cache-control`
[1] "no-cache"

...
other stuff
...

$content
 [1] 43 6f 6f 6b 69 65 73 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 74 6f 20 75 73 65 20 47 69 74 48 75 62 2e

(in ASCII, this is "Cookies must be enabled to use GitHub.")

I'm not sure why rhub_check() and rhub_doctor() is failing, but the functions should probably be fixed to parse this response and return the error in human readable form.

sessionInfo:

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rhub_2.0.0      rayimage_0.11.0

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       httr_1.4.7        cli_3.6.2         rlang_1.1.3       processx_3.8.4    textshaping_0.4.0
 [7] gitcreds_0.1.2    jsonlite_1.8.8    glue_1.7.0        openssl_2.2.0     askpass_1.2.0     rprojroot_2.0.4  
[13] ps_1.7.6          sys_3.4.2         ragg_1.3.2        fansi_1.0.6       rappdirs_0.3.3    grid_4.4.0       
[19] tibble_3.2.1      lifecycle_1.0.4   compiler_4.4.0    rematch_2.0.0     Rcpp_1.0.12       pkgconfig_2.0.3  
[25] whoami_1.3.0      rstudioapi_0.16.0 systemfonts_1.1.0 gert_2.0.1        R6_2.5.1          utf8_1.2.4       
[31] curl_5.2.1        pillar_1.9.0      credentials_2.0.1 magrittr_2.0.3    tools_4.4.0  
gaborcsardi commented 4 months ago

Are you sure that your GitHub PAT is correct?

tylermorganwall commented 4 months ago

I have regenerated a new token, added it via gitcreds::gitcreds_set(), confirmed it's correct via both gh::gh_whoami() and that it matches what is listed in gitcreds::gitcreds_get()$password, and successfully pushed a commit to a repo I own (here, I add the rhub GHA workflow to another package, which successfully then runs rhub_doctor()). (https://github.com/tylermorganwall/rayvertex/commit/c6ef676cbe2be48c4daf89d22dca30200f18f672)

> rhub::rhub_doctor()
✔ Found R package at /Users/tyler/Desktop/R/rayvertex.
✔ Found git repository at /Users/tyler/Desktop/R/rayvertex.
✔ Found GitHub PAT.                                 
✔ Found repository on GitHub at <https://github.com/tylermorganwall/rayvertex.git>.
✔ GitHub PAT has the right scopes.            
✔ Found R-hub workflow in default branch, and it is active.              
→ WOOT! You are ready to run rhub::rhub_check() on this package.

After all of that, running this in my project for rayimage returns the following:

> gh::gh_whoami()
{
  "name": {},
  "login": "tylermorganwall",
  "html_url": "https://github.com/tylermorganwall",
  "scopes": "gist, repo, user, workflow",
  "token": "(correct token)"
} 
> gitcreds::gitcreds_get()
<gitcreds>
  protocol: https
  host    : github.com
  username: PersonalAccessToken
  password: <-- hidden -->
> rhub::rhub_doctor()
✔ Found R package at /Users/tyler/Desktop/R/rayimage.
✔ Found git repository at /Users/tyler/Desktop/R/rayimage.
✔ Found GitHub PAT.                                 
Error in `resp$content$data`:
! $ operator is invalid for atomic vectors
Type .Last.error to see the more details.
gaborcsardi commented 4 months ago

Is it a fine-grained or a classic token? rhub_doctor() only works with a classic token currently. rhub_check() should still work with a fine-grained token, though.

tylermorganwall commented 4 months ago

Classic token.