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

Handle host for use_github() same way as auth_token #1333

Closed
jennybc opened this issue Sep 7, 2016 · 7 comments
Closed

Handle host for use_github() same way as auth_token #1333

jennybc opened this issue Sep 7, 2016 · 7 comments
Labels
feature a feature request or enhancement usethis

Comments

@jennybc
Copy link
Member

jennybc commented Sep 7, 2016

I propose handling the host for use_github() in a similar fashion as auth_token, which is this:

  • by default, helper function github_pat() is called, which tries to retrieve PAT from env var GITHUB_PAT

Could we use an env var named GITHUB_API_URL for this? I suggest it because gh already does this. If GITHUB_API_URL is not defined, then use existing default https://api.github.com.

This makes it easier to define a user-level GITHUB_PAT for github.com and, for individual projects, store an alternative host and PAT for GitHub Enterprise in a project-level .Renviron.

cc @ijlyttle

@ijlyttle
Copy link
Contributor

ijlyttle commented Sep 8, 2016

I'm intrigued by the idea. It might avoid the necessity for a company-specific function like I have written and packaged (privately), use_github_se(), which uses our host as a default, and looks for an env var named GITHUB_SE_PAT.

Maybe the company-specific helper function would do something different, writing the project-level .Renviron.

The one thing I cannot reason about is what might happen if we rely on a project-level definition of
GITHUB_PAT, then install_github("hadley/newpackage") - would things fail in a way that would be difficult to figure out?

I will have to make an experiment :)

@jennybc
Copy link
Member Author

jennybc commented Sep 8, 2016

OK good point. So any GHE solution needs to not make github.com API calls impossible or extremely awkward. Too bad .Renviron doesn't work more like .gitconfig, i.e. you get union of local and global settings.

@ijlyttle
Copy link
Contributor

ijlyttle commented Sep 9, 2016

I am mindful that I am likely diverting the thread, but here's (the start of) a writeup of how I have approached the GHE solution: http://ijlyttle.github.io/nondim_devtools_gh.html - it's very rough at this point, but if I wait until I get the prose right, well, we'll be waiting...

As I note, I don't pretend that this a best practice, simply that it is a practice. If nothing else, maybe it can shake loose some ideas.

@jennybc
Copy link
Member Author

jennybc commented Sep 9, 2016

Nice! So maybe I will try some variation on your solution for a while.

@ijlyttle
Copy link
Contributor

ijlyttle commented Sep 9, 2016

Thanks!

@hadley hadley added feature a feature request or enhancement install labels Nov 3, 2016
@saurfang
Copy link

saurfang commented Feb 9, 2017

This is awesome! One thing I'd like to add is that with https://github.com/cscheid/rgithub I can use OAuth token instead of PAT. The upside is that I no longer need to ask every user to go to a page, click, type, click, copy, create file, paste, save and makes mistake. Downside is that token can expire and as long as they are logged in, they just need to close the page.

A sample code might looks like this

if (!requireNamespace("devtools", quietly = TRUE)) {
    message("Installing devtools package...")
    install.packages("devtools")
}

if (!requireNamespace("github", quietly = TRUE)) {
    message("Installing rGithub package...")
    devtools::install_github("cscheid/rgithub")
}

#' Generate an Interactive GHE Context
#' 
#' Authenticate with GHE using OAuth token.
#' 
#' @seealso \link[httr]{oauth2.0_token}
ghe_context <- function() {
  # Save current httr OAuth caching option
  old_httr_oauth_cache <- getOption("httr_oauth_cache")
  # Cache OAuth token at home directory instead
  options(httr_oauth_cache = "~/.utilr{{private}}/.httr-oauth-ghe")
  # Restore httr OAuth caching option
  on.exit(options(httr_oauth_cache = old_httr_oauth_cache))
  
  client_id <- "{{not_so_secret_id}}"
  client_secret <- "{{not_so_secret_token}}"
  base_url <- "{{private host}}"
  api_url <- file.path(base_url, "api", "v3")
  github::interactive.login(client_id, client_secret, NULL, base_url, api_url)
}

#' Updates utilr{{private}} and all of its required packages to the latest versions
#'
#' @export
update_utilr <- function(...) {
  ctx <- ghe_context()
  auth_token <- ctx$token$credentials$access_token
  
  devtools::install_github("{{private}}/utilr", host = ctx$api_url, auth_token = auth_token, ...)
}

@lock
Copy link

lock bot commented Sep 18, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement usethis
Projects
None yet
Development

No branches or pull requests

5 participants