1+ #! /bin/bash
2+
3+ # Get piped stdin as input
4+ INPUT=$( cat)
5+
6+ OUTPUT=" {}"
7+
8+ OUTPUT_MODE=$1 # summary, complaints, condensed, json
9+
10+ # If not set
11+ if [ -z " $OUTPUT_MODE " ]; then
12+ OUTPUT_MODE=" summary"
13+ fi
14+
15+ FILE_PATHS=$( echo $INPUT | jq -r ' .[].filePath' )
16+ ALL_MESSAGES=$( echo $INPUT | jq -r -c ' .[].messages' )
17+
18+ # convert to array
19+ IFS=$' \n ' FILE_PATHS=($FILE_PATHS )
20+ IFS=$' \n ' ALL_MESSAGES=($ALL_MESSAGES )
21+ # Iterate over file paths
22+ for index in " ${! FILE_PATHS[@]} " ; do
23+ file_path=${FILE_PATHS[$index]}
24+ # Get the messages for the file path
25+ messages=${ALL_MESSAGES[$index]}
26+
27+ if [ $OUTPUT_MODE == " complaints" ]; then
28+
29+ # Complaint: {filePath: "path", "start": [line, column], "end": [line, column], "message": "message", "severity": "severity", "ruleId": "ruleId"}
30+ messages=$( echo $messages | jq -r -c ' .[]' | tr ' \n' ' ' )
31+ IFS=$' \n ' messages=($messages )
32+ for message in " ${messages[@]} " ; do
33+ # If endLine is null, set it to line
34+ message_parsed=$( echo $message | jq -r -c ' if .endLine == null then .endLine = .line end' )
35+ # If endColumn is null, set it to convert column to number and add 1
36+ message_parsed=$( echo $message_parsed | jq -r -c ' if .endColumn == null then .endColumn = (.column | tonumber + 1) end' )
37+ # If severity is null, set it to "error"
38+ message_parsed=$( echo $message_parsed | jq -r -c ' if .severity == null then .severity = "error" end' )
39+ COMPLAINT=$( echo $message_parsed | jq -r -c --arg file_path $file_path ' {filePath: $file_path, start: [.line, .column], end: [.endLine, .endColumn], message: .message, severity: .severity, ruleId: .ruleId}' )
40+ echo $COMPLAINT
41+ done
42+ elif [ $OUTPUT_MODE == " condensed" ]; then
43+ # Remove duplicates
44+ messages=$( echo $messages | jq -r -c ' unique_by(.ruleId)' )
45+ messages=$( echo $messages | jq -r -c ' .[]' )
46+ IFS=$' \n ' messages=($messages )
47+ # Iterate over messages
48+ for message in " ${messages[@]} " ; do
49+ # Get the message id
50+ ID=$( echo $message | jq -r ' .ruleId' )
51+
52+ # If the OUTPUT[id] is null, set it to empty array
53+ if [[ $( echo $VIOLATIONS_BY_ID | jq -r " has(\" $ID \" )" ) == ' false' ]]; then
54+ OUTPUT=$( echo $OUTPUT | jq --arg id " $ID " ' .[$id] = []' )
55+ fi
56+ # Add the fileid to the violations object key
57+ OUTPUT=$( echo $OUTPUT | jq --arg id " $ID " --arg file_path " $file_path " ' .[$id] += [$file_path]' )
58+ done
59+ elif [ $OUTPUT_MODE == " json" ]; then
60+ OUTPUT=$( echo $INPUT )
61+ else
62+ # Summary
63+ AFFECTED_FILE_COUNT=$( echo $INPUT | jq -r ' length' )
64+ TOTAL_VIOLATION_COUNT=$( echo $INPUT | jq -r ' .[].messages | length' | jq -s add)
65+
66+ if [[ $TOTAL_VIOLATION_COUNT -gt 0 ]]; then
67+ OUTPUT=" Found $TOTAL_VIOLATION_COUNT violations in $AFFECTED_FILE_COUNT files."
68+ else
69+ OUTPUT=" No violations found."
70+ fi
71+ fi
72+ done
73+
74+ echo $OUTPUT
75+
76+ exit 0
0 commit comments