@@ -2,20 +2,30 @@ const request = require('phin-retry');
2
2
const { getTitleText, getResultText, getPercentage, truncate, getPrettyDuration } = require ( '../helpers/helper' ) ;
3
3
const extension_manager = require ( '../extensions' ) ;
4
4
const { HOOK } = require ( '../helpers/constants' ) ;
5
+ const PerformanceTestResult = require ( 'performance-results-parser/src/models/PerformanceTestResult' ) ;
6
+ const { getValidMetrics, getMetricValuesText } = require ( '../helpers/performance' ) ;
5
7
6
8
async function run ( { result, target } ) {
7
9
setTargetInputs ( target ) ;
8
10
const root_payload = getRootPayload ( ) ;
9
11
const payload = root_payload . cards [ 0 ] ;
12
+ if ( result instanceof PerformanceTestResult ) {
13
+ await setPerformancePayload ( { result, target, payload, root_payload } ) ;
14
+ } else {
15
+ await setFunctionalPayload ( { result, target, payload, root_payload } ) ;
16
+ }
17
+ return request . post ( {
18
+ url : target . inputs . url ,
19
+ body : root_payload
20
+ } ) ;
21
+ }
22
+
23
+ async function setFunctionalPayload ( { result, target, payload, root_payload } ) {
10
24
await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . START } ) ;
11
25
setMainBlock ( { result, target, payload } ) ;
12
26
await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . POST_MAIN } ) ;
13
27
setSuiteBlock ( { result, target, payload } ) ;
14
28
await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . END } ) ;
15
- return request . post ( {
16
- url : target . inputs . url ,
17
- body : root_payload
18
- } ) ;
19
29
}
20
30
21
31
function setTargetInputs ( target ) {
@@ -39,22 +49,10 @@ function getRootPayload() {
39
49
}
40
50
41
51
function setMainBlock ( { result, target, payload } ) {
42
- const emoji = result . status === 'PASS' ? '✅' : '❌' ;
43
- const title_text = getTitleText ( { result, target } ) ;
52
+ const title_text_with_emoji = getTitleTextWithEmoji ( { result, target } ) ;
44
53
const result_text = getResultText ( { result } ) ;
45
54
const duration_text = getPrettyDuration ( result . duration , target . inputs . duration ) ;
46
55
47
- let title_text_with_emoji = '' ;
48
- if ( target . inputs . include_suites === false ) {
49
- title_text_with_emoji = `${ emoji } ${ title_text } ` ;
50
- } else if ( result . suites . length > 1 ) {
51
- title_text_with_emoji = title_text ;
52
- } else {
53
- title_text_with_emoji = `${ emoji } ${ title_text } ` ;
54
- }
55
- if ( target . inputs . title_link ) {
56
- title_text_with_emoji = `<a href="${ target . inputs . title_link } ">${ title_text_with_emoji } </a>` ;
57
- }
58
56
const text = `<b>${ title_text_with_emoji } </b><br><br><b>Results</b>: ${ result_text } <br><b>Duration</b>: ${ duration_text } ` ;
59
57
payload . sections . push ( {
60
58
"widgets" : [
@@ -118,6 +116,102 @@ function getFailureDetails(suite) {
118
116
return text ;
119
117
}
120
118
119
+ function getTitleTextWithEmoji ( { result, target } ) {
120
+ const emoji = result . status === 'PASS' ? '✅' : '❌' ;
121
+ const title_text = getTitleText ( { result, target } ) ;
122
+
123
+ let title_text_with_emoji = '' ;
124
+ if ( target . inputs . include_suites === false ) {
125
+ title_text_with_emoji = `${ emoji } ${ title_text } ` ;
126
+ } else if ( result . suites && result . suites . length > 1 || result . transactions && result . transactions . length > 1 ) {
127
+ title_text_with_emoji = title_text ;
128
+ } else {
129
+ title_text_with_emoji = `${ emoji } ${ title_text } ` ;
130
+ }
131
+ if ( target . inputs . title_link ) {
132
+ title_text_with_emoji = `<a href="${ target . inputs . title_link } ">${ title_text_with_emoji } </a>` ;
133
+ }
134
+ return title_text_with_emoji ;
135
+ }
136
+
137
+ async function setPerformancePayload ( { result, target, payload, root_payload } ) {
138
+ await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . START } ) ;
139
+ await setPerformanceMainBlock ( { result, target, payload } ) ;
140
+ await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . POST_MAIN } ) ;
141
+ await setTransactionBlock ( { result, target, payload } ) ;
142
+ await extension_manager . run ( { result, target, payload, root_payload, hook : HOOK . END } ) ;
143
+ }
144
+
145
+ /**
146
+ *
147
+ * @param {object } param0
148
+ * @param {PerformanceTestResult } param0.result
149
+ */
150
+ async function setPerformanceMainBlock ( { result, target, payload } ) {
151
+ const title_text_with_emoji = getTitleTextWithEmoji ( { result, target } ) ;
152
+ const result_text = getResultText ( { result } ) ;
153
+ let text = `<b>${ title_text_with_emoji } </b><br><br><b>Results</b>: ${ result_text } <br>` ;
154
+ const valid_metrics = await getValidMetrics ( { metrics : result . metrics , target, result } ) ;
155
+ for ( let i = 0 ; i < valid_metrics . length ; i ++ ) {
156
+ const metric = valid_metrics [ i ] ;
157
+ text += `<br><b>${ metric . name } </b>: ${ getMetricValuesText ( { metric, target, result } ) } ` ;
158
+ }
159
+ payload . sections . push ( {
160
+ "widgets" : [
161
+ {
162
+ "textParagraph" : {
163
+ text
164
+ }
165
+ }
166
+ ]
167
+ } ) ;
168
+ }
169
+
170
+ /**
171
+ *
172
+ * @param {object } param0
173
+ * @param {PerformanceTestResult } param0.result
174
+ */
175
+ async function setTransactionBlock ( { result, target, payload } ) {
176
+ if ( target . inputs . include_suites ) {
177
+ let texts = [ ] ;
178
+ for ( let i = 0 ; i < result . transactions . length ; i ++ ) {
179
+ const transaction = result . transactions [ i ] ;
180
+ if ( target . inputs . only_failures && transaction . status !== 'FAIL' ) {
181
+ continue ;
182
+ }
183
+ // if transactions length eq to 1 then main block will include suite summary
184
+ if ( result . transactions . length > 1 ) {
185
+ texts . push ( await getTransactionSummary ( { target, transaction, } ) ) ;
186
+ }
187
+ }
188
+ if ( texts . length > 0 ) {
189
+ payload . sections . push ( {
190
+ "widgets" : [
191
+ {
192
+ "textParagraph" : {
193
+ "text" : texts . join ( "<br><br>" )
194
+ }
195
+ }
196
+ ]
197
+ } ) ;
198
+ }
199
+ }
200
+ }
201
+
202
+ async function getTransactionSummary ( { target, transaction } ) {
203
+ const emoji = transaction . status === 'PASS' ? '✅' : '❌' ;
204
+ const suite_title = `${ emoji } ${ transaction . name } ` ;
205
+ let text = `<b>${ suite_title } </b><br>` ;
206
+ const valid_metrics = await getValidMetrics ( { metrics : transaction . metrics , target, result : transaction } ) ;
207
+ for ( let i = 0 ; i < valid_metrics . length ; i ++ ) {
208
+ const metric = valid_metrics [ i ] ;
209
+ text += `<br><b>${ metric . name } </b>: ${ getMetricValuesText ( { metric, target } ) } ` ;
210
+ }
211
+ return text ;
212
+ }
213
+
214
+
121
215
const default_options = {
122
216
condition : 'passOrFail'
123
217
} ;
0 commit comments