Skip to content

use_github does not push with https protocol and GITHUB_PAT #320

@cderv

Description

@cderv

I tried to create a 📦 with usethis calling at some point use_github.

First my setup is:

  • Windows
  • GITHUB_PAT environment variable is written in .Renviron
  • I get correct results from gh::gh_whoami.

Howerver, when using https protocol, use_github does not manage to push to the created repo because of a mishandling of credential with git2r.

Here how to reproduce the issue

testdir <- tempfile("testdir")
dir.create(testdir)
setwd(testdir)
library(usethis)
#> Warning: le package 'usethis' a été compilé avec la version R 3.4.4
create_package("testpkg", open = FALSE)
#> Changing active project to testpkg
#> <U+2714> Creating 'R/'
#> <U+2714> Creating 'man/'
#> <U+2714> Writing 'DESCRIPTION'
#> <U+2714> Writing 'NAMESPACE'
setwd(file.path(testdir, "testpkg/"))
use_git()
#> <U+2714> Initialising Git repo
#> <U+2714> Adding '.Rhistory', '.RData', '.Rproj.user' to './.gitignore'
#> <U+2714> Adding files and committing
use_github(protocol = "https")
#> <U+2714> Setting title and description
#>   Name:        testpkg
#>   Description: What the Package Does (One Line, Title Case)
#> <U+2714> Creating GitHub repository
#> <U+2714> Adding GitHub remote
#> <U+2714> Adding GitHub links to DESCRIPTION
#> <U+2714> Setting URL field in DESCRIPTION to 'https://github.com/cderv/testpkg'
#> <U+2714> Setting BugReports field in DESCRIPTION to 'https://github.com/cderv/testpkg/issues'
#> <U+2714> Pushing to GitHub and setting remote tracking branch
#> Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'cred_user_pass' for signature '"character", "NULL"'

the last step failed. The repository was created but use_github was not able to push to the repository. It seems it can’t deal with current git2r credential.

usethis/R/github.R

Lines 142 to 147 in 7d1c255

} else { ## protocol == "https"
## in https case, when GITHUB_PAT is passed as password,
## the username is immaterial, but git2r doesn't know that
cred <- git2r::cred_user_pass("EMAIL", auth_token)
git2r::push(r, "origin", "refs/heads/master", credentials = cred)
}

The credentials used are built with cred <- git2r::cred_user_pass("EMAIL", auth_token).
However, here auth_token = NULL by default and git2r does not know how to handle that.
auth_token = NULL by default indicates in the documentation that it should use the github PAT.
I am really not sure to understand why the use of cred_user_pass with "EMAIL" as username and an auth_token as password. I will try to investigate further the reason of this choice, but if you have an explanation, please tell me.

As it seems strange to me I tried another way.
First, git2r as specific credential object for token authentification, that by default get the GITHUB_PAT environment variable. Using this works.

r <- git2r::repository(file.path(testdir, "testpkg/"))
cred <- git2r::cred_token()
git2r::push(r, "origin", "refs/heads/master", credentials = cred)

There is also a way to use user/pass auth with the token using the username (or organization) and token as password.

cred2 <- git2r::cred_user_pass("cderv", Sys.getenv("GITHUB_PAT"))
# nothing to push but no error
git2r::push(r, "origin", "refs/heads/master", credentials = cred)

# cleaning this reprex
unlink(testdir, recursive = TRUE)

# delete manually the repo on github

A the end, do you think the choice in use_github is correct ? Should it be change to another credential object ? What are you thinking about all this ?

I am wiling to help with a PR when ready. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions