Skip to content

Commit 8eef8df

Browse files
committed
feat(xo-server-web-hooks): handle office 365 connector format
1 parent dba08f0 commit 8eef8df

File tree

1 file changed

+65
-23
lines changed
  • packages/xo-server-web-hooks/src

1 file changed

+65
-23
lines changed

Diff for: packages/xo-server-web-hooks/src/index.js

+65-23
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@ import { createLogger } from '@xen-orchestra/log'
22

33
const log = createLogger('xo:web-hooks')
44

5+
function constructData(isOfficeHook, data, type) {
6+
if (isOfficeHook) {
7+
// https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
8+
const facts = Object.keys(data).map(key => {
9+
const value = data[key]
10+
return { name: key, value: typeof value === 'string' ? value : JSON.stringify(value) }
11+
})
12+
13+
return {
14+
'@type': 'MessageCard',
15+
'@context': 'https://schema.org/extensions',
16+
themeColor: 'BE1621',
17+
summary: 'New notification from the Xen-Orchestra webhook plugin',
18+
sections: [{ title: 'XO notification' }, { facts }],
19+
}
20+
}
21+
22+
return { ...data, type }
23+
}
24+
525
function handleHook(type, data) {
626
const hooks = this._hooks[data.method]?.[type]
727
if (hooks !== undefined) {
828
return Promise.all(
929
// eslint-disable-next-line array-callback-return
10-
hooks.map(({ url, waitForResponse = false }) => {
11-
const promise = this._makeRequest(url, type, data).catch(error => {
30+
hooks.map(({ url, waitForResponse = false, isOfficeHook = false }) => {
31+
const _data = constructData(isOfficeHook, data, type)
32+
const promise = this._makeRequest(url, _data).catch(error => {
1233
log.error('web hook failed', {
1334
error,
14-
webHook: { ...data, url, type },
35+
webHook: { ...data, url, type, isOfficeHook },
1536
})
1637
})
1738
if (waitForResponse && type === 'pre') {
@@ -33,9 +54,9 @@ class XoServerHooks {
3354
this._handlePostHook = handleHook.bind(this, 'post')
3455
}
3556

36-
_makeRequest(url, type, data) {
57+
_makeRequest(url, data) {
3758
return this._xo.httpRequest(url, {
38-
body: JSON.stringify({ ...data, type }),
59+
body: JSON.stringify(data),
3960
headers: { 'Content-Type': 'application/json' },
4061
method: 'POST',
4162
timeout: 1e4,
@@ -89,24 +110,35 @@ class XoServerHooks {
89110
this._xo.removeListener('backup:postCall', this._handlePostHook)
90111
}
91112

92-
async test({ url }) {
93-
await this._makeRequest(url, 'pre', {
94-
callId: '0',
95-
userId: 'b4tm4n',
96-
userName: '[email protected]',
97-
method: 'vm.start',
98-
params: { id: '67aac198-0174-11ea-8d71-362b9e155667' },
99-
timestamp: 0,
100-
})
101-
await this._makeRequest(url, 'post', {
102-
callId: '0',
103-
userId: 'b4tm4n',
104-
userName: '[email protected]',
105-
method: 'vm.start',
106-
result: '',
107-
timestamp: 500,
108-
duration: 500,
109-
})
113+
async test({ url, isOfficeHook }) {
114+
const preData = constructData(
115+
isOfficeHook,
116+
{
117+
callId: '0',
118+
userId: 'b4tm4n',
119+
userName: '[email protected]',
120+
method: 'vm.start',
121+
params: { id: '67aac198-0174-11ea-8d71-362b9e155667' },
122+
timestamp: 0,
123+
},
124+
'pre'
125+
)
126+
await this._makeRequest(url, preData)
127+
128+
const postData = constructData(
129+
isOfficeHook,
130+
{
131+
callId: '0',
132+
userId: 'b4tm4n',
133+
userName: '[email protected]',
134+
method: 'vm.start',
135+
result: '',
136+
timestamp: 500,
137+
duration: 500,
138+
},
139+
'post'
140+
)
141+
await this._makeRequest(url, postData)
110142
}
111143
}
112144

@@ -121,6 +153,11 @@ export const configurationSchema = ({ xo: { apiMethods } }) => ({
121153
type: 'object',
122154
title: 'Hook',
123155
properties: {
156+
isOfficeHook: {
157+
description: 'Enable if your URL require a payload that follow the office 365 connector format',
158+
title: 'Office 365 connector format',
159+
type: 'boolean',
160+
},
124161
method: {
125162
description: 'The method to be bound to',
126163
enum: Object.keys(apiMethods).sort(),
@@ -158,6 +195,11 @@ export const testSchema = {
158195
type: 'object',
159196
description: 'The test will simulate a hook on `vm.start` (both "pre" and "post" hooks)',
160197
properties: {
198+
isOfficeHook: {
199+
description: 'Enable if your URL require a payload that follow the office 365 connector format',
200+
title: 'Office 365 connector format',
201+
type: 'boolean',
202+
},
161203
url: {
162204
title: 'URL',
163205
type: 'string',

0 commit comments

Comments
 (0)