magit / forge

Work with Git forges from the comfort of Magit
GNU General Public License v3.0
1.31k stars 115 forks source link

Gitlab connection fails, self-hosted and Gitlab.com - how to setup? #186

Closed webframp closed 5 years ago

webframp commented 5 years ago

Hi There 👋 ,

I think I'm just missing some obvious detail for Gitlab setup but I can't get forge-pull to work with my self hosted Gitlab instance or with Gitlab.com. For my own gitlab host I have provided a valid ssl certificate, currently version 11.10 and reviewed issue #9

It must be some part of setup that I'm missing. I have in my ~/.authinfo.gpg the following, just using a valid domain and a token I manually created with api scope:

machine gitlab.selfhosted.url/api/v4 login webframp^forge password mytoken

My auth-sources is set to:

("/Users/sme/.emacs.d/.local/etc/authinfo.gpg" "~/.authinfo.gpg")

For my git variables I have the username set globally:

git config --global gitlab.selfhosted.url/api/v4.user webframp

With these settings I always get the following message:

Decrypting /Users/sme/.authinfo.gpg...done
ghub--token: Required Gitlab token ("webframp^forge" for "gitlab.selfhosted.url/api/v4") does not exist.
See https://magit.vc/manual/ghub/Support-for-Other-Forges.html for instructions.

but checking https://magit.vc/manual/ghub/Support-for-Other-Forges.html doesn't give me much more info, at least I can't decipher it correctly I guess.

I tried setting (setq auth-source-debug t) but didn't get much useful info, it appears authinfo is decrypting just fine. Here's the output from *Messages*:

auth-source-search: found 1 results (max 1) matching (:host "gitlab.selfhosted.url/api/v4" :user "webframp^forge" :max 1)
ghub--token: Required Gitlab token ("webframp^forge" for "gitlab.selfhosted.url/api/v4") does not exist.

What am I missing here and how can better debug what's happening? Once I figure this out I'll be happy to send a PR for doc updates.

tarsius commented 5 years ago

What's the value of auth-sources? If it doesn't begin with "/.authinfo.gpg" then that is why.

webframp commented 5 years ago

Currently it's set to:

("/Users/sme/.emacs.d/.local/etc/authinfo.gpg" "~/.authinfo.gpg")

I'll override to just ~/.authinfo.gpg and give it a try.

webframp commented 5 years ago

Well, it's a different error:

Wrong type argument: (or eieio-object class), nil, obj

I'm using the doom emacs config, lightly modified: https://github.com/hlissner/doom-emacs/

This seems like something else must have changed, maybe not forge related, will try recreating with a vanilla emacs config and report back.

tarsius commented 5 years ago

Well, it's a different error:

I need to know where that error occurs. Do M-x toggle-debug-on-error, trigger the error, and post the backtrace here.

webframp commented 5 years ago

Here's the back trace with my current doom based config:

Debugger entered--Lisp error: (wrong-type-argument (or eieio-object class) nil obj)
  signal(wrong-type-argument ((or eieio-object class) nil obj))
  #f(compiled-function (obj slot) "Return the value in OBJ at SLOT in the object vector." #<bytecode 0x44410ae9>)(nil selective-p)
  eieio-oref--closql-oref(#f(compiled-function (obj slot) "Return the value in OBJ at SLOT in the object vector." #<bytecode 0x44410ae9>) nil selective-p)
  apply(eieio-oref--closql-oref #f(compiled-function (obj slot) "Return the value in OBJ at SLOT in the object vector." #<bytecode 0x44410ae9>) (nil selective-p))
  eieio-oref(nil selective-p)
  forge-pull(nil nil)
  funcall-interactively(forge-pull nil nil)
  call-interactively(forge-pull nil nil)
  command-execute(forge-pull)

I'll follow up with same from a vanilla setup in a bit

tarsius commented 5 years ago

Sounds like

(let ((default-directory "/path/to/clone/"))
  (forge-get-repository 'create))

fails to either create and return a repository object or else raise an error. Please verify.

webframp commented 5 years ago

auth-sources is set to ("~/.authinfo.gpg") I called forge-list-repositories and it lists nothing but did compile the emacs-sqlite binary. Then I used the following in ielm

(let ((default-directory "~/path/to/clone/"))
         (forge-get-repository 'create))
*** Eval error ***  Required Gitlab token ("webframp^forge" for "gitlab.selfhosted.url/api/v4") does not exist.
See https://magit.vc/manual/ghub/Support-for-Other-Forges.html for instructions.

paths are sanitized in output here but used a real checkout with matching origin for url in the authinfo file:

origin  git@gitlab.selfhosted.url:org/repo.git (fetch)
origin  git@gitlab.selfhosted.url:org/repo.git (push)

I get prompted for gpg passphrase and Messages buffer shows the following: Decrypting /Users/sme/.authinfo.gpg...done

webframp commented 5 years ago

Tested also a vanilla emacs 26.3 with the following barebones init.el:

(require 'package)

(setq package-list
      '(use-package magit forge))

(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (when no-ssl
    (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
  ;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
  ;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
  (when (< emacs-major-version 24)
    ;; For important compatibility libraries like cl-lib
    (add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)

(unless package-archive-contents
  (package-refresh-contents))

(dolist (package package-list)
  (unless (package-installed-p package)
    (package-install package)))

(setq auth-sources
      '((:source "~/.authinfo.gpg"))
      auth-source-debug t)

(require 'epa-file)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(epg-gpg-program "/usr/local/bin/gpg")
 '(package-selected-packages (quote (use-package forge))))
(epa-file-enable)

(use-package forge
         :after magit)
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

Confirmed emacs can open my authinfo.gpg by opening it in a buffer and it decrypts fine. Going to the same repo from magit-status and trying f y results in:

Cannot determine forge repository.  git@gitlab.selfhosted.url:group/repo.git isn’t a forge url

Trying the same lisp as above in ielm:

(let ((default-directory "~/path/to/clone/"))                                                             
  (forge-get-repository 'create))                                                                        
*** Eval error ***  Cannot determine forge repository.  git@gitlab.selfhosted.url:group/repo.git isn’t a forge url
reverland commented 5 years ago

haven't you set something like this?

  (add-to-list 'forge-alist '("gitlab.gnome.org" "gitlab.gnome.org/api/v4" "gitlab.gnome.org" forge-gitlab-repository))
webframp commented 5 years ago

@reverland you're right, thanks. I missed that from the vanilla testing setup, although I do have it in my normal config already.

Adding

(add-to-list 'forge-alist '("gitlab.selfhosted.url" "gitlab.selfhosted.url/api/v4" "gitlab.selfhosted.url" forge-gitlab-repository))

Results in the same error now on forge-pull:

*** Eval error ***  Required Gitlab token ("webframp^forge" for "gitlab.selfhosted.url/api/v4") does not exist.
webframp commented 5 years ago

Tested both configurations with an unencrypted .authinfo file, which seems to work just fine for both gitlab and private hosted (although slower, as expected with gitlab). It seems the issue may be with using an ~/.authinfo.gpg file.

I'd prefer not to have any tokens in an unencrypted file of course so suggestions are appreciated.

webframp commented 5 years ago

Further debugging revealed this was a problem with the format of my ~/.authinfo.gpg file. I knew it had to be something dumb I was missing. toggle-debug-on-error continued to show the wrong type argument error. Another useful thing was setting auth-source-debug to 'trivia as described in the manual

Solution I had the file local variable set to select my gpg key wrong:

-*- epa-file-encrypt-to: ("identity@") -*-

This worked for emacs to decrypt the file when opened, but since the file local var was not properly commented out it seemed to interfere reading the file by forge. Making it a proper comment fixed the issue:

# -*- epa-file-encrypt-to: ("identity@") -*-

Now I can use an encrypted file for storing forge credentials.

tarsius commented 5 years ago

Added https://github.com/magit/forge/wiki/Common-mistakes.