-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.clj
62 lines (54 loc) · 2.03 KB
/
token.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns clj-github.token
(:require [cheshire.core :as cheshire]
[clj-github-app.token-manager :as token-manager]
[clj-yaml.core :as yaml]
[clojure.java.io :as io]
[org.httpkit.client :as httpkit]))
(defn- file-exists-or-nil [file]
(when (.exists file)
file))
(def hub-config
(memoize
(fn []
(some-> (io/file (System/getenv "HOME") ".config/hub")
file-exists-or-nil
(slurp)
yaml/parse-string
(get :github.com)
first
(get :oauth_token)))))
(def env-var
(memoize
(fn [] (System/getenv "GITHUB_TOKEN"))))
(def github-url "https://api.github.com")
(def ^:private get-token-manager
(memoize
(fn [{:keys [github-app-id github-private-key]}]
(when (and github-app-id github-private-key)
(token-manager/make-token-manager github-url github-app-id github-private-key)))))
(def ^:private get-installation-id
(memoize
(fn
[{:keys [github-org] :as opts}]
(let [token-manager (get-token-manager opts)
result @(httpkit/request {:url (str github-url "/app/installations")
:method :get
:headers {"Authorization" (str "Bearer " (token-manager/get-app-token token-manager))
"Accept" "application/vnd.github.machine-man-preview+json"}})]
(->> (cheshire/parse-string (:body result) keyword)
(filter #(= (:login (:account %)) (or github-org "nubank")))
(first)
(:id))))))
(defn github-app-token-manager [opts]
(fn []
(when-let [token-manager (get-token-manager opts)]
(token-manager/get-installation-token token-manager
(get-installation-id opts)))))
(defn chain [providers]
(fn []
(loop [providers providers]
(when (seq providers)
(if-let [token (-> (first providers)
(apply nil))]
token
(recur (rest providers)))))))