0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-10 17:18:21 -05:00

Merge pull request #2882 from penpot/niwinz-webhooks-fix

🐛 Fix incorrect state management on webhooks crud
This commit is contained in:
Alejandro 2023-02-01 10:38:33 +01:00 committed by GitHub
commit 037ba19e87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 24 deletions

View file

@ -8,6 +8,7 @@
(:require (:require
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.uri :as u]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.db :as db] [app.db :as db]
[app.http.client :as http] [app.http.client :as http]
@ -22,10 +23,15 @@
[cuerdas.core :as str] [cuerdas.core :as str]
[promesa.core :as p])) [promesa.core :as p]))
(defn decode-row
[{:keys [uri] :as row}]
(cond-> row
(string? uri) (assoc :uri (u/uri uri))))
;; --- Mutation: Create Webhook ;; --- Mutation: Create Webhook
(s/def ::team-id ::us/uuid) (s/def ::team-id ::us/uuid)
(s/def ::uri ::us/not-empty-string) (s/def ::uri ::us/uri)
(s/def ::is-active ::us/boolean) (s/def ::is-active ::us/boolean)
(s/def ::mtype (s/def ::mtype
#{"application/json" #{"application/json"
@ -59,7 +65,7 @@
(if (not= (:uri whook) (:uri params)) (if (not= (:uri whook) (:uri params))
(->> (http/req! cfg {:method :head (->> (http/req! cfg {:method :head
:uri (:uri params) :uri (str (:uri params))
:timeout (dt/duration "3s")}) :timeout (dt/duration "3s")})
(p/hmap (fn [response exception] (p/hmap (fn [response exception]
(if exception (if exception
@ -79,22 +85,24 @@
(defn- insert-webhook! (defn- insert-webhook!
[{:keys [::db/pool]} {:keys [team-id uri mtype is-active] :as params}] [{:keys [::db/pool]} {:keys [team-id uri mtype is-active] :as params}]
(db/insert! pool :webhook (-> (db/insert! pool :webhook
{:id (uuid/next) {:id (uuid/next)
:team-id team-id :team-id team-id
:uri uri :uri (str uri)
:is-active is-active :is-active is-active
:mtype mtype})) :mtype mtype})
(decode-row)))
(defn- update-webhook! (defn- update-webhook!
[{:keys [::db/pool] :as cfg} {:keys [id] :as wook} {:keys [uri mtype is-active] :as params}] [{:keys [::db/pool] :as cfg} {:keys [id] :as wook} {:keys [uri mtype is-active] :as params}]
(db/update! pool :webhook (-> (db/update! pool :webhook
{:uri uri {:uri (str uri)
:is-active is-active :is-active is-active
:mtype mtype :mtype mtype
:error-code nil :error-code nil
:error-count 0} :error-count 0}
{:id id})) {:id id})
(decode-row)))
(sv/defmethod ::create-webhook (sv/defmethod ::create-webhook
{::doc/added "1.17"} {::doc/added "1.17"}
@ -110,7 +118,7 @@
(sv/defmethod ::update-webhook (sv/defmethod ::update-webhook
{::doc/added "1.17"} {::doc/added "1.17"}
[{:keys [::db/pool ::wrk/executor] :as cfg} {:keys [::rpc/profile-id id] :as params}] [{:keys [::db/pool ::wrk/executor] :as cfg} {:keys [::rpc/profile-id id] :as params}]
(let [whook (db/get pool :webhook {:id id})] (let [whook (-> (db/get pool :webhook {:id id}) (decode-row))]
(check-edition-permissions! pool profile-id (:team-id whook)) (check-edition-permissions! pool profile-id (:team-id whook))
(->> (validate-webhook! cfg whook params) (->> (validate-webhook! cfg whook params)
(p/fmap executor (fn [_] (update-webhook! cfg whook params)))))) (p/fmap executor (fn [_] (update-webhook! cfg whook params))))))
@ -123,7 +131,7 @@
{::doc/added "1.17"} {::doc/added "1.17"}
[{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id]}] [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id]}]
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(let [whook (db/get conn :webhook {:id id})] (let [whook (-> (db/get conn :webhook {:id id}) decode-row)]
(check-edition-permissions! conn profile-id (:team-id whook)) (check-edition-permissions! conn profile-id (:team-id whook))
(db/delete! conn :webhook {:id id}) (db/delete! conn :webhook {:id id})
nil))) nil)))
@ -143,4 +151,5 @@
[{:keys [pool] :as cfg} {:keys [::rpc/profile-id team-id]}] [{:keys [pool] :as cfg} {:keys [::rpc/profile-id team-id]}]
(with-open [conn (db/open pool)] (with-open [conn (db/open pool)]
(check-read-permissions! conn profile-id team-id) (check-read-permissions! conn profile-id team-id)
(db/exec! conn [sql:get-webhooks team-id]))) (->> (db/exec! conn [sql:get-webhooks team-id])
(mapv decode-row))))

View file

@ -6,11 +6,12 @@
(ns backend-tests.rpc-webhooks-test (ns backend-tests.rpc-webhooks-test
(:require (:require
[app.common.uri :as u]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.db :as db] [app.db :as db]
[app.http :as http] [app.http :as http]
[app.storage :as sto]
[app.rpc :as-alias rpc] [app.rpc :as-alias rpc]
[app.storage :as sto]
[backend-tests.helpers :as th] [backend-tests.helpers :as th]
[clojure.test :as t] [clojure.test :as t]
[mockery.core :refer [with-mocks]])) [mockery.core :refer [with-mocks]]))
@ -31,7 +32,7 @@
(let [params {::th/type :create-webhook (let [params {::th/type :create-webhook
::rpc/profile-id (:id prof) ::rpc/profile-id (:id prof)
:team-id team-id :team-id team-id
:uri "http://example.com" :uri (u/uri "http://example.com")
:mtype "application/json"} :mtype "application/json"}
out (th/command! params)] out (th/command! params)]

View file

@ -637,7 +637,9 @@
{::mf/register modal/components {::mf/register modal/components
::mf/register-as :webhook} ::mf/register-as :webhook}
[{:keys [webhook] :as props}] [{:keys [webhook] :as props}]
(let [initial (mf/use-memo (fn [] (or webhook {:is-active false :mtype "application/json"}))) ;; FIXME: this is a workaround because input fields do not support rendering hooks
(let [initial (mf/use-memo (fn [] (or (some-> webhook (update :uri str))
{:is-active false :mtype "application/json"})))
form (fm/use-form :spec ::webhook-form form (fm/use-form :spec ::webhook-form
:initial initial) :initial initial)
on-success on-success
@ -735,8 +737,6 @@
] ]
[:div.explain (tr "dashboard.webhooks.active.explain")]]] [:div.explain (tr "dashboard.webhooks.active.explain")]]]
[:div.modal-footer [:div.modal-footer
[:div.action-buttons [:div.action-buttons
[:input.cancel-button [:input.cancel-button
@ -827,7 +827,7 @@
{:success? (nil? error-code) {:success? (nil? error-code)
:text last-delivery-text}]]] :text last-delivery-text}]]]
[:div.table-field.uri [:div.table-field.uri
[:div (:uri webhook)]] [:div (dm/str (:uri webhook))]]
[:div.table-field.active [:div.table-field.active
[:div (if (:is-active webhook) [:div (if (:is-active webhook)
(tr "labels.active") (tr "labels.active")