1+ // Fetch the consolidated test results
2+ const testResults = JSON . parse ( pm . collectionVariables . get ( "testResults" ) || "[]" ) ;
3+
4+ if ( ! testResults . length ) {
5+ console . error ( "No test results found in testResults variable!" ) ;
6+ } else {
7+ console . log ( "Final testResults variable before posting to Jira:" , testResults ) ;
8+ }
9+
10+ // Remove duplicate test scenarios by using a Set to track unique names
11+ const uniqueTestResults = Array . from (
12+ new Map ( testResults . map ( result => [ result . name , result ] ) ) . values ( )
13+ ) ;
14+
15+ console . log ( "Unique testResults after deduplication:" , uniqueTestResults ) ;
16+
17+ // Function to create a heading in Jira doc format
18+ function createHeading ( text , level = 3 ) {
19+ return {
20+ type : "heading" ,
21+ attrs : { level } ,
22+ content : [
23+ {
24+ text,
25+ type : "text"
26+ }
27+ ]
28+ } ;
29+ }
30+
31+ // Function to create a paragraph in Jira doc format
32+ function createParagraph ( textArray ) {
33+ return {
34+ type : "paragraph" ,
35+ content : textArray . map ( text => (
36+ typeof text === "string"
37+ ? { text, type : "text" }
38+ : text // If it's already a formatted object (e.g., bold text), include it as is
39+ ) )
40+ } ;
41+ }
42+
43+ // Function to create a bold text block in Jira doc format
44+ function createBoldText ( text ) {
45+ return {
46+ text,
47+ type : "text" ,
48+ marks : [ { type : "strong" } ]
49+ } ;
50+ }
51+
52+ // Function to create a code block in Jira doc format
53+ function createCodeBlock ( text ) {
54+ return {
55+ type : "codeBlock" ,
56+ content : [
57+ {
58+ text,
59+ type : "text"
60+ }
61+ ]
62+ } ;
63+ }
64+
65+ // Construct the Jira comment body
66+ const jiraComment = {
67+ body : {
68+ type : "doc" ,
69+ version : 1 ,
70+ content : [
71+ createHeading ( "Consolidated Test Results" ) , // Main heading
72+ ...uniqueTestResults . flatMap ( result => [
73+ createHeading ( result . name , 3 ) , // Heading for each test scenario
74+ createParagraph ( [
75+ createBoldText ( "Request Method: " ) ,
76+ {
77+ text : `${ result . details . method } ` ,
78+ type : "text"
79+ }
80+ ] ) ,
81+ createParagraph ( [
82+ createBoldText ( "Request URI: " ) ,
83+ {
84+ text : `${ result . details . uri } ` ,
85+ type : "text"
86+ }
87+ ] ) ,
88+ createParagraph ( [
89+ createBoldText ( "Request Payload: " ) ,
90+ {
91+ text : JSON . stringify ( result . details . payload || { } , null , 2 ) ,
92+ type : "text"
93+ }
94+ ] ) ,
95+ createParagraph ( [
96+ createBoldText ( "Request Query Params: " ) ,
97+ {
98+ text : JSON . stringify ( result . details . queryParams || { } , null , 2 ) ,
99+ type : "text"
100+ }
101+ ] ) ,
102+ createParagraph ( [
103+ createBoldText ( "Response Status Code: " ) ,
104+ {
105+ text : `${ result . details . statusCode } ` ,
106+ type : "text"
107+ }
108+ ] ) ,
109+ createParagraph ( [
110+ createBoldText ( "Response Body:" )
111+ ] ) ,
112+ createCodeBlock ( JSON . stringify ( result . details . responseBody , null , 2 ) ) // JSON-formatted response body
113+ ] )
114+ ]
115+ }
116+ } ;
117+
118+ // Log the constructed payload for Jira
119+ console . log ( "Constructed Jira Comment Payload:" , JSON . stringify ( jiraComment , null , 2 ) ) ;
120+
121+ // Set the constructed comment as the request body
122+ pm . request . body . raw = JSON . stringify ( jiraComment ) ;
0 commit comments