@@ -2,16 +2,37 @@ import { createLogger } from '@xen-orchestra/log'
2
2
3
3
const log = createLogger ( 'xo:web-hooks' )
4
4
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
+
5
25
function handleHook ( type , data ) {
6
26
const hooks = this . _hooks [ data . method ] ?. [ type ]
7
27
if ( hooks !== undefined ) {
8
28
return Promise . all (
9
29
// 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 => {
12
33
log . error ( 'web hook failed' , {
13
34
error,
14
- webHook : { ...data , url, type } ,
35
+ webHook : { ...data , url, type, isOfficeHook } ,
15
36
} )
16
37
} )
17
38
if ( waitForResponse && type === 'pre' ) {
@@ -33,9 +54,9 @@ class XoServerHooks {
33
54
this . _handlePostHook = handleHook . bind ( this , 'post' )
34
55
}
35
56
36
- _makeRequest ( url , type , data ) {
57
+ _makeRequest ( url , data ) {
37
58
return this . _xo . httpRequest ( url , {
38
- body : JSON . stringify ( { ... data , type } ) ,
59
+ body : JSON . stringify ( data ) ,
39
60
headers : { 'Content-Type' : 'application/json' } ,
40
61
method : 'POST' ,
41
62
timeout : 1e4 ,
@@ -89,24 +110,35 @@ class XoServerHooks {
89
110
this . _xo . removeListener ( 'backup:postCall' , this . _handlePostHook )
90
111
}
91
112
92
- async test ( { url } ) {
93
- await this . _makeRequest ( url , 'pre' , {
94
- callId : '0' ,
95
- userId : 'b4tm4n' ,
96
-
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
-
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
+
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
+
134
+ method : 'vm.start' ,
135
+ result : '' ,
136
+ timestamp : 500 ,
137
+ duration : 500 ,
138
+ } ,
139
+ 'post'
140
+ )
141
+ await this . _makeRequest ( url , postData )
110
142
}
111
143
}
112
144
@@ -121,6 +153,11 @@ export const configurationSchema = ({ xo: { apiMethods } }) => ({
121
153
type : 'object' ,
122
154
title : 'Hook' ,
123
155
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
+ } ,
124
161
method : {
125
162
description : 'The method to be bound to' ,
126
163
enum : Object . keys ( apiMethods ) . sort ( ) ,
@@ -158,6 +195,11 @@ export const testSchema = {
158
195
type : 'object' ,
159
196
description : 'The test will simulate a hook on `vm.start` (both "pre" and "post" hooks)' ,
160
197
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
+ } ,
161
203
url : {
162
204
title : 'URL' ,
163
205
type : 'string' ,
0 commit comments