Usethis: use_github() enterprise API problem

Created on 11 Jul 2019  路  5Comments  路  Source: r-lib/usethis

Hi,

I noticed that when trying to us the use_github() function with an enterprise account, it will give me an error saying

Error: GitHub `auth_token` is invalid.
See `browse_github_token()` for help storing a token as an environment variable.

After a long search I found out this is because I have an enterprise account through my university and it uses a different host address. When I do use the host api in the function (e.g. use_github(host = "github.uc.edu/api/v3") ), it still produces the same error.

I know the token is working as I tested it with the gh function:

gh::gh_whoami(.api_url = "https://github.uc.edu/api/v3")

I dug in the code and think I found the source of the problem in the following function:

github_user <- function(auth_token = github_token()) {
  suppressMessages(
    tryCatch(gh::gh_whoami(auth_token), error = function(e) NULL)
  )
}

This will generate a NULL value if the correct host variable is not given. I think it can simply be fixed by adding the .api_url = host (where host is the variable specified in the use_github ) like so:

github_user <- function(auth_token = github_token()) {
  suppressMessages(
    tryCatch(gh::gh_whoami(auth_token, .api_url = host), error = function(e) NULL)
  )
}

Let me know what you think!

Grtz,
PJ

Most helpful comment

After a tour in the code, I think compatibility with github enterprise is not completely full yet.

  • A few functions has a host argument but there is still some where it is missing. Adding it to any function that requires it could get thinks more complex. Setting GITHUB_API_URL as built gh and removing the host argument from any function could be a better solution... That way usethis is depending on gh for GHE support.
  • Some functions like pr_create_gh depends on https://github.com directly and won't work in Github enterprise. I don't know if they should support GHE.

I opened a draft PR to centralize the thinking on this.

All 5 comments

I don't think this is really documented but usethis should be compliant with gh default behaviour. And you can configure an environment variable for gh to change the default url.
Try:

Sys.setenv(GITHUB_API_URL="https://github.uc.edu/api/v3")
gh::gh_whoami()

This should work - it works for me.

I searched for documentation on this and there is none at my knowledge, unless you read code, PR and commits.

I don't know if this could be change in usethis. I find it convenient that it uses the default behavior of gh and not introduce more arguments.
However, I think this should be documented in usethis and gh.

Thank you so much!
It worked like a charm!!
But I agree it should be documented better so others will not suffer over it anymore :)

Thanks
PJ

This is now documented in gh.

For usethis, I see two things

  • Document that gh default behaviour is used with GITHUB_API_URL env var.
  • add a host argument in github_user() as you suggest because every other github funtion in usethis have a host argument.

After a tour in the code, I think compatibility with github enterprise is not completely full yet.

  • A few functions has a host argument but there is still some where it is missing. Adding it to any function that requires it could get thinks more complex. Setting GITHUB_API_URL as built gh and removing the host argument from any function could be a better solution... That way usethis is depending on gh for GHE support.
  • Some functions like pr_create_gh depends on https://github.com directly and won't work in Github enterprise. I don't know if they should support GHE.

I opened a draft PR to centralize the thinking on this.

Hi @cderv, just to note that your suggestion just saved the day for me:

Sys.setenv(GITHUB_API_URL="https://github.uc.edu/api/v3")

FWIW, I am implementing it in a function using withr:

  withr::with_envvar(
    new = list(GITHUB_API_URL = "<url here>"),
    usethis::use_github(blah, blah, ...)
  )

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxheld83 picture maxheld83  路  3Comments

rpruim picture rpruim  路  5Comments

hadley picture hadley  路  3Comments

uribo picture uribo  路  5Comments

malcolmbarrett picture malcolmbarrett  路  4Comments