/
github.clj
82 lines (59 loc) · 2.41 KB
/
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
(ns pinkgorilla.storage.github
(:require
[clojure.tools.logging :refer (info)]
[clojure.string]
[tentacles.gists]
[tentacles.repos]))
;defn load-gist [gist-id & [token]]
; :reason-phrase "Unauthorized"
;:status 401
(defn extract-gist-result [response]
(if (:id response)
{:success true :id (:id response)}
{:success false :error-message (get-in response [:body :message])}))
(defn save-gist [id description is-public filename content token]
(let [files {filename content}
options {:description description
:oauth-token token}]
(if (nil? id)
(do (info "creating gist: " options)
(->>
(tentacles.gists/create-gist files (assoc options :public is-public))
(extract-gist-result)))
(do (info "updating gist: " id " : " options)
(->>
(tentacles.gists/edit-gist id (assoc options :files {filename {:content content}}))
(extract-gist-result))))
{:id id :filename filename}))
(defn load-gist-all [gist-id & [token]]
(tentacles.gists/file-contents
(if (nil? token)
(tentacles.gists/specific-gist gist-id)
(tentacles.gists/specific-gist gist-id {:oauth-token token}))))
(defn load-gist [gist-id filename & [token]]
(let [f (keyword filename)]
(->> (load-gist-all gist-id token)
(f))))
(defn load-repo-raw [user repo path & [token]]
(if (nil? token)
(tentacles.repos/contents user repo path {:str? true})
(tentacles.repos/contents user repo path {:str? true :oauth-token token})))
(defn load-repo [user repo path & [token]]
(:content
(if (or (nil? token) (clojure.string/blank? token))
(tentacles.repos/contents user repo path {:str? true})
(tentacles.repos/contents user repo path {:str? true :oauth-token token}))))
;; body :message
(defn save-repo [user repo path content token]
(let [commit-message "pinkgorilla notebook save"
existing-file (load-repo-raw user repo path token)
sha (:sha existing-file)
_ (info "existing git repo sha is: " sha)]
(tentacles.repos/update-contents user repo path commit-message content sha {:oauth-token token})
{:sha sha}))
(comment
(def creds
(-> (slurp "/tmp/creds.edn")
(clojure.edn/read-string)))
(save-repo "pink-gorilla" "sample-notebooks" "unittest.txt" "test!" (:github-token creds))
(load-repo "pink-gorilla" "sample-notebooks" "unittest.txt" (:github-token creds)))