-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-auth-github.clj
executable file
·97 lines (81 loc) · 2.9 KB
/
ssh-auth-github.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env bb
(require '[babashka.curl :as curl])
(require '[cheshire.core :as json])
(require '[clojure.tools.cli :as tools.cli])
(require '[clojure.string :as str])
(require '[babashka.fs :as fs])
(defn print-usage [summary]
(println
(->> ["Usage: " "ssh-auth-github.clj [options]"
""
"Options:"
summary]
(str/join \newline))))
(defn read-config []
(let [{:keys [options _arguments errors summary] :as _cli-options}
(tools.cli/parse-opts
*command-line-args*
[["-h" "--help"]
["-t" "--token TOKEN" "Github token"]
["-o" "--organization ORGANIZATION" "Github organization"]
["-e" "--team TEAM" "Github team"]
["-c" "--config PATH" "Path to configuration file" :default "config.edn"]])]
(when (:help options)
(print-usage summary)
(System/exit 1))
(when errors
(println errors)
(print-usage summary)
(System/exit 1))
(cond-> {}
(fs/exists? (:config options))
(merge
(read-string (slurp (:config options))))
true (merge (dissoc options :config)))))
(defn query [organization team]
(format "{organization(login: \"%s\") {
team(slug: \"%s\") {
members {
nodes {
login
publicKeys(first: 100) {
nodes {
key
}
}
}
}
}
}
}"
organization team))
(defn retrieve-keys [config]
(let [resp (curl/post "https://api.github.com/graphql"
{:body (json/generate-string {"query" (query (:organization config)
(:team config))})
:throw false
:headers {"Authorization" (format "bearer %s" (:token config))}})
body (-> resp
(:body)
(json/parse-string true))]
(cond
(not= 200 (:status resp))
(do (clojure.pprint/pprint body)
(System/exit 1))
(:errors body)
(do
(clojure.pprint/pprint (:errors body))
(System/exit 1))
:else
body)))
(defn parse-response [data]
(when-not (seq (get-in data [:data :organization :team]))
(println "Error: Team is empty!")
(System/exit 1))
(->> (get-in data [:data :organization :team :members :nodes])
(mapcat (fn [n] (map #(str (:key %) " " (:login n))
(get-in n [:publicKeys :nodes]))))))
(doseq [l (->> (read-config)
(retrieve-keys)
(parse-response))]
(println l))