mirror of https://github.com/certd/certd
fix: 修复自定义webhook contextType的bug
parent
3254afc756
commit
7e5ea0cee0
|
@ -1,5 +1,5 @@
|
||||||
import { BaseNotification, IsNotification, NotificationBody, NotificationInput } from '@certd/pipeline';
|
import { BaseNotification, IsNotification, NotificationBody, NotificationInput } from '@certd/pipeline';
|
||||||
|
import qs from 'qs';
|
||||||
@IsNotification({
|
@IsNotification({
|
||||||
name: 'webhook',
|
name: 'webhook',
|
||||||
title: '自定义webhook',
|
title: '自定义webhook',
|
||||||
|
@ -9,7 +9,10 @@ export class WebhookNotification extends BaseNotification {
|
||||||
@NotificationInput({
|
@NotificationInput({
|
||||||
title: 'webhook地址',
|
title: 'webhook地址',
|
||||||
component: {
|
component: {
|
||||||
placeholder: '',
|
placeholder: 'https://xxxxx.com/xxxx',
|
||||||
|
},
|
||||||
|
col: {
|
||||||
|
span: 24,
|
||||||
},
|
},
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
|
@ -17,13 +20,14 @@ export class WebhookNotification extends BaseNotification {
|
||||||
|
|
||||||
@NotificationInput({
|
@NotificationInput({
|
||||||
title: '请求方式',
|
title: '请求方式',
|
||||||
value: 'post',
|
value: 'POST',
|
||||||
component: {
|
component: {
|
||||||
name: 'a-select',
|
name: 'a-select',
|
||||||
placeholder: 'post/put',
|
placeholder: 'post/put/get',
|
||||||
options: [
|
options: [
|
||||||
{ value: 'post', label: 'post' },
|
{ value: 'POST', label: 'POST' },
|
||||||
{ value: 'put', label: 'put' },
|
{ value: 'PUT', label: 'PUT' },
|
||||||
|
{ value: 'GET', label: 'GET' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -32,14 +36,15 @@ export class WebhookNotification extends BaseNotification {
|
||||||
|
|
||||||
@NotificationInput({
|
@NotificationInput({
|
||||||
title: 'ContentType',
|
title: 'ContentType',
|
||||||
value: 'json',
|
value: 'application/json',
|
||||||
component: {
|
component: {
|
||||||
name: 'a-select',
|
name: 'a-auto-complete',
|
||||||
options: [
|
options: [
|
||||||
{ value: 'application/json', label: 'json' },
|
{ value: 'application/json', label: 'application/json' },
|
||||||
{ value: 'application/x-www-form-urlencoded', label: 'x-www-form-urlencoded' },
|
{ value: 'application/x-www-form-urlencoded', label: 'application/x-www-form-urlencoded' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
helper: '也可以自定义填写',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
contentType = '';
|
contentType = '';
|
||||||
|
@ -49,9 +54,12 @@ export class WebhookNotification extends BaseNotification {
|
||||||
component: {
|
component: {
|
||||||
name: 'a-textarea',
|
name: 'a-textarea',
|
||||||
vModel: 'value',
|
vModel: 'value',
|
||||||
rows: 3,
|
rows: 2,
|
||||||
},
|
},
|
||||||
helper: '一行一个,格式为key:value',
|
col: {
|
||||||
|
span: 24,
|
||||||
|
},
|
||||||
|
helper: '一行一个,格式为key=value',
|
||||||
required: false,
|
required: false,
|
||||||
})
|
})
|
||||||
headers = '';
|
headers = '';
|
||||||
|
@ -69,34 +77,66 @@ export class WebhookNotification extends BaseNotification {
|
||||||
col: {
|
col: {
|
||||||
span: 24,
|
span: 24,
|
||||||
},
|
},
|
||||||
helper: `根据实际的webhook接口,构建一个json对象作为参数,支持变量:{title}、{content}、{url},变量用{}包裹,字符串需要双引号`,
|
helper: `根据实际的webhook接口,构建一个json对象作为参数,支持变量:{title}、{content}、{url},变量用{}包裹,字符串需要双引号\n如果是get方式,将作为query参数拼接到url上`,
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
template = '';
|
template = '';
|
||||||
|
|
||||||
|
replaceTemplate(target: string, body: any, urlEncode = false) {
|
||||||
|
let bodyStr = target;
|
||||||
|
const keys = Object.keys(body);
|
||||||
|
for (const key of keys) {
|
||||||
|
const value = urlEncode ? encodeURIComponent(body[key]) : body[key];
|
||||||
|
bodyStr = bodyStr.replaceAll(`{${key}}`, value);
|
||||||
|
}
|
||||||
|
return bodyStr;
|
||||||
|
}
|
||||||
|
|
||||||
async send(body: NotificationBody) {
|
async send(body: NotificationBody) {
|
||||||
if (!this.template) {
|
if (!this.template) {
|
||||||
throw new Error('模版不能为空');
|
throw new Error('模版不能为空');
|
||||||
}
|
}
|
||||||
|
if (!this.webhook) {
|
||||||
|
throw new Error('webhook不能为空');
|
||||||
|
}
|
||||||
|
|
||||||
const bodyStr = this.template.replaceAll('{title}', body.title).replaceAll('{content}', body.content).replaceAll('{url}', body.url);
|
const replaceBody = {
|
||||||
|
title: body.title,
|
||||||
|
content: body.content,
|
||||||
|
url: body.url,
|
||||||
|
};
|
||||||
|
const bodyStr = this.replaceTemplate(this.template, replaceBody);
|
||||||
|
let data = JSON.parse(bodyStr);
|
||||||
|
|
||||||
const data = JSON.parse(bodyStr);
|
let url = this.webhook;
|
||||||
|
if (this.method.toLowerCase() === 'get') {
|
||||||
|
const query = qs.stringify(data);
|
||||||
|
if (url.includes('?')) {
|
||||||
|
url = `${url}&${query}`;
|
||||||
|
} else {
|
||||||
|
url = `${url}?${query}`;
|
||||||
|
}
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
|
||||||
const headers: any = {};
|
const headers: any = {};
|
||||||
if (this.headers && this.headers.trim()) {
|
if (this.headers && this.headers.trim()) {
|
||||||
this.headers.split('\n').forEach(item => {
|
this.headers.split('\n').forEach(item => {
|
||||||
item = item.trim();
|
item = item.trim();
|
||||||
if (item) {
|
if (item) {
|
||||||
const [key, value] = item.split(':');
|
const arr = item.split('=');
|
||||||
headers[key] = value;
|
if (arr.length !== 2) {
|
||||||
|
this.logger.warn('header格式错误,请使用=号', item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
headers[arr[0]] = arr[1];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.http.request({
|
await this.http.request({
|
||||||
url: this.webhook,
|
url: url,
|
||||||
method: this.method,
|
method: this.method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': `${this.contentType}; charset=UTF-8`,
|
'Content-Type': `${this.contentType}; charset=UTF-8`,
|
||||||
|
|
Loading…
Reference in New Issue