From b8b60d9208bbc715ebe9b537c2795409d96b933b Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 3 Aug 2022 13:34:10 +0200 Subject: [PATCH] :tada: Add RPC command for clone builtin template --- backend/src/app/rpc/commands/binfile.clj | 8 +++- backend/src/app/rpc/commands/management.clj | 44 +++++++++++++++++- backend/src/app/rpc/mutations/management.clj | 2 +- backend/test/app/services_management_test.clj | 16 +++++++ backend/test/app/test_files/template.penpot | Bin 0 -> 799 bytes backend/test/app/test_helpers.clj | 10 +++- 6 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 backend/test/app/test_files/template.penpot diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index 36c51b0e63..c7bdf54dfa 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -536,7 +536,7 @@ :or {overwrite? false migrate? false timestamp (dt/now)} :as options}] - (us/assert! ::read-import-options options) + (us/verify! ::read-import-options options) (letfn [(lookup-index [id] (if ignore-index-errors? @@ -752,7 +752,11 @@ (case section :v1/rels (read-rels-section! input) :v1/files (read-files-section! input files) - :v1/sobjects (read-sobjects-section! input)))))))))) + :v1/sobjects (read-sobjects-section! input))) + + ;; Knowing that the ids of the created files are in + ;; index, just lookup them and return it as a set + (into #{} (keep #(get @*index* %)) files)))))))) (defn export! [cfg] diff --git a/backend/src/app/rpc/commands/management.clj b/backend/src/app/rpc/commands/management.clj index a78bfe3104..25cccad76e 100644 --- a/backend/src/app/rpc/commands/management.clj +++ b/backend/src/app/rpc/commands/management.clj @@ -13,6 +13,7 @@ [app.common.spec :as us] [app.common.uuid :as uuid] [app.db :as db] + [app.rpc.commands.binfile :as binfile] [app.rpc.doc :as-alias doc] [app.rpc.mutations.projects :refer [create-project-role create-project]] [app.rpc.queries.projects :as proj] @@ -204,7 +205,9 @@ :opt-un [::name])) (sv/defmethod ::duplicate-project - [{:keys [pool] :as cfg} {:keys [profile-id project-id] :as params}] + "Duplicate an entire project with all the files" + {::doc/added "1.16"} + [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (duplicate-project conn params))) @@ -310,6 +313,8 @@ (s/keys :req-un [::profile-id ::ids ::project-id])) (sv/defmethod ::move-files + "Move a set of files from one project to other." + {::doc/added "1.16"} [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (move-files conn params))) @@ -347,6 +352,43 @@ (s/keys :req-un [::profile-id ::team-id ::project-id])) (sv/defmethod ::move-project + "Move projects between teams." + {::doc/added "1.16"} [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (move-project conn params))) + +;; --- COMMAND: Clone Template + +(declare clone-template) + +(s/def ::template-id ::us/not-empty-string) +(s/def ::clone-template + (s/keys :req-un [::profile-id ::project-id ::template-id])) + +(sv/defmethod ::clone-template + "Clone into the specified project the template by its id." + {::doc/added "1.16"} + [{:keys [pool] :as cfg} params] + (db/with-atomic [conn pool] + (-> (assoc cfg :conn conn) + (clone-template params)))) + +(defn- clone-template + [{:keys [conn templates] :as cfg} {:keys [profile-id template-id project-id]}] + (let [template (d/seek #(= (:id %) template-id) templates) + project (db/get-by-id conn :project project-id {:columns [:id :team-id]})] + + (teams/check-edition-permissions! conn profile-id (:team-id project)) + + (when-not template + (ex/raise :type :not-found + :code :template-not-found + :hint "template not found")) + + (-> cfg + (assoc ::binfile/input (:path template)) + (assoc ::binfile/project-id (:id project)) + (assoc ::binfile/ignore-index-errors? true) + (assoc ::binfile/migrate? true) + (binfile/import!)))) diff --git a/backend/src/app/rpc/mutations/management.clj b/backend/src/app/rpc/mutations/management.clj index c37d3a6455..c2038868ae 100644 --- a/backend/src/app/rpc/mutations/management.clj +++ b/backend/src/app/rpc/mutations/management.clj @@ -31,7 +31,7 @@ (sv/defmethod ::duplicate-project {::doc/added "1.2" ::doc/deprecated "1.16"} - [{:keys [pool] :as cfg} {:keys [profile-id project-id] :as params}] + [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (cmd.mgm/duplicate-project conn params))) diff --git a/backend/test/app/services_management_test.clj b/backend/test/app/services_management_test.clj index bcfa277f96..2724ee1dbd 100644 --- a/backend/test/app/services_management_test.clj +++ b/backend/test/app/services_management_test.clj @@ -604,3 +604,19 @@ (t/is (= (:library-file-id item1) (:id file2)))) ))) + +(t/deftest clone-template + (let [prof (th/create-profile* 1 {:is-active true}) + data {::th/type :clone-template + :profile-id (:id prof) + :project-id (:default-project-id prof) + :template-id "test"} + + out (th/command! data)] + ;; (th/print-result! out) + + (t/is (nil? (:error out))) + (let [result (:result out)] + (t/is (set? result)) + (t/is (uuid? (first result))) + (t/is (= 1 (count result)))))) diff --git a/backend/test/app/test_files/template.penpot b/backend/test/app/test_files/template.penpot new file mode 100644 index 0000000000000000000000000000000000000000..1375c6d525936b08b0619a22df8cc06b20e7924b GIT binary patch literal 799 zcmV+)1K|89wJ-euV4N5Lrf8B!0L`ic6E+OQ3JuulEDrU_1V^=&^?!9LH%USN*MNJV znwTaqNun*~#;&8ECMz_@gf={cPTB%=PC>K=>NMGSJA0KSY@EjJ;h!0I7yyW~U~ zl2Tx`-7HLS91Rf0>QUJ6Z~%r?i(xhJl>k9tOZ_t(P^*N}+M!_emMsQo+)CYu%)FXq{B7Ub;%O#ii5 z{5q0Z5A~m2mAbD!QgfL!Y@a^PH#Z}0ZbpFMST;WmNf+cT%=n})&0j&XvWwU1u+IY_ zI!oFmM;F@3N^rjJ&MHA5h?o`=lEE44agAQL3m zQej#HgHSL|9y~q&1uPM8>CBMBCfXKF2#x>?$QWwVP9hRkv)RRGFgr#r35jtDGzL() zE7=A{Gk_8!`2g&VXG)MU=@*DNdr9A8*$cqFUI8ACl>%`3)WZn)F*$^J`<3Bqs(&q& zI?mGsf&z>Uw{pmUhJ3cfaR^8Tx*kgz0*003vuc9j4C literal 0 HcmV?d00001 diff --git a/backend/test/app/test_helpers.clj b/backend/test/app/test_helpers.clj index 8f436ea02d..13c5f0fd03 100644 --- a/backend/test/app/test_helpers.clj +++ b/backend/test/app/test_helpers.clj @@ -50,11 +50,18 @@ (defn state-init [next] - (let [config (-> main/system-config + (let [templates [{:id "test" + :name "test" + :file-uri "test" + :thumbnail-uri "test" + :path (-> "app/test_files/template.penpot" io/resource fs/path)}] + + config (-> main/system-config (assoc-in [:app.msgbus/msgbus :redis-uri] (:redis-uri config)) (assoc-in [:app.db/pool :uri] (:database-uri config)) (assoc-in [:app.db/pool :username] (:database-username config)) (assoc-in [:app.db/pool :password] (:database-password config)) + (assoc-in [:app.rpc/methods :templates] templates) (dissoc :app.srepl/server :app.http/server :app.http/router @@ -64,6 +71,7 @@ :app.auth.oidc/gitlab-provider :app.auth.oidc/github-provider :app.auth.oidc/generic-provider + :app.setup/builtin-templates :app.auth.oidc/routes ;; :app.auth.ldap/provider :app.worker/executors-monitor