Personal token

pull/645/head
Tienson Qin 2020-05-14 18:52:45 +08:00
parent 94851966e9
commit e7712cf5a7
7 changed files with 114 additions and 126 deletions

View File

@ -140,7 +140,7 @@
(rum/defcc diff < rum/reactive (rum/defcc diff < rum/reactive
{:will-mount {:will-mount
(fn [state] (fn [state]
(let [repo (state/get-current-repo)] (when-let [repo (state/get-current-repo)]
(handler/get-latest-commit (handler/get-latest-commit
repo repo
(fn [commit] (fn [commit]

View File

@ -27,11 +27,10 @@
:on-click :on-click
#(handler/set-preferred-format! :org))]]) #(handler/set-preferred-format! :org))]])
(rum/defcs add-repo < rum/reactive (rum/defc add-repo < rum/reactive
(rum/local "" ::repo-url) []
[state] (let [access-token (state/sub [:me :access-token])
(let [repo-url (state/sub :git/clone-repo) repo-url (state/sub :git/clone-repo)]
git-ask-private-grant? (state/sub :git/ask-for-private-repo-grant)]
[:div.p-8.flex.items-center.justify-center [:div.p-8.flex.items-center.justify-center
[:div.w-full.mx-auto [:div.w-full.mx-auto
[:div [:div
@ -39,13 +38,24 @@
[:h1.title.mb-1 [:h1.title.mb-1
"Import your notes"] "Import your notes"]
[:p.text-sm.text-gray-500.pl-1 "You can import your notes from a repo on Github."] [:p.text-sm.text-gray-500.pl-1 "You can import your notes from a repo on Github."]
[:div.mt-2.mb-2.relative.rounded-md.shadow-sm.max-w-xs [:div.mt-4.mb-2.relative.rounded-md.shadow-sm.max-w-xs
[:div.font-bold.text-gray-600.mb-1 "Repo url:"]
[:input#repo.form-input.block.w-full.sm:text-sm.sm:leading-5 [:input#repo.form-input.block.w-full.sm:text-sm.sm:leading-5
{:autoFocus true {:autoFocus true
:placeholder "https://github.com/username/repo" :placeholder "https://github.com/username/repo"
:value repo-url :value repo-url
:on-change (fn [e] :on-change (fn [e]
(state/set-git-clone-repo! (util/evalue e)))}]]]] (state/set-git-clone-repo! (util/evalue e)))}]]
[:div.mt-2.mb-2.relative.rounded-md.shadow-sm.max-w-xs
[:div.font-bold.text-gray-600.mb-1 "Personal access token (optional):"]
[:input#repo.form-input.block.w-full.sm:text-sm.sm:leading-5
{:value access-token
:on-change (fn [e]
(let [value (util/evalue e)]
(when-not (string/blank? value)
(state/set-github-token! value))))}]]]]
(ui/button (ui/button
"Clone" "Clone"
:on-click :on-click
@ -54,21 +64,22 @@
(handler/clone-and-pull repo-url) (handler/clone-and-pull repo-url)
(handler/redirect! {:to :home})))) (handler/redirect! {:to :home}))))
(when git-ask-private-grant? ;; (when git-ask-private-grant?
[:div ;; [:div
[:hr] ;; [:hr]
[:div ;; [:div
[:h3.text-red-700.mb-2 "Git clone failed, it might be two reasons:"] ;; [:h3.text-red-700.mb-2 "Git clone failed, it might be two reasons:"]
[:ol ;; [:ol
[:li.mb-1 "Please check the repo link is correct."] ;; [:li.mb-1 "Please check the repo link is correct."]
[:li ;; [:li
[:div.mb-1 ;; [:div.mb-1
"You're cloning a " ;; "You're cloning a "
[:b "private"] ;; [:b "private"]
" repo, we need your permission grants for that. ;; " repo, we need your permission grants for that.
We promise that our server will never store your github oauth token, it'll be stored securely and only in the " ;; We promise that our server will never store your github oauth token, it'll be stored securely and only in the "
[:a.underline {:title "Which has a HttpOnly flag"} ;; [:a.underline {:title "Which has a HttpOnly flag"}
"browser cookie"] ;; "browser cookie"]
"."] ;; "."]
[:a {:href "/auth/github_ask_repo_permission"} ;; [:a {:href "/auth/github_ask_repo_permission"}
(ui/button "Grant us your private repo permission")]]]]])]])) ;; (ui/button "Grant us your private repo permission")]]]]])
]]))

View File

@ -3,6 +3,7 @@
[promesa.core :as p])) [promesa.core :as p]))
(set! (.-fs js/window) (js/LightningFS. "logseq")) (set! (.-fs js/window) (js/LightningFS. "logseq"))
(js/git.plugins.set "fs" js/window.fs)
(set! (.-pfs js/window) js/window.fs.promises) (set! (.-pfs js/window) js/window.fs.promises)
(defn mkdir (defn mkdir

View File

@ -12,43 +12,33 @@
;; TODO: move to a js worker ;; TODO: move to a js worker
(defonce default-branch "master") (defonce default-branch "master")
(defn with-fs-http
[m]
(clojure.core/merge
m
{:fs js/window.fs
:http js/window.GitHttp}))
;; only support Github now ;; only support Github now
(defn auth (defn auth
[token] [token]
{:onAuth (fn [_url] {:username token
(clj->js :password "x-oauth-basic"})
{:username token
:password "x-oauth-basic"}))})
(defn set-username-email (defn set-username-email
[dir username email] [dir username email]
(p/let [_ (js/git.setConfig (util/p-handle (js/git.config (clj->js
(clj->js {:global true
(with-fs-http :dir dir
{:dir dir :path "user.name"
:path "user.name" :value username}))
:value username})))] (fn [result]
(js/git.setConfig (js/git.config (clj->js
(clj->js {:global true
(with-fs-http :dir dir
{:dir dir :path "user.email"
:path "user.email" :value email})))
:value email}))))) (fn [error]
(prn "error:" error))))
(defn with-auth (defn with-auth
[token m] [token m]
(clj->js (clj->js
(clojure.core/merge (clojure.core/merge (auth token)
m m)))
(auth token))))
(defn get-repo-dir (defn get-repo-dir
[repo-url] [repo-url]
@ -56,102 +46,87 @@
(defn clone (defn clone
[repo-url token] [repo-url token]
(let [m (with-fs-http (js/git.clone (with-auth token
{:dir (get-repo-dir repo-url) {:dir (get-repo-dir repo-url)
:url repo-url :url repo-url
:corsProxy "https://cors.isomorphic-git.org" :corsProxy "https://cors.isomorphic-git.org"
:ref default-branch :singleBranch true
:singleBranch true :depth 1})))
:username (get-in @state/state [:me :name] "logseq-demo")
:depth 1})]
(js/git.clone (if token
(with-auth token m)
(clj->js m)))))
(defn list-files (defn list-files
[repo-url] [repo-url]
(js/git.listFiles (clj->js (js/git.listFiles (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref "HEAD"})))
:ref "HEAD"}))))
(defn fetch (defn fetch
[repo-url token] [repo-url token]
(js/git.fetch (with-auth token (js/git.fetch (with-auth token
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref default-branch
:ref default-branch :singleBranch true
:singleBranch true :depth 100
:depth 100 :tags false})))
:tags false}))))
(defn merge (defn merge
[repo-url] [repo-url]
(js/git.merge (clj->js (js/git.merge (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ours default-branch
:ours default-branch :theirs (str "remotes/origin/" default-branch)
:theirs (str "remotes/origin/" default-branch) :fastForwardOnly true})))
:fastForwardOnly true}))))
(defn checkout (defn checkout
[repo-url] [repo-url]
(js/git.checkout (clj->js (js/git.checkout (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref default-branch})))
:ref default-branch}))))
(defn log (defn log
[repo-url token depth] [repo-url token depth]
(js/git.log (with-auth token (js/git.log (with-auth token
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref default-branch
:ref default-branch :depth depth
:depth depth :singleBranch true})))
:singleBranch true}))))
(defn pull (defn pull
[repo-url token] [repo-url token]
(js/git.pull (with-auth token (js/git.pull (with-auth token
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref default-branch
:ref default-branch :singleBranch true
:singleBranch true :fast true})))
:fast true}))))
(defn add (defn add
[repo-url file] [repo-url file]
(js/git.add (clj->js (js/git.add (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :filepath file})))
:filepath file}))))
(defn commit (defn commit
[repo-url message] [repo-url message]
(let [{:keys [name email]} (:me @state/state)] (let [{:keys [name email]} (:me @state/state)]
(js/git.commit (clj->js (js/git.commit (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :message message
:message message :author {:name name
:author {:name name :email email}}))))
:email email}})))))
(defn read-commit (defn read-commit
[repo-url oid] [repo-url oid]
(js/git.readCommit (clj->js (js/git.readCommit (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :oid oid})))
:oid oid}))))
(defn push (defn push
([repo-url token] ([repo-url token]
(push repo-url token false)) (push repo-url token false))
([repo-url token force?] ([repo-url token force?]
(js/git.push (with-auth token (js/git.push (with-auth token
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :remote "origin"
:remote "origin" :ref default-branch
:ref default-branch :force force?}))))
:force force?})))))
(defn add-commit (defn add-commit
[repo-url file message commit-ok-handler commit-error-handler] [repo-url file message commit-ok-handler commit-error-handler]
@ -183,9 +158,8 @@
([repo-url branch] ([repo-url branch]
(p/let [matrix (js/git.statusMatrix (p/let [matrix (js/git.statusMatrix
(clj->js (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref "HEAD"}))]
:ref "HEAD"})))]
(let [matrix (bean/->clj matrix)] (let [matrix (bean/->clj matrix)]
;; added, modified, deleted ;; added, modified, deleted
{:added (->> (filter (fn [[_file head-status _workdir-status _stage-status]] {:added (->> (filter (fn [[_file head-status _workdir-status _stage-status]]
@ -227,11 +201,10 @@
(defn read-blob (defn read-blob
[repo-url oid path] [repo-url oid path]
(js/git.readBlob (clj->js (js/git.readBlob (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :gitdir (str (get-repo-dir repo-url) ".git")
:gitdir (str (get-repo-dir repo-url) ".git") :oid oid
:oid oid :path path})))
:path path}))))
;; * await git.writeRef({ ;; * await git.writeRef({
;; * fs, ;; * fs,
@ -242,8 +215,7 @@
(defn write-ref! (defn write-ref!
[repo-url oid] [repo-url oid]
(js/git.writeRef (clj->js (js/git.writeRef (clj->js
(with-fs-http {:dir (get-repo-dir repo-url)
{:dir (get-repo-dir repo-url) :ref (str "refs/heads/" default-branch)
:ref (str "refs/heads/" default-branch) :value oid
:value oid :force true})))
:force true}))))

View File

@ -1,7 +1,7 @@
// move to cljs // move to cljs
// copied from https://github.com/kpj/GitViz/blob/83dfc65624f5dae41ffb9e8a97d2ee61512c1365/src/git-handler.js // copied from https://github.com/kpj/GitViz/blob/83dfc65624f5dae41ffb9e8a97d2ee61512c1365/src/git-handler.js
export var getFileStateChanges = async function (commitHash1, commitHash2, dir) { export var getFileStateChanges = async function (commitHash1, commitHash2, dir) {
return git.walk({ return git.walkBeta1({
trees: [ trees: [
git.TREE({ dir: dir, ref: commitHash1 }), git.TREE({ dir: dir, ref: commitHash1 }),
git.TREE({ dir: dir, ref: commitHash2 }) git.TREE({ dir: dir, ref: commitHash2 })

View File

@ -484,11 +484,11 @@
(defn periodically-pull-and-push (defn periodically-pull-and-push
[repo-url {:keys [pull-now?] [repo-url {:keys [pull-now?]
:or {pull-now? true}}] :or {pull-now? true}}]
;; (periodically-pull repo-url pull-now?) (periodically-pull repo-url pull-now?)
;; (periodically-push-tasks repo-url) (periodically-push-tasks repo-url)
(when-not config/dev? ;; (when-not config/dev?
(periodically-pull repo-url pull-now?) ;; (periodically-pull repo-url pull-now?)
(periodically-push-tasks repo-url)) ;; (periodically-push-tasks repo-url))
) )
(defn render-local-images! (defn render-local-images!

View File

@ -241,3 +241,7 @@
(defn logged? (defn logged?
[] []
(get-in @state [:me :email])) (get-in @state [:me :email]))
(defn set-github-token!
[token]
(swap! state assoc-in [:me :access-token] token))