diff --git a/internal/deployer/providers.go b/internal/deployer/providers.go index 92b42c783..65daf63ed 100644 --- a/internal/deployer/providers.go +++ b/internal/deployer/providers.go @@ -1524,7 +1524,7 @@ func createSSLDeployerProvider(options *deployerProviderOptions) (core.SSLDeploy deployer, err := pWebhook.NewSSLDeployerProvider(&pWebhook.SSLDeployerProviderConfig{ WebhookUrl: access.Url, - WebhookData: xmaps.GetOrDefaultString(options.ProviderServiceConfig, "webhookData", access.DataStringForDeployment), + WebhookData: xmaps.GetOrDefaultString(options.ProviderServiceConfig, "webhookData", access.DataString), Method: access.Method, Headers: mergedHeaders, AllowInsecureConnections: access.AllowInsecureConnections, diff --git a/internal/domain/access.go b/internal/domain/access.go index c40307850..63a93140c 100644 --- a/internal/domain/access.go +++ b/internal/domain/access.go @@ -402,12 +402,11 @@ type AccessConfigForWangsu struct { } type AccessConfigForWebhook struct { - Url string `json:"url"` - Method string `json:"method,omitempty"` - HeadersString string `json:"headers,omitempty"` - DataStringForDeployment string `json:"dataForDeployment,omitempty"` - DataStringForNotification string `json:"dataForNotification,omitempty"` - AllowInsecureConnections bool `json:"allowInsecureConnections,omitempty"` + Url string `json:"url"` + Method string `json:"method,omitempty"` + HeadersString string `json:"headers,omitempty"` + DataString string `json:"data,omitempty"` + AllowInsecureConnections bool `json:"allowInsecureConnections,omitempty"` } type AccessConfigForWeComBot struct { diff --git a/internal/notify/providers.go b/internal/notify/providers.go index 2b54e45e0..58ef7a91f 100644 --- a/internal/notify/providers.go +++ b/internal/notify/providers.go @@ -158,7 +158,7 @@ func createNotifierProvider(options *notifierProviderOptions) (core.Notifier, er return pWebhook.NewNotifierProvider(&pWebhook.NotifierProviderConfig{ WebhookUrl: access.Url, - WebhookData: xmaps.GetOrDefaultString(options.ProviderServiceConfig, "webhookData", access.DataStringForNotification), + WebhookData: xmaps.GetOrDefaultString(options.ProviderServiceConfig, "webhookData", access.DataString), Method: access.Method, Headers: mergedHeaders, AllowInsecureConnections: access.AllowInsecureConnections, diff --git a/migrations/1755187200_cm0.4.0_migrate.go b/migrations/1756296000_cm0.4.0_migrate.go similarity index 96% rename from migrations/1755187200_cm0.4.0_migrate.go rename to migrations/1756296000_cm0.4.0_migrate.go index 9d85755a8..5d9919199 100644 --- a/migrations/1755187200_cm0.4.0_migrate.go +++ b/migrations/1756296000_cm0.4.0_migrate.go @@ -105,16 +105,36 @@ func init() { case "webhook": if _, ok := config["defaultDataForDeployment"]; ok { - config["dataForDeployment"] = config["defaultDataForDeployment"] - delete(config, "defaultDataForDeployment") - record.Set("config", config) - changed = true + if existsData, exists := config["data"]; !exists || existsData == "" { + config["data"] = config["defaultDataForDeployment"] + delete(config, "defaultDataForDeployment") + record.Set("config", config) + changed = true + } } if _, ok := config["defaultDataForNotification"]; ok { - config["dataForNotification"] = config["defaultDataForNotification"] - delete(config, "defaultDataForNotification") - record.Set("config", config) - changed = true + if existsData, exists := config["data"]; !exists || existsData == "" { + config["data"] = config["defaultDataForNotification"] + delete(config, "defaultDataForNotification") + record.Set("config", config) + changed = true + } + } + if _, ok := config["dataForDeployment"]; ok { + if existsData, exists := config["data"]; !exists || existsData == "" { + config["data"] = config["dataForDeployment"] + delete(config, "dataForDeployment") + record.Set("config", config) + changed = true + } + } + if _, ok := config["dataForNotification"]; ok { + if existsData, exists := config["data"]; !exists || existsData == "" { + config["data"] = config["dataForNotification"] + delete(config, "dataForNotification") + record.Set("config", config) + changed = true + } } } diff --git a/ui/src/components/access/forms/AccessConfigFieldsProviderEmail.tsx b/ui/src/components/access/forms/AccessConfigFieldsProviderEmail.tsx index 378f0d3c5..559497399 100644 --- a/ui/src/components/access/forms/AccessConfigFieldsProviderEmail.tsx +++ b/ui/src/components/access/forms/AccessConfigFieldsProviderEmail.tsx @@ -19,10 +19,10 @@ const AccessConfigFormFieldsProviderEmail = () => { const initialValues = getInitialValues(); const handleTlsSwitchChange = (checked: boolean) => { - const oldPort = formInst.getFieldValue("smtpPort"); + const oldPort = formInst.getFieldValue([parentNamePath, "smtpPort"]); const newPort = checked && (oldPort == null || oldPort === 25) ? 465 : !checked && (oldPort == null || oldPort === 465) ? 25 : oldPort; if (newPort !== oldPort) { - formInst.setFieldValue("smtpPort", newPort); + formInst.setFieldValue([parentNamePath, "smtpPort"], newPort); } }; diff --git a/ui/src/components/access/forms/AccessConfigFieldsProviderWebhook.tsx b/ui/src/components/access/forms/AccessConfigFieldsProviderWebhook.tsx index 10bda9eee..4079dc724 100644 --- a/ui/src/components/access/forms/AccessConfigFieldsProviderWebhook.tsx +++ b/ui/src/components/access/forms/AccessConfigFieldsProviderWebhook.tsx @@ -23,47 +23,49 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF }); const formRule = createSchemaFieldRule(formSchema); const formInst = Form.useFormInstance(); - const initialValues = getInitialValues(); + const initialValues = getInitialValues({ usage }); const handleWebhookHeadersBlur = () => { - let value = formInst.getFieldValue("headers"); + let value = formInst.getFieldValue([parentNamePath, "headers"]); value = value.trim(); value = value.replace(/(? { - const value = formInst.getFieldValue("dataForDeployment"); + const value = formInst.getFieldValue([parentNamePath, "data"]); try { const json = JSON.stringify(JSON.parse(value), null, 2); - formInst.setFieldValue("dataForDeployment", json); + formInst.setFieldValue([parentNamePath, "data"], json); } catch { return; } }; const handleWebhookDataForNotificationBlur = () => { - const value = formInst.getFieldValue("dataForNotification"); + const value = formInst.getFieldValue([parentNamePath, "data"]); try { const json = JSON.stringify(JSON.parse(value), null, 2); - formInst.setFieldValue("dataForNotification", json); + formInst.setFieldValue([parentNamePath, "data"], json); } catch { return; } }; const handlePresetDataForDeploymentClick = () => { - formInst.setFieldValue("dataForDeployment", getInitialValues().dataForDeployment); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "data"], getInitialValues({ usage: "deployment" }).data); }; const handlePresetDataForNotificationClick = (key: string) => { switch (key) { case "bark": - formInst.setFieldValue("url", "https://api.day.app/push"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json\r\nAuthorization: Bearer "); + formInst.setFieldValue([parentNamePath, "url"], "https://api.day.app/push"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { title: "${SUBJECT}", @@ -77,11 +79,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "gotify": - formInst.setFieldValue("url", "https:///"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json\r\nAuthorization: Bearer "); + formInst.setFieldValue([parentNamePath, "url"], "https:///"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json\r\nAuthorization: Bearer "); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { title: "${SUBJECT}", @@ -95,11 +97,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "ntfy": - formInst.setFieldValue("url", "https:///"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "url"], "https:///"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { topic: "", @@ -114,11 +116,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "pushover": - formInst.setFieldValue("url", "https://api.pushover.net/1/messages.json"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "url"], "https://api.pushover.net/1/messages.json"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { token: "", @@ -133,11 +135,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "pushplus": - formInst.setFieldValue("url", "https://www.pushplus.plus/send"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "url"], "https://www.pushplus.plus/send"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { token: "", @@ -151,11 +153,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "serverchan3": - formInst.setFieldValue("url", "https://.push.ft07.com/send/.send"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "url"], "https://.push.ft07.com/send/.send"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { title: "${SUBJECT}", @@ -168,11 +170,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; case "serverchanturbo": - formInst.setFieldValue("url", "https://sctapi.ftqq.com/.send"); - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "url"], "https://sctapi.ftqq.com/.send"); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); formInst.setFieldValue( - "dataForNotification", + [parentNamePath, "data"], JSON.stringify( { text: "${SUBJECT}", @@ -185,9 +187,9 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF break; default: - formInst.setFieldValue("method", "POST"); - formInst.setFieldValue("headers", "Content-Type: application/json"); - formInst.setFieldValue("dataForNotification", getInitialValues().dataForNotification); + formInst.setFieldValue([parentNamePath, "method"], "POST"); + formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json"); + formInst.setFieldValue([parentNamePath, "data"], getInitialValues({ usage: "notification" }).data); break; } }; @@ -221,7 +223,7 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF /> - +
- +
- +
- + >> => { +const getInitialValues = ({ usage = "none" }: { usage?: "deployment" | "notification" | "none" }): Nullish>> => { return { url: "", method: "POST", headers: "Content-Type: application/json", allowInsecureConnections: false, - dataForDeployment: JSON.stringify( - { - name: "${DOMAINS}", - cert: "${CERTIFICATE}", - privkey: "${PRIVATE_KEY}", - }, - null, - 2 - ), - dataForNotification: JSON.stringify( - { - subject: "${SUBJECT}", - message: "${MESSAGE}", - }, + data: JSON.stringify( + usage === "deployment" + ? { + name: "${DOMAINS}", + cert: "${CERTIFICATE}", + privkey: "${PRIVATE_KEY}", + } + : usage === "notification" + ? { + subject: "${SUBJECT}", + message: "${MESSAGE}", + } + : {}, null, 2 ), @@ -356,20 +357,7 @@ const getSchema = ({ i18n = getI18n() }: { i18n: ReturnType }) = } return true; }, t("access.form.webhook_headers.errmsg.invalid")), - dataForDeployment: z - .string() - .nullish() - .refine((v) => { - if (!v) return true; - - try { - const obj = JSON.parse(v); - return typeof obj === "object" && !Array.isArray(obj); - } catch { - return false; - } - }, t("access.form.webhook_data.errmsg.json_invalid")), - dataForNotification: z + data: z .string() .nullish() .refine((v) => {