Skip to content

Commit

Permalink
fix(spec): add some specs in repo.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
defclass committed Nov 25, 2020
1 parent b8dc610 commit a650a1c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
33 changes: 30 additions & 3 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require [frontend.util :as util :refer-macros [profile]]
[frontend.fs :as fs]
[promesa.core :as p]
[lambdaisland.glogi :as log]
[frontend.state :as state]
[frontend.db :as db]
[frontend.git :as git]
Expand All @@ -21,14 +22,16 @@
[cljs.reader :as reader]
[clojure.string :as string]
[frontend.dicts :as dicts]
[frontend.helper :as helper]))
[frontend.helper :as helper]
[frontend.spec :as spec]))

;; Project settings should be checked in two situations:
;; 1. User changes the config.edn directly in logseq.com (fn: alter-file)
;; 2. Git pulls the new change (fn: load-files)

(defn show-install-error!
[repo-url title]
(spec/validate :repos/url repo-url)
(notification/show!
[:p.content
title
Expand All @@ -49,6 +52,7 @@

(defn create-config-file-if-not-exists
[repo-url]
(spec/validate :repos/url repo-url)
(let [repo-dir (util/get-repo-dir repo-url)
app-dir config/app-name
dir (str repo-dir "/" app-dir)]
Expand Down Expand Up @@ -76,6 +80,7 @@

(defn create-contents-file
[repo-url]
(spec/validate :repos/url repo-url)
(let [repo-dir (util/get-repo-dir repo-url)
format (state/get-preferred-format)
path (str "pages/contents." (if (= (name format) "markdown")
Expand All @@ -92,6 +97,7 @@

(defn create-custom-theme
[repo-url]
(spec/validate :repos/url repo-url)
(let [repo-dir (util/get-repo-dir repo-url)
path (str config/app-name "/" config/custom-css-file)
file-path (str "/" path)
Expand All @@ -105,6 +111,7 @@

(defn create-dummy-notes-page
[repo-url content]
(spec/validate :repos/url repo-url)
(let [repo-dir (util/get-repo-dir repo-url)
path (str (config/get-pages-directory) "/how_to_make_dummy_notes.md")
file-path (str "/" path)]
Expand All @@ -117,6 +124,7 @@
([repo-url]
(create-today-journal-if-not-exists repo-url nil))
([repo-url content]
(spec/validate :repos/url repo-url)
(let [repo-dir (util/get-repo-dir repo-url)
format (state/get-preferred-format repo-url)
title (date/today)
Expand Down Expand Up @@ -152,6 +160,7 @@

(defn create-default-files!
[repo-url]
(spec/validate :repos/url repo-url)
(when-let [name (get-in @state/state [:me :name])]
(create-config-file-if-not-exists repo-url)
(create-today-journal-if-not-exists repo-url)
Expand All @@ -160,6 +169,7 @@

(defn load-repo-to-db!
[repo-url diffs first-clone?]
(spec/validate :repos/url repo-url)
(let [load-contents (fn [files delete-files delete-blocks re-render?]
(file-handler/load-files-contents!
repo-url
Expand Down Expand Up @@ -211,18 +221,21 @@

(defn persist-repo!
[repo]
(spec/validate :repos/url repo)
(when-let [files-conn (db/get-files-conn repo)]
(db/persist repo @files-conn true))
(when-let [db (db/get-conn repo)]
(db/persist repo db false)))

(defn load-db-and-journals!
[repo-url diffs first-clone?]
(spec/validate :repos/url repo-url)
(when (or diffs first-clone?)
(load-repo-to-db! repo-url diffs first-clone?)))

(defn transact-react-and-alter-file!
[repo tx transact-option files]
(spec/validate :repos/url repo)
(let [files (remove nil? files)
pages (->> (map db/get-file-page (map first files))
(remove nil?))]
Expand All @@ -239,6 +252,7 @@

(defn persist-repo-metadata!
[repo]
(spec/validate :repos/url repo)
(let [files (db/get-files repo)]
(when (seq files)
(let [data (db/get-sync-metadata repo)
Expand All @@ -259,6 +273,7 @@
[repo-url {:keys [force-pull? show-diff?]
:or {force-pull? false
show-diff? false}}]
(spec/validate :repos/url repo-url)
(when (and
(db/get-conn repo-url true)
(db/cloned? repo-url))
Expand Down Expand Up @@ -345,6 +360,7 @@
diff-push? false
commit-push? false
force? false}}]
(spec/validate :repos/url repo-url)
(let [status (db/get-key-value repo-url :git/status)]
(when (or
commit-push?
Expand Down Expand Up @@ -373,7 +389,7 @@
(git-handler/set-git-error! repo-url nil)
(common-handler/check-changed-files-status repo-url))
(fn [error]
(println "Git push error: ")
(log/error :git/push-error error)
(js/console.error error)
(common-handler/check-changed-files-status repo-url)
(do
Expand All @@ -383,13 +399,14 @@
(pull repo-url {:force-pull? true
:show-diff? true}))))))))))
(p/catch (fn [error]
(println "Git push error: ")
(log/error :git/get-changed-files-error error)
(git-handler/set-git-status! repo-url :push-failed)
(git-handler/set-git-error! repo-url error)
(js/console.dir error)))))))

(defn push-if-auto-enabled!
[repo]
(spec/validate :repos/url repo)
(when (state/git-auto-push?)
(push repo nil)))

Expand All @@ -400,6 +417,7 @@

(defn clone
[repo-url]
(spec/validate :repos/url repo-url)
(p/let [token (helper/get-github-token repo-url)]
(when token
(util/p-handle
Expand Down Expand Up @@ -443,6 +461,7 @@

(defn remove-repo!
[{:keys [id url] :as repo}]
(spec/validate :repos/repo repo)
(util/delete (str config/api "repos/" id)
(fn []
(db/remove-conn! url)
Expand Down Expand Up @@ -476,6 +495,7 @@

(defn periodically-pull
[repo-url pull-now?]
(spec/validate :repos/url repo-url)
(p/let [token (helper/get-github-token repo-url)]
(when token
(when pull-now? (pull repo-url nil))
Expand All @@ -484,6 +504,7 @@

(defn periodically-push-tasks
[repo-url]
(spec/validate :repos/url repo-url)
(let [push (fn []
(when (and (not (false? (:git-auto-push (state/get-config repo-url))))
;; (not config/dev?)
Expand All @@ -495,11 +516,14 @@
(defn periodically-pull-and-push
[repo-url {:keys [pull-now?]
:or {pull-now? true}}]
(spec/validate :repos/url repo-url)
(periodically-pull repo-url pull-now?)
(periodically-push-tasks repo-url))

(defn create-repo!
[repo-url branch]
(spec/validate :repos/url repo-url)
(spec/validate :repos/branch branch)
(util/post (str config/api "repos")
{:url repo-url
:branch branch}
Expand All @@ -513,6 +537,7 @@

(defn clone-and-pull
[repo-url]
(spec/validate :repos/url repo-url)
(->
(p/let [_ (clone repo-url)
_ (git-handler/git-set-username-email! repo-url (:me @state/state))]
Expand All @@ -525,6 +550,7 @@

(defn clone-and-pull-repos
[me]
(spec/validate :state/me me)
(if (and js/window.git js/window.pfs)
(doseq [{:keys [id url]} (:repos me)]
(let [repo url]
Expand All @@ -545,6 +571,7 @@

(defn rebuild-index!
[{:keys [id url] :as repo}]
(spec/validate :me/repos+ repo)
(db/remove-conn! url)
(db/clear-query-state!)
(-> (p/let [_ (db/remove-db! url)
Expand Down
5 changes: 4 additions & 1 deletion src/main/frontend/helper.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[lambdaisland.glogi :as log]
[frontend.util :as util]
[frontend.state :as state]
[frontend.config :as config]))
[frontend.config :as config]
[frontend.spec :as spec]))

(defn request-app-tokens!
[ok-handler error-handler]
Expand All @@ -27,9 +28,11 @@

(defn- get-github-token*
[repo]
(spec/validate :repos/url repo)
(when repo
(let [{:keys [token expires_at] :as token-state}
(state/get-github-token repo)]
(spec/validate :repos/repo token-state)
(if (and (map? token-state)
(string? expires_at))
(let [expires-at (tf/parse (tf/formatters :date-time-no-ms) expires_at)
Expand Down
55 changes: 49 additions & 6 deletions src/main/frontend/spec.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,56 @@

(set! s/*explain-out* expound/printer)

(defn validate [spec value]
(when-let [error (s/explain-data spec value)]
(if config/dev?
(throw (ex-info (expound/expound-str spec value) error))
(js/console.log (expound/expound-str spec value)))))
(defn validate
"This function won't crash the current thread, just log error."
[spec value]
(if (s/explain-data spec value)
(let [error-message (expound/expound-str spec value)
ex (ex-info "Error in validate" nil)]
(log/error :exception ex :spec/validate-failed error-message)
false)
true))

;; repo

(s/def :repos/id string?)
(s/def :repos/url string?)
(s/def :repos/branch string?)
(s/def :repos/installation_id string?)
(s/def :repos/token string?)
(s/def :repos/expires_at string?)
(s/def :repos/repo (s/keys :req-un [:repos/id :repos/url :repos/branch :repos/installation_id]
:opt-un [:repos/token :repos/expires_at]))

; Didn't know how to impl `require token` version in :me key.
(s/def :repos/repo-require-token (s/keys :req-un [:repos/id :repos/url :repos/branch :repos/installation_id
:repos/token :repos/expires_at]))

(s/def :me/repos (s/* :repos/repo))


;; project

(s/def :projects/name string?)
(s/def :projects/repo string?)
(s/def :projects/project (s/keys :req-un [:projects/name :projects/repo]))
(s/def :me/projects (s/* :projects/project))

;; me

(s/def :me/name string?)
(s/def :me/email string?)
(s/def :me/avatar string?)
(s/def :me/preferred_format string?)
(s/def :me/preferred_workflow string?)
(s/def :me/cors_proxy (s/or :nil nil?
:string string?))

;; state

(s/def :state/me (s/keys :req-un [:me/name :me/email :me/avatar :me/repos :me/projects :me/preferred_format
:me/preferred_workflow :me/cors_proxy]))

(s/def :user/repo string?)

(comment
(validate :user/repo 1))

0 comments on commit a650a1c

Please sign in to comment.