ropensci / piggyback

:package: for using large(r) data files on GitHub
https://docs.ropensci.org/piggyback
GNU General Public License v3.0
182 stars 26 forks source link

Slight modification to allow for Github Enterprise servers #122

Closed ying14 closed 2 months ago

ying14 commented 4 months ago

Many thanks for piggyback... it is super-handy for many of our data-keeping needs.

It would be very useful if this worked with Github Enterprise deployments, i.e. Github API servers other than https://api.github.com.

At the moment, it almost works... I can get some functions to play along by setting the environment variable GITHUB_API_URL to the desired Enterprise server location:

Sys.setenv("GITHUB_API_URL" = "https://hostname/api/v3")
pb_releases("enterprise_owner/enterprise_repo")

This works because many piggyback functions are using gh::gh() and gh::gh_token(), which to allow for the Github API URL to be specified...either explicitly in the arguments, or through the environment variable (if neither is specified, it will default to https://api.github.com).

However, the environment variable trick does not work in functions where https://api.github.com is hard-coded, such as pb_release_create() and pb_release_delete().

Sys.setenv("GITHUB_API_URL" = "https://hostname/api/v3")
pb_release_create("enterprise_owner/enterprise_repo",tag="v0.0.5")

#> $message
#> [1] "Bad credentials"
#> 
#> $documentation_url
#> [1] "https://docs.github.com/rest"
#> 
#> Warning message:
#> ! Failed to create release: HTTP error 401.
#> See returned error messages for more details 

I found that I can get it to work if I modify these package functions to check the environment, similar to what gh is doing. For example, in pb_release_create(), add the line api_url <- Sys.getenv("GITHUB_API_URL", unset = "https://api.github.com"), and change ...url = glue::glue("https://api.github.com/repos/{r[[1]]}/{r[[2]]}/releases")... to ...url = glue::glue("{api_url}/repos/{r[[1]]}/{r[[2]]}/releases")....

See if you think this is worthwhile... not sure if I am missing important considerations but this seems to do the trick for my purposes. I suppose the other way would be to add a .api_url parameter to all the pertinent functions, and make sure the token is appropriately matching.

tanho63 commented 4 months ago

Yeah, using the gh envvar is probably the right call here