Merge pull request #2642 from penpot/niwinz-backend-webhooks-4

🎉 Add webhooks processing & errors UI integration
This commit is contained in:
Alejandro 2022-12-14 15:53:11 +01:00 committed by GitHub
commit c9ad82edc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 1187 additions and 515 deletions

View file

@ -64,7 +64,7 @@
(defn initialize
[{:keys [id] :as params}]
(us/assert ::us/uuid id)
(us/assert! ::us/uuid id)
(ptk/reify ::initialize
ptk/UpdateEvent
(update [_ state]
@ -201,7 +201,7 @@
(defn search
[params]
(us/assert ::search params)
(us/assert! ::search params)
(ptk/reify ::search
ptk/UpdateEvent
(update [_ state]
@ -236,7 +236,7 @@
(defn fetch-files
[{:keys [project-id] :as params}]
(us/assert ::us/uuid project-id)
(us/assert! ::us/uuid project-id)
(ptk/reify ::fetch-files
ptk/WatchEvent
(watch [_ _ _]
@ -347,7 +347,7 @@
(defn toggle-file-select
[{:keys [id project-id] :as file}]
(us/assert ::file file)
(us/assert! ::file file)
(ptk/reify ::toggle-file-select
ptk/UpdateEvent
(update [_ state]
@ -377,7 +377,7 @@
(defn create-team
[{:keys [name] :as params}]
(us/assert string? name)
(us/assert! ::us/string name)
(ptk/reify ::create-team
ptk/WatchEvent
(watch [_ _ _]
@ -394,7 +394,7 @@
(defn create-team-with-invitations
[{:keys [name emails role] :as params}]
(us/assert string? name)
(us/assert! ::us/string name)
(ptk/reify ::create-team-with-invitations
ptk/WatchEvent
(watch [_ _ _]
@ -413,7 +413,7 @@
(defn update-team
[{:keys [id name] :as params}]
(us/assert ::team params)
(us/assert! ::team params)
(ptk/reify ::update-team
ptk/UpdateEvent
(update [_ state]
@ -426,7 +426,7 @@
(defn update-team-photo
[{:keys [file] :as params}]
(us/assert ::di/file file)
(us/assert! ::di/file file)
(ptk/reify ::update-team-photo
ptk/WatchEvent
(watch [_ state _]
@ -447,8 +447,8 @@
(defn update-team-member-role
[{:keys [role member-id] :as params}]
(us/assert ::us/uuid member-id)
(us/assert ::us/keyword role)
(us/assert! ::us/uuid member-id)
(us/assert! ::us/keyword role)
(ptk/reify ::update-team-member-role
ptk/WatchEvent
(watch [_ state _]
@ -461,7 +461,7 @@
(defn delete-team-member
[{:keys [member-id] :as params}]
(us/assert ::us/uuid member-id)
(us/assert! ::us/uuid member-id)
(ptk/reify ::delete-team-member
ptk/WatchEvent
(watch [_ state _]
@ -474,7 +474,9 @@
(defn leave-team
[{:keys [reassign-to] :as params}]
(us/assert (s/nilable ::us/uuid) reassign-to)
(us/assert!
:spec (s/nilable ::us/uuid)
:val reassign-to)
(ptk/reify ::leave-team
ptk/WatchEvent
(watch [_ state _]
@ -510,9 +512,9 @@
(defn update-team-invitation-role
[{:keys [email team-id role] :as params}]
(us/assert ::us/email email)
(us/assert ::us/uuid team-id)
(us/assert ::us/keyword role)
(us/assert! ::us/email email)
(us/assert! ::us/uuid team-id)
(us/assert! ::us/keyword role)
(ptk/reify ::update-team-invitation-role
IDeref
(-deref [_] {:role role})
@ -528,8 +530,8 @@
(defn delete-team-invitation
[{:keys [email team-id] :as params}]
(us/assert ::us/email email)
(us/assert ::us/uuid team-id)
(us/assert! ::us/email email)
(us/assert! ::us/uuid team-id)
(ptk/reify ::delete-team-invitation
ptk/WatchEvent
(watch [_ _ _]
@ -542,7 +544,7 @@
(defn delete-team-webhook
[{:keys [id] :as params}]
(us/assert ::us/uuid id)
(us/assert! ::us/uuid id)
(ptk/reify ::delete-team-webhook
ptk/WatchEvent
(watch [_ state _]
@ -562,10 +564,10 @@
(defn update-team-webhook
[{:keys [id uri mtype is-active] :as params}]
(us/assert ::us/uuid id)
(us/assert ::us/uri uri)
(us/assert ::mtype mtype)
(us/assert ::us/boolean is-active)
(us/assert! ::us/uuid id)
(us/assert! ::us/uri uri)
(us/assert! ::mtype mtype)
(us/assert! ::us/boolean is-active)
(ptk/reify ::update-team-webhook
ptk/WatchEvent
(watch [_ state _]
@ -580,9 +582,9 @@
(defn create-team-webhook
[{:keys [uri mtype is-active] :as params}]
(us/assert ::us/uri uri)
(us/assert ::mtype mtype)
(us/assert ::us/boolean is-active)
(us/assert! ::us/uri uri)
(us/assert! ::mtype mtype)
(us/assert! ::us/boolean is-active)
(ptk/reify ::create-team-webhook
ptk/WatchEvent
(watch [_ state _]
@ -599,7 +601,7 @@
(defn delete-team
[{:keys [id] :as params}]
(us/assert ::team params)
(us/assert! ::team params)
(ptk/reify ::delete-team
ptk/WatchEvent
(watch [_ _ _]
@ -652,7 +654,7 @@
(defn duplicate-project
[{:keys [id name] :as params}]
(us/assert ::us/uuid id)
(us/assert! ::us/uuid id)
(ptk/reify ::duplicate-project
ptk/WatchEvent
(watch [_ _ _]
@ -669,8 +671,8 @@
(defn move-project
[{:keys [id team-id] :as params}]
(us/assert ::us/uuid id)
(us/assert ::us/uuid team-id)
(us/assert! ::us/uuid id)
(us/assert! ::us/uuid team-id)
(ptk/reify ::move-project
IDeref
(-deref [_]
@ -688,7 +690,7 @@
(defn toggle-project-pin
[{:keys [id is-pinned] :as project}]
(us/assert ::project project)
(us/assert! ::project project)
(ptk/reify ::toggle-project-pin
ptk/UpdateEvent
(update [_ state]
@ -705,7 +707,7 @@
(defn rename-project
[{:keys [id name] :as params}]
(us/assert ::project params)
(us/assert! ::project params)
(ptk/reify ::rename-project
ptk/UpdateEvent
(update [_ state]
@ -723,7 +725,7 @@
(defn delete-project
[{:keys [id] :as params}]
(us/assert ::project params)
(us/assert! ::project params)
(ptk/reify ::delete-project
ptk/UpdateEvent
(update [_ state]
@ -745,7 +747,7 @@
(defn delete-file
[{:keys [id project-id] :as params}]
(us/assert ::file params)
(us/assert! ::file params)
(ptk/reify ::delete-file
ptk/UpdateEvent
(update [_ state]
@ -764,7 +766,7 @@
(defn rename-file
[{:keys [id name] :as params}]
(us/assert ::file params)
(us/assert! ::file params)
(ptk/reify ::rename-file
IDeref
(-deref [_]
@ -787,7 +789,7 @@
(defn set-file-shared
[{:keys [id is-shared] :as params}]
(us/assert ::file params)
(us/assert! ::file params)
(ptk/reify ::set-file-shared
IDeref
(-deref [_]
@ -828,7 +830,7 @@
(defn create-file
[{:keys [project-id] :as params}]
(us/assert ::us/uuid project-id)
(us/assert! ::us/uuid project-id)
(ptk/reify ::create-file
IDeref
@ -857,8 +859,8 @@
(defn duplicate-file
[{:keys [id name] :as params}]
(us/assert ::us/uuid id)
(us/assert ::name name)
(us/assert! ::us/uuid id)
(us/assert! ::name name)
(ptk/reify ::duplicate-file
ptk/WatchEvent
(watch [_ _ _]
@ -877,8 +879,8 @@
(defn move-files
[{:keys [ids project-id] :as params}]
(us/assert ::us/set-of-uuid ids)
(us/assert ::us/uuid project-id)
(us/assert! ::us/set-of-uuid ids)
(us/assert! ::us/uuid project-id)
(ptk/reify ::move-files
IDeref
(-deref [_]
@ -898,7 +900,7 @@
;; --- EVENT: clone-template
(defn clone-template
[{:keys [template-id project-id] :as params}]
(us/assert ::us/uuid project-id)
(us/assert! ::us/uuid project-id)
(ptk/reify ::clone-template
IDeref
(-deref [_]
@ -920,7 +922,7 @@
(defn go-to-workspace
[{:keys [id project-id] :as file}]
(us/assert ::file file)
(us/assert! ::file file)
(ptk/reify ::go-to-workspace
ptk/WatchEvent
(watch [_ _ _]

View file

@ -38,7 +38,7 @@
(defn- collect-context
[]
(let [uagent (UAParser.)]
(d/merge
(merge
{:app-version (:full @cf/version)
:locale @i18n/locale}
(let [browser (.getBrowser uagent)]
@ -215,12 +215,17 @@
(defn- persist-events
[events]
(if (seq events)
(let [uri (u/join @cf/public-uri "api/audit/events")
(let [uri (u/join @cf/public-uri "api/rpc/command/push-audit-events")
params {:uri uri
:method :post
:credentials "include"
:body (http/transit-data {:events events})}]
(->> (http/send! params)
(rx/mapcat rp/handle-response)))
(rx/mapcat rp/handle-response)
(rx/catch (fn [_]
(l/error :hint "unexpected error on persisting audit events")
(rx/of nil)))))
(rx/of nil)))
(defn initialize
@ -274,7 +279,7 @@
(rx/map (fn [event]
(let [session* (or @session (dt/now))
context (-> @context
(d/merge (:context event))
(merge (:context event))
(assoc :session session*))]
(reset! session session*)
(-> event

View file

@ -60,6 +60,7 @@
http/conditional-decode-transit)]
(->> (http/send! {:method :get
:uri (u/join @cf/public-uri "api/rpc/query/" (name id))
:headers {"accept" "application/transit+json"}
:credentials "include"
:query params})
(rx/map decode-transit)
@ -71,6 +72,7 @@
[id params]
(->> (http/send! {:method :post
:uri (u/join @cf/public-uri "api/rpc/mutation/" (name id))
:headers {"accept" "application/transit+json"}
:credentials "include"
:body (http/transit-data params)})
(rx/map http/conditional-decode-transit)
@ -88,6 +90,7 @@
(->> (http/send! {:method method
:uri (u/join @cf/public-uri "api/rpc/command/" (name id))
:credentials "include"
:headers {"accept" "application/transit+json"}
:body (when (= method :post)
(if form-data?
(http/form-data params)

View file

@ -587,53 +587,78 @@
(s/def ::webhook-form
(s/keys :req-un [::uri ::mtype]))
(mf/defc webhook-modal {::mf/register modal/components
::mf/register-as :webhook}
(def valid-webhook-mtypes
[{:label "application/json" :value "application/json"}
{:label "application/x-www-form-urlencoded" :value "application/x-www-form-urlencoded"}
{:label "application/transit+json" :value "application/transit+json"}])
(defn- extract-status
[error-code]
(-> error-code (str/split #":") second))
(mf/defc webhook-modal
{::mf/register modal/components
::mf/register-as :webhook}
[{:keys [webhook] :as props}]
(let [initial (mf/use-memo (fn [] (or webhook {:is-active false :mtype "application/json"})))
form (fm/use-form :spec ::webhook-form
:initial initial)
mtypes [{:label "application/json" :value "application/json"}
{:label "application/x-www-form-urlencoded" :value "application/x-www-form-urlencoded"}
{:label "application/transit+json" :value "application/transit+json"}]
on-success
(fn [message]
(st/emit! (dd/fetch-team-webhooks)
(msg/success message)
(modal/hide)))
(mf/use-fn
(fn [_]
(let [message (tr "dashboard.webhooks.create.success")]
(st/emit! (dd/fetch-team-webhooks)
(msg/success message)
(modal/hide)))))
on-error
(fn [message {:keys [type code hint] :as error}]
(let [message (if (and (= type :validation) (= code :webhook-validation))
(str message " "
(case hint
"ssl-validation" (tr "errors.webhooks.ssl-validation")
"")) ;; TODO Add more error codes when back defines them
message)]
(rx/of (msg/error message))))
(mf/use-fn
(fn [form {:keys [type code hint] :as error}]
(if (and (= type :validation)
(= code :webhook-validation))
(let [message (cond
(= hint "unknown")
(tr "errors.webhooks.unexpected")
(= hint "ssl-validation-error")
(tr "errors.webhooks.ssl-validation")
(= hint "timeout")
(tr "errors.webhooks.timeout")
(= hint "connection-error")
(tr "errors.webhooks.connection")
(str/starts-with? hint "unexpected-status")
(tr "errors.webhooks.unexpected-status" (extract-status hint)))]
(swap! form assoc-in [:errors :uri] {:message message}))
(rx/throw error))))
on-create-submit
(fn []
(let [mdata {:on-success #(on-success (tr "dashboard.webhooks.create.success"))
:on-error (partial on-error (tr "dashboard.webhooks.create.error"))}
webhook {:uri (get-in @form [:clean-data :uri])
:mtype (get-in @form [:clean-data :mtype])
:is-active (get-in @form [:clean-data :is-active])}]
(st/emit! (dd/create-team-webhook (with-meta webhook mdata)))))
(mf/use-fn
(fn [form]
(let [cdata (:clean-data @form)
mdata {:on-success (partial on-success form)
:on-error (partial on-error form)}
params {:uri (:uri cdata)
:mtype (:mtype cdata)
:is-active (:is-active cdata)}]
(st/emit! (dd/create-team-webhook
(with-meta params mdata))))))
on-update-submit
(fn []
(let [mdata {:on-success #(on-success (tr "dashboard.webhooks.update.success"))
:on-error (partial on-error (tr "dashboard.webhooks.update.error"))}
webhook (get @form :clean-data)]
(st/emit! (dd/update-team-webhook (with-meta webhook mdata)))))
(mf/use-fn
(fn [form]
(let [params (:clean-data @form)
mdata {:on-success (partial on-success form)
:on-error (partial on-error form)}]
(st/emit! (dd/update-team-webhook
(with-meta params mdata))))))
on-submit
#(let [data (:clean-data @form)]
(if (:id data)
(on-update-submit)
(on-create-submit)))]
(mf/use-fn
(fn [form]
(prn @form)
(let [data (:clean-data @form)]
(if (:id data)
(on-update-submit form)
(on-create-submit form)))))]
[:div.modal-overlay
[:div.modal-container.webhooks-modal
@ -659,7 +684,7 @@
:placeholder (tr "modals.create-webhook.url.placeholder")}]]
[:div.fields-row
[:& fm/select {:options mtypes
[:& fm/select {:options valid-webhook-mtypes
:label (tr "dashboard.webhooks.content-type")
:default "application/json"
:name :mtype}]]]
@ -704,79 +729,75 @@
{:on-click #(st/emit! (modal/show :webhook {}))}
[:span (tr "dashboard.webhooks.create")]]]])
(mf/defc webhook-actions
[{:keys [on-edit on-delete] :as props}]
(let [show? (mf/use-state false)]
[:*
[:span.icon {:on-click #(reset! show? true)} [i/actions]]
[:& dropdown {:show @show?
:on-close #(reset! show? false)}
[:ul.dropdown.actions-dropdown
[:li {:on-click on-edit} (tr "labels.edit")]
[:li {:on-click on-delete} (tr "labels.delete")]]]]))
(mf/defc webhook-actions
[{:keys [on-edit on-delete] :as props}]
(let [show? (mf/use-state false)]
[:*
[:span.icon {:on-click #(reset! show? true)} [i/actions]]
[:& dropdown {:show @show?
:on-close #(reset! show? false)}
[:ul.dropdown.actions-dropdown
[:li {:on-click on-edit} (tr "labels.edit")]
[:li {:on-click on-delete} (tr "labels.delete")]]]]))
(mf/defc last-delivery-icon
[{:keys [success? text] :as props}]
[:div.last-delivery-icon
[:div.tooltip
[:div.label text]
[:div.arrow-down]]
(if success?
[:span.icon.success i/msg-success]
[:span.icon.failure i/msg-warning])])
(mf/defc last-delivery-icon
[{:keys [success? text] :as props}]
[:div.last-delivery-icon
[:div.tooltip
[:div.label text]
[:div.arrow-down]]
(if success?
[:span.icon.success i/msg-success]
[:span.icon.failure i/msg-warning])])
(mf/defc webhook-item
{::mf/wrap [mf/memo]}
[{:keys [webhook] :as props}]
(let [on-edit #(st/emit! (modal/show :webhook {:webhook webhook}))
error-code (:error-code webhook)
(mf/defc webhook-item
{::mf/wrap [mf/memo]}
[{:keys [webhook] :as props}]
(let [on-edit #(st/emit! (modal/show :webhook {:webhook webhook}))
error-code (:error-code webhook)
extract-status
(fn [error-code]
(let [status (-> error-code
(str/split "-")
last
parse-long)]
(if (nil? status)
""
status)))
delete-fn
(fn []
(let [params {:id (:id webhook)}
mdata {:on-success #(st/emit! (dd/fetch-team-webhooks))}]
(st/emit! (dd/delete-team-webhook (with-meta params mdata)))))
on-delete #(st/emit! (modal/show
{:type :confirm
:title (tr "modals.delete-webhook.title")
:message (tr "modals.delete-webhook.message")
:accept-label (tr "modals.delete-webhook.accept")
:on-accept delete-fn}))
last-delivery-text (cond
(nil? error-code)
(tr "webhooks.last-delivery.success")
delete-fn
(fn []
(let [params {:id (:id webhook)}
mdata {:on-success #(st/emit! (dd/fetch-team-webhooks))}]
(st/emit! (dd/delete-team-webhook (with-meta params mdata)))))
(= error-code "ssl-validation")
(str (tr "errors.webhooks.last-delivery") " " (tr "errors.webhooks.ssl-validation"))
on-delete
(fn []
(st/emit! (modal/show
{:type :confirm
:title (tr "modals.delete-webhook.title")
:message (tr "modals.delete-webhook.message")
:accept-label (tr "modals.delete-webhook.accept")
:on-accept delete-fn})))
(str/starts-with? error-code "unexpected-status")
(str (tr "errors.webhooks.last-delivery")
" "
(tr "errors.webhooks.unexpected-status" (extract-status error-code)))
last-delivery-text
(if (nil? error-code)
(tr "webhooks.last-delivery.success")
(str (tr "errors.webhooks.last-delivery")
(cond
(= error-code "ssl-validation-error")
(dm/str " " (tr "errors.webhooks.ssl-validation"))
:else
(tr "errors.webhooks.last-delivery"))]
[:div.table-row
[:div.table-field.last-delivery
[:div.icon-container
[:& last-delivery-icon {:success? (nil? error-code) :text last-delivery-text}]]]
[:div.table-field.uri
[:div (:uri webhook)]]
[:div.table-field.active
[:div (if (:is-active webhook) (tr "labels.active") (tr "labels.inactive"))]]
[:div.table-field.actions
[:& webhook-actions {:on-edit on-edit
:on-delete on-delete}]]]))
(str/starts-with? error-code "unexpected-status")
(dm/str " " (tr "errors.webhooks.unexpected-status" (extract-status error-code))))))]
[:div.table-row
[:div.table-field.last-delivery
[:div.icon-container
[:& last-delivery-icon
{:success? (nil? error-code)
:text last-delivery-text}]]]
[:div.table-field.uri
[:div (:uri webhook)]]
[:div.table-field.active
[:div (if (:is-active webhook)
(tr "labels.active")
(tr "labels.inactive"))]]
[:div.table-field.actions
[:& webhook-actions
{:on-edit on-edit
:on-delete on-delete}]]]))
(mf/defc webhooks-list
[{:keys [webhooks] :as props}]