pak icon indicating copy to clipboard operation
pak copied to clipboard

Support GITLAB_PAT

Open rorynolan opened this issue 1 year ago • 9 comments

Could you please support GITLAB_PAT the same way you support GITHUB_PAT? The remotes package supports both. pak is so much faster that right now my workaround is to first clone the gitlab repo and then do a pak::local_install(). I'm happy to have a crack at a PR if you give me some instruction, but also happy for you do do it yourself.

Thank you for pak, I use it all the time.

rorynolan avatar Jan 25 '24 16:01 rorynolan

This is already possible, see the gitcreds package for the env var you need to define for your host. E.g. for gitlab.org:

❯ gitcreds::gitcreds_cache_envvar("https://gitlab.org")
[1] "GITHUB_PAT_GITLAB_ORG"

So GITHUB_PAT_GITLAB_ORG should work, let me know if it doesn't because I can't try it now.

We should document this better.

gaborcsardi avatar Jan 25 '24 17:01 gaborcsardi

Thanks for the fast response. Would you be interested in a docs-only PR to mention this?

rorynolan avatar Jan 25 '24 18:01 rorynolan

Does it actually work? :D

gaborcsardi avatar Jan 25 '24 18:01 gaborcsardi

It does :-)

rorynolan avatar Jan 25 '24 19:01 rorynolan

Yeah, in that case a PR is welcome. I'll probably also add a function to pak to call gitcreds internally. We should probably also support GIT_TOKEN_*, e.g. GIT_TOKEN_GITLAB_ORG, the current scheme is weird for other sites.

gaborcsardi avatar Jan 25 '24 21:01 gaborcsardi

The GIT_TOKEN_* makes sense to me. Do you want to implement that before I make my PR? Also, would gitcreds be used only if available? I see it's only in pak Suggests.

rorynolan avatar Jan 25 '24 21:01 rorynolan

No, I can do that later, or I can add it to the PR. gitcreds is already included in pak: https://github.com/r-lib/pak/blob/main/src/library/pkgdepends/R/git-auth.R

gaborcsardi avatar Jan 25 '24 21:01 gaborcsardi

Dear @gaborcsardi

The method describe here to install packages from private gitlab.com/org repositories does not seem to work for private gitlab instances. See reprex anonymized below

gitcreds::gitcreds_cache_envvar("https://boulangerie.thinkr.fr")
#> [1] "GITHUB_PAT_BOULANGERIE_THINKR_FR"
Sys.setenv(
  "GITHUB_PAT_BOULANGERIE_THINKR_FR" = "lefrenchmotdepasseverysecret"
)
pak::pkg_install("git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage")
#> Error: ! error in pak subprocess
#> Caused by error: 
#> ! Could not solve package dependencies:
#> * git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage: ! pkgdepends resolution error for
#> git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage.
#> Caused by error: 
#> ! Failed to download 'DESCRIPTION' from git repo at
#> <https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage>.
#> Caused by error in `stop(http_error(resp))`:
#> ! Unauthorized (HTTP 401).

pd <- pkgdepends::new_pkg_deps("git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage")
pd$solve()
pd$draw()
#> Error: ! Could not solve package dependencies:
#> * git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage: ! pkgdepends resolution error for
#> git::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage.
#> Caused by error: 
#> ! Failed to download 'DESCRIPTION' from git repo at
#> <https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage>.
#> Caused by error in `stop(http_error(resp))`:
#> ! Unprocessable Entity (WebDAV; RFC 4918) (HTTP 422).

# Control with remotes to make sur the TOKEN is valid
options(
  remotes.git_credentials = git2r::cred_user_pass(
    username = "gitlab-ci-token", 
    password = Sys.getenv("GITHUB_PAT_boulangerie_THINKR_FR")
  )
)
remotes::install_git("https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage")
#> Downloading git repo https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage
#> Registered S3 methods overwritten by 'callr':
#>   method                    from
#>   format.callr_status_error     
#>   print.callr_status_error
#> ── R CMD build ─────────────────────────────────────────────────────────────────
#> * checking for file ‘/tmp/RtmpkTHZEZ/file7cca331d9c4/DESCRIPTION’ ... OK
#> * preparing ‘mypackage’:
#> * checking DESCRIPTION meta-information ... OK
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building ‘mypackage_0.0.1.tar.gz’
#> Installing package into '/home/bob/R/x86_64-pc-linux-gnu-library/4.3'
#> (as 'lib' is unspecified)

Created on 2024-03-13 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       Pop!_OS 22.04 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Amsterdam
#>  date     2024-03-13
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  callr         3.7.5      2024-02-19 [1] CRAN (R 4.3.2)
#>  cli           3.6.2      2023-12-11 [1] CRAN (R 4.3.2)
#>  curl          5.2.1      2024-03-01 [1] CRAN (R 4.3.2)
#>  desc          1.4.3      2023-12-10 [1] CRAN (R 4.3.2)
#>  digest        0.6.35     2024-03-11 [1] CRAN (R 4.3.2)
#>  evaluate      0.23       2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi         1.0.6      2023-12-08 [1] CRAN (R 4.3.2)
#>  fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.3.1)
#>  filelock      1.0.3      2023-12-11 [1] CRAN (R 4.3.2)
#>  fs            1.6.3      2023-07-20 [1] CRAN (R 4.3.1)
#>  git2r         0.33.0     2023-11-26 [1] CRAN (R 4.3.2)
#>  gitcreds      0.1.2      2022-09-08 [1] CRAN (R 4.3.1)
#>  glue          1.7.0      2024-01-09 [1] CRAN (R 4.3.2)
#>  htmltools     0.5.7      2023-11-03 [1] CRAN (R 4.3.2)
#>  knitr         1.45       2023-10-30 [1] CRAN (R 4.3.2)
#>  lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.3.2)
#>  lpSolve       5.6.20     2023-12-10 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.3.1)
#>  pak           0.7.1.9000 2024-03-13 [1] local
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.3.1)
#>  pkgbuild      1.4.3      2023-12-10 [1] CRAN (R 4.3.2)
#>  pkgcache      2.2.1      2023-12-10 [1] CRAN (R 4.3.2)
#>  pkgdepends    0.7.1      2023-12-10 [1] CRAN (R 4.3.2)
#>  processx      3.8.3      2023-12-10 [1] CRAN (R 4.3.2)
#>  ps            1.7.6      2024-01-18 [1] CRAN (R 4.3.2)
#>  purrr         1.0.2      2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo          1.26.0     2024-01-24 [1] CRAN (R 4.3.2)
#>  R.utils       2.12.3     2023-11-18 [1] CRAN (R 4.3.2)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.3.1)
#>  remotes       2.4.2.1    2023-07-18 [1] CRAN (R 4.3.1)
#>  reprex        2.1.0      2024-01-11 [1] CRAN (R 4.3.2)
#>  rlang         1.1.3      2024-01-10 [1] CRAN (R 4.3.2)
#>  rmarkdown     2.26       2024-03-05 [1] CRAN (R 4.3.2)
#>  rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.3.1)
#>  styler        1.10.2     2023-08-29 [1] CRAN (R 4.3.1)
#>  utf8          1.2.4      2023-10-22 [1] CRAN (R 4.3.2)
#>  vctrs         0.6.5      2023-12-01 [1] CRAN (R 4.3.2)
#>  withr         3.0.0      2024-01-16 [1] CRAN (R 4.3.2)
#>  xfun          0.42       2024-02-08 [1] CRAN (R 4.3.2)
#>  yaml          2.3.8      2023-12-11 [1] CRAN (R 4.3.2)
#> 
#>  [1] /home/bob/R/x86_64-pc-linux-gnu-library/4.3
#>  [2] /opt/R/4.3.2/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

ALanguillaume avatar Mar 13 '24 09:03 ALanguillaume

Actually I encounter the errors with gitlab.com

gitcreds::gitcreds_cache_envvar("https://gitlab.com")
#> [1] "GITHUB_PAT_GITLAB_COM"

Sys.setenv(
  "GITHUB_PAT_GITLAB_COM" = "lefrenchmotdepasseverysecret"
)

pak::pkg_install("git::https://gitlab.com/antoine120/myprivatepkg")
#> Error: ! error in pak subprocess
#> Caused by error: 
#> ! Could not solve package dependencies:
#> * git::https://gitlab.com/antoine120/myprivatepkg: ! pkgdepends resolution error for
#> git::https://gitlab.com/antoine120/myprivatepkg.
#> Caused by error: 
#> ! Failed to download 'DESCRIPTION' from git repo at
#> <https://gitlab.com/antoine120/myprivatepkg>.
#> Caused by error in `stop(http_error(resp))`:
#> ! Unauthorized (HTTP 401).

pd <- pkgdepends::new_pkg_deps("git::https://gitlab.com/antoine120/myprivatepkg")
pd$solve()
pd$draw()
#> Error: ! Could not solve package dependencies:
#> * git::https://gitlab.com/antoine120/myprivatepkg: ! pkgdepends resolution error for
#> git::https://gitlab.com/antoine120/myprivatepkg.
#> Caused by error: 
#> ! Failed to download 'DESCRIPTION' from git repo at
#> <https://gitlab.com/antoine120/myprivatepkg>.
#> Caused by error in `stop(http_error(resp))`:
#> ! Unprocessable Entity (WebDAV; RFC 4918) (HTTP 422).


# Control with remotes to make sur the TOKEN is valid
options(
  remotes.git_credentials = git2r::cred_user_pass(
    username = "gitlab-ci-token", 
    password = Sys.getenv("GITHUB_PAT_GITLAB_COM")
  )
)
remotes::install_git("https://gitlab.com/antoine120/myprivatepkg")
#> Downloading git repo https://gitlab.com/antoine120/myprivatepkg
#> Registered S3 methods overwritten by 'callr':
#>   method                    from
#>   format.callr_status_error     
#>   print.callr_status_error
#> ── R CMD build ─────────────────────────────────────────────────────────────────
#> * checking for file ‘/tmp/RtmpIs2L18/file86c8627193d4/DESCRIPTION’ ... OK
#> * preparing ‘myprivatepkg’:
#> * checking DESCRIPTION meta-information ... OK
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building ‘myprivatepkg_0.0.0.9000.tar.gz’
#> Installing package into '/home/bob/R/x86_64-pc-linux-gnu-library/4.3'
#> (as 'lib' is unspecified)

Created on 2024-03-13 with reprex v2.1.0

ALanguillaume avatar Mar 13 '24 10:03 ALanguillaume

@ALanguillaume, it resolved the issue for me by setting the dependency to "gitlab" rather than "git" within pak::pkg_install(). In your case, it should be:

pak::pkg_install("gitlab::https://gitlab.com/antoine120/myprivatepkg")

For a private gitlab instance with the PAT "GITHUB_PAT_BOULANGERIE_THINKR_FR" it is:

pak::pkg_install("gitlab::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage")

mnlang avatar Jan 16 '25 07:01 mnlang

It git does not work then this is still a bug.

gaborcsardi avatar May 09 '25 11:05 gaborcsardi

FYI, I just found this thread to try to learn how to install from a self-hosted GitLab instance. The steps that worked for me (using @ALanguillaume's example):

  1. Edit the .Renviron file (using Sys.setenv() did not work-- I think because pak runs in a separate process)
    1. Get the environmental variable name for my server using the equivalent of gitcreds::gitcreds_cache_envvar("https://boulangerie.thinkr.fr") for my server
    2. Set the value to an API token with at least read_api on the server
  2. Restart R (so that the .Renviron changes take effect)
  3. Run the equivalent of pak::pak("gitlab::https://boulangerie.thinkr.fr/mygroup/mysubgroup/mypackage")

Two things that are not clear from the documentation:

  1. The project-path in a GitLab installation can include the host (like https://boulangerie.thinkr.fr/)
  2. How to find the environmental variable to set (or other way to set credentials)

billdenney avatar Aug 10 '25 00:08 billdenney