mirror of
https://github.com/logseq/logseq.git
synced 2026-06-01 19:01:22 +00:00
feat(token): fetch token if token expires
This commit is contained in:
@@ -10,7 +10,8 @@
|
||||
[frontend.handler.notification :as notification]
|
||||
[frontend.handler.route :as route-handler]
|
||||
[frontend.handler.common :as common-handler]
|
||||
[cljs-time.local :as tl]))
|
||||
[cljs-time.local :as tl]
|
||||
[frontend.helper :as helper]))
|
||||
|
||||
(defn- set-git-status!
|
||||
[repo-url value]
|
||||
@@ -43,9 +44,8 @@
|
||||
(p/let [remote-oid (common-handler/get-remote-ref repo)
|
||||
commit-oid (git/commit repo commit-message (array remote-oid))
|
||||
result (git/write-ref! repo commit-oid)
|
||||
push-result (git/push repo
|
||||
(state/get-github-token repo)
|
||||
true)]
|
||||
token (helper/get-github-token repo)
|
||||
push-result (git/push repo token true)]
|
||||
(reset! pushing? false)
|
||||
(notification/clear! nil)
|
||||
(route-handler/redirect! {:to :home}))))
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
[cljs.reader :as reader]
|
||||
[clojure.string :as string]
|
||||
[frontend.dicts :as dicts]
|
||||
;; [clojure.set :as set]
|
||||
))
|
||||
[frontend.helper :as helper]))
|
||||
|
||||
;; Project settings should be checked in two situations:
|
||||
;; 1. User changes the config.edn directly in logseq.com (fn: alter-file)
|
||||
@@ -43,42 +42,6 @@
|
||||
:error
|
||||
false))
|
||||
|
||||
(defn get-new-token
|
||||
[repo]
|
||||
(when-let [installation-id (-> (filter
|
||||
(fn [r]
|
||||
(= (:url r) repo))
|
||||
(:repos (state/get-me)))
|
||||
(first)
|
||||
:installation_id)]
|
||||
(util/post (str config/api "refresh_github_token")
|
||||
{:installation-ids [installation-id]}
|
||||
(fn [result]
|
||||
(let [token (:token (first result))]
|
||||
(state/set-github-token! repo token)))
|
||||
(fn [error]
|
||||
(println "Something wrong!")
|
||||
(js/console.dir error)))))
|
||||
|
||||
(defn request-app-tokens!
|
||||
[ok-handler error-handler]
|
||||
(let [repos (:repos (state/get-me))
|
||||
installation-ids (->> (map :installation_id repos)
|
||||
(remove nil?)
|
||||
(distinct))]
|
||||
(when (or (seq repos)
|
||||
(seq installation-ids))
|
||||
(util/post (str config/api "refresh_github_token")
|
||||
{:installation-ids installation-ids
|
||||
:repos repos}
|
||||
(fn [result]
|
||||
(state/set-github-installation-tokens! result)
|
||||
(when ok-handler (ok-handler)))
|
||||
(fn [error]
|
||||
(println "Something wrong!")
|
||||
(js/console.dir error)
|
||||
(when error-handler (error-handler)))))))
|
||||
|
||||
(defn journal-file-changed?
|
||||
[repo-url diffs]
|
||||
(contains? (set (map :path diffs))
|
||||
@@ -364,10 +327,11 @@
|
||||
(and (or (string/includes? (str error) "401")
|
||||
(string/includes? (str error) "404"))
|
||||
(not fallback?))
|
||||
(request-app-tokens!
|
||||
(fn []
|
||||
(pull repo-url (state/get-github-token repo-url) {:fallback? true}))
|
||||
nil)
|
||||
(p/let [token (helper/get-github-token repo-url)]
|
||||
(helper/request-app-tokens!
|
||||
(fn []
|
||||
(pull repo-url token {:fallback? true}))
|
||||
nil))
|
||||
|
||||
(or (string/includes? (str error) "401")
|
||||
(string/includes? (str error) "404"))
|
||||
@@ -400,9 +364,10 @@
|
||||
(let [commit-message (if (string/blank? commit-message)
|
||||
"Logseq auto save"
|
||||
commit-message)]
|
||||
(p/let [commit-oid (git/commit repo-url commit-message)]
|
||||
(p/let [commit-oid (git/commit repo-url commit-message)
|
||||
token (helper/get-github-token repo-url)]
|
||||
(git-handler/set-git-status! repo-url :pushing)
|
||||
(when-let [token (state/get-github-token repo-url)]
|
||||
(when token
|
||||
(util/p-handle
|
||||
(git/push repo-url token force?)
|
||||
(fn [result]
|
||||
@@ -417,27 +382,27 @@
|
||||
(string/includes? (str error) "404"))]
|
||||
(cond
|
||||
(and permission? (not fallback?))
|
||||
(request-app-tokens!
|
||||
(helper/request-app-tokens!
|
||||
(fn []
|
||||
(git-handler/set-git-status! repo-url :re-push)
|
||||
(push repo-url
|
||||
{:commit-message commit-message
|
||||
:fallback? true}))
|
||||
{:commit-message commit-message
|
||||
:fallback? true}))
|
||||
nil)
|
||||
|
||||
:else
|
||||
(do
|
||||
(git-handler/set-git-status! repo-url :push-failed)
|
||||
(git-handler/set-git-error! repo-url error)
|
||||
(cond
|
||||
permission?
|
||||
(show-install-error! repo-url (util/format "Failed to push to %s. " repo-url))
|
||||
:else
|
||||
(do
|
||||
(git-handler/set-git-status! repo-url :push-failed)
|
||||
(git-handler/set-git-error! repo-url error)
|
||||
(cond
|
||||
permission?
|
||||
(show-install-error! repo-url (util/format "Failed to push to %s. " repo-url))
|
||||
|
||||
(state/online?)
|
||||
(pull repo-url token {:force-pull? true})
|
||||
(state/online?)
|
||||
(pull repo-url token {:force-pull? true})
|
||||
|
||||
:else ; offline
|
||||
nil)))))))))))
|
||||
:else ; offline
|
||||
nil))))))))))
|
||||
(p/catch (fn [error]
|
||||
(println "Git push error: ")
|
||||
(git-handler/set-git-status! repo-url :push-failed)
|
||||
@@ -452,39 +417,40 @@
|
||||
(defn pull-current-repo
|
||||
[]
|
||||
(when-let [repo (state/get-current-repo)]
|
||||
(when-let [token (state/get-github-token repo)]
|
||||
(pull repo token {:force-pull? true}))))
|
||||
(p/let [token (helper/get-github-token repo)]
|
||||
(when token (pull repo token {:force-pull? true})))))
|
||||
|
||||
(defn clone
|
||||
([repo-url]
|
||||
(clone repo-url false))
|
||||
([repo-url fallback?]
|
||||
(when-let [token (state/get-github-token repo-url)]
|
||||
(util/p-handle
|
||||
(do
|
||||
(state/set-cloning! true)
|
||||
(git/clone repo-url token))
|
||||
(fn [result]
|
||||
(state/set-git-clone-repo! "")
|
||||
(state/set-current-repo! repo-url)
|
||||
(db/start-db-conn! (:me @state/state) repo-url)
|
||||
(db/mark-repo-as-cloned repo-url))
|
||||
(fn [e]
|
||||
(if (and (not fallback?)
|
||||
(or (string/includes? (str e) "401")
|
||||
(string/includes? (str e) "404")))
|
||||
(request-app-tokens!
|
||||
(fn []
|
||||
(clone repo-url true))
|
||||
nil)
|
||||
(do
|
||||
(println "Clone failed, error: ")
|
||||
(js/console.error e)
|
||||
(state/set-cloning! false)
|
||||
(git-handler/set-git-status! repo-url :clone-failed)
|
||||
(git-handler/set-git-error! repo-url e)
|
||||
(p/let [token (helper/get-github-token repo-url)]
|
||||
(when token
|
||||
(util/p-handle
|
||||
(do
|
||||
(state/set-cloning! true)
|
||||
(git/clone repo-url token))
|
||||
(fn [result]
|
||||
(state/set-git-clone-repo! "")
|
||||
(state/set-current-repo! repo-url)
|
||||
(db/start-db-conn! (:me @state/state) repo-url)
|
||||
(db/mark-repo-as-cloned repo-url))
|
||||
(fn [e]
|
||||
(if (and (not fallback?)
|
||||
(or (string/includes? (str e) "401")
|
||||
(string/includes? (str e) "404")))
|
||||
(helper/request-app-tokens!
|
||||
(fn []
|
||||
(clone repo-url true))
|
||||
nil)
|
||||
(do
|
||||
(println "Clone failed, error: ")
|
||||
(js/console.error e)
|
||||
(state/set-cloning! false)
|
||||
(git-handler/set-git-status! repo-url :clone-failed)
|
||||
(git-handler/set-git-error! repo-url e)
|
||||
|
||||
(show-install-error! repo-url (util/format "Failed to clone %s." repo-url)))))))))
|
||||
(show-install-error! repo-url (util/format "Failed to clone %s." repo-url))))))))))
|
||||
|
||||
(defn set-config-content!
|
||||
[repo path new-config]
|
||||
@@ -543,18 +509,18 @@
|
||||
|
||||
(defn periodically-pull
|
||||
[repo-url pull-now?]
|
||||
(when-let [token (state/get-github-token repo-url)]
|
||||
(when pull-now? (pull repo-url token nil))
|
||||
(js/setInterval #(pull repo-url token nil)
|
||||
(* (config/git-pull-secs) 1000))))
|
||||
(p/let [token (helper/get-github-token repo-url)]
|
||||
(when token
|
||||
(when pull-now? (pull repo-url token nil))
|
||||
(js/setInterval #(pull repo-url token nil)
|
||||
(* (config/git-pull-secs) 1000)))))
|
||||
|
||||
(defn periodically-push-tasks
|
||||
[repo-url]
|
||||
(let [token (state/get-github-token repo-url)
|
||||
push (fn []
|
||||
(let [push (fn []
|
||||
(when (and (not (false? (:git-auto-push (state/get-config repo-url))))
|
||||
;; (not config/dev?)
|
||||
)
|
||||
;; (not config/dev?)
|
||||
)
|
||||
(push repo-url nil)))]
|
||||
(js/setInterval push
|
||||
(* (config/git-push-secs) 1000))))
|
||||
|
||||
Reference in New Issue
Block a user