|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Define the input files and output json file |
| 4 | +INPUT_FILE="test.json" |
| 5 | +TIMES_FILE="test_times.json" |
| 6 | +OUTPUT_FILE="slack_message.json" |
| 7 | + |
| 8 | +# Initialize variables |
| 9 | +PASSED_TESTS=() |
| 10 | +FAILED_TESTS=() |
| 11 | +TEST_NAMES=() |
| 12 | +TEST_TIMES=() |
| 13 | + |
| 14 | +# Clear the output file if it already exists |
| 15 | +> "$OUTPUT_FILE" |
| 16 | + |
| 17 | +# Function to create the header block for the Slack message |
| 18 | +create_header_block() { |
| 19 | + cat <<EOF >> "$OUTPUT_FILE" |
| 20 | +{ |
| 21 | + "blocks": [ |
| 22 | + { |
| 23 | + "type": "header", |
| 24 | + "text": { |
| 25 | + "type": "plain_text", |
| 26 | + "text": "3L Monitoring Test Report :test_tube:", |
| 27 | + "emoji": true |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + "type": "divider" |
| 32 | + }, |
| 33 | +EOF |
| 34 | +} |
| 35 | + |
| 36 | +# Function to create the summary block for the Slack message |
| 37 | +create_summary_block() { |
| 38 | + total_passed=$1 |
| 39 | + total_failed=$2 |
| 40 | + |
| 41 | + cat <<EOF >> "$OUTPUT_FILE" |
| 42 | + { |
| 43 | + "type": "section", |
| 44 | + "fields": [ |
| 45 | + { |
| 46 | + "type": "mrkdwn", |
| 47 | + "text": "*Total Tests Passed:*\n$total_passed" |
| 48 | + }, |
| 49 | + { |
| 50 | + "type": "mrkdwn", |
| 51 | + "text": "*Total Tests Failed:*\n$total_failed" |
| 52 | + } |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "type": "divider" |
| 57 | + }, |
| 58 | +EOF |
| 59 | +} |
| 60 | + |
| 61 | +# Function to create the passed tests block |
| 62 | +create_passed_tests_block() { |
| 63 | + if [ ${#PASSED_TESTS[@]} -gt 0 ]; then |
| 64 | + echo " {" >> "$OUTPUT_FILE" |
| 65 | + echo ' "type": "section",' >> "$OUTPUT_FILE" |
| 66 | + echo ' "text": {' >> "$OUTPUT_FILE" |
| 67 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 68 | + echo ' "text": "*Passed Tests:*"' >> "$OUTPUT_FILE" |
| 69 | + echo " }," >> "$OUTPUT_FILE" |
| 70 | + echo ' "fields": [' >> "$OUTPUT_FILE" |
| 71 | + for test in "${PASSED_TESTS[@]}"; do |
| 72 | + echo ' {' >> "$OUTPUT_FILE" |
| 73 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 74 | + echo ' "text": "`'$test'`"' >> "$OUTPUT_FILE" |
| 75 | + echo ' },' >> "$OUTPUT_FILE" |
| 76 | + done |
| 77 | + echo ' ]' >> "$OUTPUT_FILE" |
| 78 | + echo ' },' >> "$OUTPUT_FILE" |
| 79 | + echo ' {' >> "$OUTPUT_FILE" |
| 80 | + echo ' "type": "divider"' >> "$OUTPUT_FILE" |
| 81 | + echo ' },' >> "$OUTPUT_FILE" |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +# Function to create the failed tests block |
| 86 | +create_failed_tests_block() { |
| 87 | + if [ ${#FAILED_TESTS[@]} -gt 0 ]; then |
| 88 | + echo " {" >> "$OUTPUT_FILE" |
| 89 | + echo ' "type": "section",' >> "$OUTPUT_FILE" |
| 90 | + echo ' "text": {' >> "$OUTPUT_FILE" |
| 91 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 92 | + echo ' "text": "*Failed Tests:*"' >> "$OUTPUT_FILE" |
| 93 | + echo " }," >> "$OUTPUT_FILE" |
| 94 | + echo ' "fields": [' >> "$OUTPUT_FILE" |
| 95 | + |
| 96 | + for test in "${FAILED_TESTS[@]}"; do |
| 97 | + test_name=$(echo "$test" | cut -d "|" -f 1) |
| 98 | + |
| 99 | + # Add the test name as its own field |
| 100 | + echo ' {' >> "$OUTPUT_FILE" |
| 101 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 102 | + echo ' "text": "`'$test_name'`"' >> "$OUTPUT_FILE" |
| 103 | + echo ' },' >> "$OUTPUT_FILE" |
| 104 | + done |
| 105 | + |
| 106 | + echo ' ]' >> "$OUTPUT_FILE" |
| 107 | + echo ' },' >> "$OUTPUT_FILE" |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +# Function to create the test execution times block |
| 112 | +create_test_times_block() { |
| 113 | + if [ ${#TEST_NAMES[@]} -gt 0 ]; then |
| 114 | + echo " {" >> "$OUTPUT_FILE" |
| 115 | + echo ' "type": "section",' >> "$OUTPUT_FILE" |
| 116 | + echo ' "text": {' >> "$OUTPUT_FILE" |
| 117 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 118 | + echo ' "text": "*Execution Times:*"' >> "$OUTPUT_FILE" |
| 119 | + echo " }," >> "$OUTPUT_FILE" |
| 120 | + echo ' "fields": [' >> "$OUTPUT_FILE" |
| 121 | + for i in "${!TEST_NAMES[@]}"; do |
| 122 | + echo ' {' >> "$OUTPUT_FILE" |
| 123 | + echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE" |
| 124 | + echo ' "text": "`'${TEST_NAMES[$i]}'`: '${TEST_TIMES[$i]}' seconds"' >> "$OUTPUT_FILE" |
| 125 | + echo ' },' >> "$OUTPUT_FILE" |
| 126 | + done |
| 127 | + echo ' ]' >> "$OUTPUT_FILE" |
| 128 | + echo ' },' >> "$OUTPUT_FILE" |
| 129 | + echo ' {' >> "$OUTPUT_FILE" |
| 130 | + echo ' "type": "divider"' >> "$OUTPUT_FILE" |
| 131 | + echo ' },' >> "$OUTPUT_FILE" |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +# Function to end the JSON message block |
| 136 | +end_json_block() { |
| 137 | + echo " ]" >> "$OUTPUT_FILE" |
| 138 | + echo "}" >> "$OUTPUT_FILE" |
| 139 | +} |
| 140 | + |
| 141 | +# Parse the test times JSON file and store in parallel arrays |
| 142 | +while IFS= read -r line; do |
| 143 | + test_name=$(echo "$line" | jq -r '.test') |
| 144 | + time_seconds=$(echo "$line" | jq -r '.time_seconds') |
| 145 | + |
| 146 | + # Store test names and execution times in parallel arrays |
| 147 | + TEST_NAMES+=("$test_name") |
| 148 | + TEST_TIMES+=("$time_seconds") |
| 149 | +done < "$TIMES_FILE" |
| 150 | + |
| 151 | +# Parse the test result JSON line by line |
| 152 | +while IFS= read -r line; do |
| 153 | + # Parse the type and event fields from the JSON line |
| 154 | + type=$(echo "$line" | jq -r '.type') |
| 155 | + event=$(echo "$line" | jq -r '.event') |
| 156 | + |
| 157 | + if [[ "$type" == "test" ]]; then |
| 158 | + test_name=$(echo "$line" | jq -r '.name') |
| 159 | + |
| 160 | + if [[ "$event" == "ok" ]]; then |
| 161 | + PASSED_TESTS+=("$test_name") |
| 162 | + elif [[ "$event" == "failed" ]]; then |
| 163 | + FAILED_TESTS+=("$test_name") |
| 164 | + fi |
| 165 | + elif [[ "$type" == "suite" && ("$event" == "ok" || "$event" == "failed")]]; then |
| 166 | + total_passed=$(echo "$line" | jq -r '.passed') |
| 167 | + total_failed=$(echo "$line" | jq -r '.failed') |
| 168 | + |
| 169 | + # Create header and summary in the Slack message |
| 170 | + create_header_block |
| 171 | + create_summary_block "$total_passed" "$total_failed" |
| 172 | + fi |
| 173 | +done < "$INPUT_FILE" |
| 174 | + |
| 175 | +# Write the Slack message |
| 176 | +create_passed_tests_block |
| 177 | +create_failed_tests_block |
| 178 | +create_test_times_block |
| 179 | +end_json_block |
| 180 | + |
| 181 | +echo "Slack Block Kit JSON message generated in $OUTPUT_FILE" |
0 commit comments