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

Support GitHub Enterprise #5

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions ghub.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,62 @@
(defvar url-http-end-of-headers)
(defvar url-http-response-status)

(defconst ghub--domain "api.github.com")
(defconst ghub--root-endpoint "https://api.github.com")

(defvar ghub-unpaginate nil)

(defvar ghub-domain "github.com"
"The GitHub domain to query against.
Default value is for GitHub.com, but this variable may be
let-bound to query against GitHub Enterprise installations.

See also `ghub-domain-configuration'.")

(defgroup ghub nil
"The minuscule client for GitHub"
:prefix "ghub")

(defcustom ghub-domain-configuration
'(("github.com" t (lambda ()
(substring (shell-command-to-string "git config github.user")
0 -1))))
"Domain configurations.
Each configuration is a list of the following format:

\(DOMAIN SECURE USERNAME\)

where

DOMAIN is the domain name of your GitHub installation
SECURE specifies use of HTTPS (default) when non-nil
USERNAME is your username or a function that returns it"
:group 'ghub
:type '(repeat (list (string :tag "Domain")
(boolean :tag "Use secure connection?"
:value t)
(choice :tag "Username"
(string)
(function)))))

(defun ghub--domain-configuration (domain prop &optional default)
"Search for DOMAIN in `ghub-domain-configuration' and return PROP.
If DOMAIN is not present in `ghub-domain-configuration', return
DEFAULT."
(let* ((config (cdr (assoc-string domain ghub-domain-configuration)))
(config (list :secure (nth 0 config)
:username (nth 1 config))))
(let ((val (if (member domain (mapcar #'car ghub-domain-configuration))
(plist-get config prop)
default)))
(if (functionp val)
(funcall val)
val))))

(defun ghub--root-endpoint (domain)
"Gets the root API endpoint for DOMAIN."
(if (string= domain "github.com")
"https://api.github.com"
(concat "http" (when (ghub--domain-configuration domain :secure t) "s")
"://" domain "/api/v3")))

(defun ghub-get (resource &optional params data noerror)
(ghub--request "GET" resource params data noerror))

Expand Down Expand Up @@ -99,11 +150,11 @@
(d (and data (json-encode-list data)))
(url-request-extra-headers
`(("Content-Type" . "application/json")
("Authorization" . ,(concat "token " (ghub--get-access-token)))))
("Authorization" . ,(concat "token " (ghub--get-access-token ghub-domain)))))
(url-request-method method)
(url-request-data d))
(with-current-buffer
(url-retrieve-synchronously (concat ghub--root-endpoint resource p))
(url-retrieve-synchronously (concat (ghub--root-endpoint ghub-domain) resource p))
(let (link body)
(goto-char (point-min))
(save-restriction
Expand Down Expand Up @@ -148,14 +199,12 @@
(url-hexify-string val)))
params "&"))

(defun ghub--get-access-token ()
(defun ghub--get-access-token (domain)
(let ((secret
(plist-get (car (auth-source-search
:max 1
:user (substring (shell-command-to-string
"git config github.user")
0 -1)
:host ghub--domain))
:user (ghub--domain-configuration domain :username)
:host domain))
:secret)))
(if (functionp secret)
(funcall secret)
Expand Down