feat: merge AccessModel fields of webhook data

This commit is contained in:
Fu Diwei
2025-08-27 20:57:29 +08:00
parent 6e2c719374
commit f74c68cc22
6 changed files with 97 additions and 90 deletions
+1 -1
View File
@@ -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,
+5 -6
View File
@@ -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 {
+1 -1
View File
@@ -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,
@@ -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
}
}
}
@@ -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);
}
};
@@ -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(/(?<!\r)\n/g, "\r\n");
formInst.setFieldValue("headers", value);
formInst.setFieldValue([parentNamePath, "headers"], value);
};
const handleWebhookDataForDeploymentBlur = () => {
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 <your-gotify-token>");
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://<your-gotify-server>/");
formInst.setFieldValue("method", "POST");
formInst.setFieldValue("headers", "Content-Type: application/json\r\nAuthorization: Bearer <your-gotify-token>");
formInst.setFieldValue([parentNamePath, "url"], "https://<your-gotify-server>/");
formInst.setFieldValue([parentNamePath, "method"], "POST");
formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json\r\nAuthorization: Bearer <your-gotify-token>");
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://<your-ntfy-server>/");
formInst.setFieldValue("method", "POST");
formInst.setFieldValue("headers", "Content-Type: application/json");
formInst.setFieldValue([parentNamePath, "url"], "https://<your-ntfy-server>/");
formInst.setFieldValue([parentNamePath, "method"], "POST");
formInst.setFieldValue([parentNamePath, "headers"], "Content-Type: application/json");
formInst.setFieldValue(
"dataForNotification",
[parentNamePath, "data"],
JSON.stringify(
{
topic: "<your-ntfy-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: "<your-pushover-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: "<your-pushplus-token>",
@@ -151,11 +153,11 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF
break;
case "serverchan3":
formInst.setFieldValue("url", "https://<your-serverchan-uid>.push.ft07.com/send/<your-serverchan-sendkey>.send");
formInst.setFieldValue("method", "POST");
formInst.setFieldValue("headers", "Content-Type: application/json");
formInst.setFieldValue([parentNamePath, "url"], "https://<your-serverchan-uid>.push.ft07.com/send/<your-serverchan-sendkey>.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/<your-serverchan-key>.send");
formInst.setFieldValue("method", "POST");
formInst.setFieldValue("headers", "Content-Type: application/json");
formInst.setFieldValue([parentNamePath, "url"], "https://sctapi.ftqq.com/<your-serverchan-key>.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
/>
</Form.Item>
<Show when={!usage || usage === "deployment"}>
<Show when={usage === "deployment"}>
<Form.Item className="relative" label={t("access.form.webhook_data.label")} extra={t("access.form.webhook_data.help")}>
<div className="absolute -top-[6px] right-0 -translate-y-full">
<Dropdown
@@ -242,7 +244,7 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF
</Button>
</Dropdown>
</div>
<Form.Item name={[parentNamePath, "dataForDeployment"]} initialValue={initialValues.dataForDeployment} noStyle rules={[formRule]}>
<Form.Item name={[parentNamePath, "data"]} initialValue={initialValues.data} noStyle rules={[formRule]}>
<CodeInput
height="auto"
minHeight="64px"
@@ -259,7 +261,7 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF
</Form.Item>
</Show>
<Show when={!usage || usage === "notification"}>
<Show when={usage === "notification"}>
<Form.Item className="relative" label={t("access.form.webhook_data.label")} extra={t("access.form.webhook_data.help")}>
<div className="absolute -top-[6px] right-0 -translate-y-full">
<Dropdown
@@ -278,7 +280,7 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF
</Button>
</Dropdown>
</div>
<Form.Item name={[parentNamePath, "dataForNotification"]} initialValue={initialValues.dataForNotification} noStyle rules={[formRule]}>
<Form.Item name={[parentNamePath, "data"]} initialValue={initialValues.data} noStyle rules={[formRule]}>
<CodeInput
height="auto"
minHeight="64px"
@@ -310,26 +312,25 @@ const AccessConfigFormFieldsProviderWebhook = ({ usage = "none" }: AccessConfigF
);
};
const getInitialValues = (): Nullish<z.infer<ReturnType<typeof getSchema>>> => {
const getInitialValues = ({ usage = "none" }: { usage?: "deployment" | "notification" | "none" }): Nullish<z.infer<ReturnType<typeof getSchema>>> => {
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<typeof getI18n> }) =
}
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) => {