11#! /bin/sh
22set -e
33
4- # ========== ARQUIVOS TEMPORÁRIOS ==========
54CODENARC_RESULT=" result.txt"
65LINE_VIOLATIONS=" line_violations.txt"
76FILE_VIOLATIONS=" file_violations.txt"
@@ -18,32 +17,25 @@ cleanup_temp_files() {
1817}
1918trap ' cleanup_temp_files' EXIT
2019
21- # ========== ETAPA 1 - EXECUTA CODENARC ==========
2220run_codenarc () {
2321 report=" ${INPUT_REPORT:- compact: stdout} "
2422 includes_arg=" "
25-
2623 [ -n " $INPUT_SOURCE_FILES " ] && includes_arg=" -includes=${INPUT_SOURCE_FILES} "
2724
2825 echo " 🔍 Executando CodeNarc..."
2926 java -jar /lib/codenarc-all.jar \
3027 -report=" $report " \
3128 -rulesetfiles=" ${INPUT_RULESETFILES} " \
3229 -basedir=" ." \
33- $includes_arg \
34- > " $CODENARC_RESULT "
30+ $includes_arg > " $CODENARC_RESULT "
3531
36- echo " "
3732 echo " "
3833 echo " 📋 Saída do CodeNarc:"
3934 echo " "
40- echo " "
4135 cat " $CODENARC_RESULT "
4236 echo " "
43- echo " "
4437}
4538
46- # ========== ETAPA 2 - REVIEWDOG ==========
4739run_reviewdog_with_config () {
4840 input_file=" $1 "
4941 efm=" $2 "
@@ -68,15 +60,14 @@ separate_violations() {
6860}
6961
7062run_reviewdog () {
71- separate_violations
72-
73- if [ ! -s " $LINE_VIOLATIONS " ] && [ ! -s " $FILE_VIOLATIONS " ]; then
63+ if ! grep -qE ' ^[^:]+:[0-9]+:|^[^:]+:(null:|\|\|)' " $CODENARC_RESULT " ; then
7464 return 0
7565 fi
7666
67+ separate_violations
68+
7769 echo " 📤 Enviando resultados para reviewdog..."
7870 echo " "
79- echo " "
8071
8172 if [ -s " $LINE_VIOLATIONS " ]; then
8273 run_reviewdog_with_config " $LINE_VIOLATIONS " " %f:%l:%m" \
@@ -85,7 +76,6 @@ run_reviewdog() {
8576 fi
8677
8778 if [ -s " $FILE_VIOLATIONS " ]; then
88- true > " ${FILE_VIOLATIONS} .formatted"
8979 while read -r violation; do
9080 if echo " $violation " | grep -q ' ||' ; then
9181 echo " $violation " | sed ' s/||/::/g'
@@ -169,37 +159,23 @@ build_changed_lines_cache() {
169159 done < " $ALL_DIFF "
170160}
171161
172- # ========== FUNÇÕES AUXILIARES ==========
173162get_rule_priority () {
174163 rule_name=" $1 "
175-
176- # Busca por property name='RuleName' primeiro (override no XML)
177164 priority=$( echo " $INPUT_RULESETS_CONTENT " | grep -B 2 " name='$rule_name '" | grep -o ' priority" value="[0-9]' | head -1 | cut -d' "' -f3)
178-
179- # Se não encontrou, busca por class que termina com RuleNameRule (adiciona sufixo Rule)
180165 if [ -z " $priority " ]; then
181166 priority=$( echo " $INPUT_RULESETS_CONTENT " | grep " class='[^']*${rule_name} Rule'" -A 5 | grep -o ' priority" value="[0-9]' | head -1 | cut -d' "' -f3)
182167 fi
183-
184- # Se ainda não encontrou, tenta sem adicionar Rule (pode já ter o sufixo)
185168 if [ -z " $priority " ]; then
186169 priority=$( echo " $INPUT_RULESETS_CONTENT " | grep " class='[^']*${rule_name} '" -A 5 | grep -o ' priority" value="[0-9]' | head -1 | cut -d' "' -f3)
187170 fi
188-
189- # Se ainda não encontrou, busca em rule-script com property name
190171 if [ -z " $priority " ]; then
191172 priority=$( echo " $INPUT_RULESETS_CONTENT " | grep -A 3 " path='[^']*${rule_name} " | grep -o ' priority" value="[0-9]' | head -1 | cut -d' "' -f3)
192173 fi
193-
194174 echo " ${priority:- 2} "
195175}
196176
197177extract_rule_name () {
198- violation_line=" $1 "
199-
200- # Formato: file:line:RuleName Message ou file:null:RuleName Message
201- # Extrai apenas o RuleName (terceiro campo após os dois pontos)
202- echo " $violation_line " | sed -E ' s/^[^:]+:[^:]+:([A-Za-z0-9]+).*/\1/'
178+ echo " $1 " | sed -E ' s/^[^:]+:[^:]+:([A-Za-z0-9]+).*/\1/'
203179}
204180
205181get_p1_count () {
@@ -214,9 +190,7 @@ get_allowed_patterns() {
214190file_matches_patterns () {
215191 file=" $1 "
216192 patterns=" $2 "
217-
218193 [ -z " $patterns " ] && return 0
219-
220194 for pattern in $patterns ; do
221195 echo " $file " | grep -Eq " $pattern " && return 0
222196 done
@@ -231,18 +205,14 @@ is_file_changed() {
231205 grep -q " ^$1 $" " $CHANGED_FILES_CACHE "
232206}
233207
234- # ========== ETAPA 4 - BLOQUEIO POR P1 ==========
235208check_blocking_rules () {
236209 echo " 🔎 Verificando violações bloqueantes (priority 1)..."
237-
238210 [ ! -f " $CODENARC_RESULT " ] && echo " ❌ Resultado não encontrado" && return 1
239211
240212 p1_count=$( get_p1_count)
241-
242213 if [ " $p1_count " -eq 0 ]; then
243214 echo " ✅ Nenhuma violação P1 detectada"
244215 echo " "
245- echo " "
246216 return 0
247217 fi
248218
@@ -258,22 +228,15 @@ check_blocking_rules() {
258228
259229 while IFS=: read -r file line rest; do
260230 [ -z " $file " ] && continue
261-
262- # Trata file-based violations (formato com ||)
263231 if echo " $file " | grep -q ' ||' ; then
264232 file=$( echo " $file " | cut -d' |' -f1)
265233 line=" "
266234 fi
267-
268235 file_matches_patterns " $file " " $allowed_patterns " || continue
269-
270- # Extrai o nome da regra e busca a priority no XML
271236 rule_name=$( extract_rule_name " $file :$line :$rest " )
272237 priority=$( get_rule_priority " $rule_name " )
273-
274238 [ " $priority " != " 1" ] && continue
275239
276- # Verifica se é file-based ou line-based
277240 if [ -z " $line " ] || [ " $line " = " null" ]; then
278241 if is_file_changed " $file " ; then
279242 p1_in_diff=$(( p1 _in_diff + 1 ))
@@ -288,8 +251,6 @@ check_blocking_rules() {
288251 done < " $TMP_VIOLATIONS "
289252
290253 rm -f " $TMP_VIOLATIONS "
291-
292- echo " "
293254 echo " "
294255 if [ " $( cat " $VIOLATIONS_FLAG " ) " -eq 1 ]; then
295256 echo " ❌ BLOQUEIO: $p1_in_diff violação(ões) P1 encontrada(s) em linhas/arquivos alterados do PR"
@@ -300,12 +261,9 @@ check_blocking_rules() {
300261 echo " ✅ APROVADO: Nenhuma violação P1 em linhas/arquivos alterados do PR"
301262 [ " $p1_outside_diff " -gt 0 ] && echo " ℹ️ ${p1_outside_diff} violação(ões) P1 em código não modificado (não bloqueia)"
302263 fi
303-
304- echo " "
305264 echo " "
306265}
307266
308- # ========== EXECUÇÃO PRINCIPAL ==========
309267if [ -n " ${GITHUB_WORKSPACE} " ]; then
310268 cd " ${GITHUB_WORKSPACE} /${INPUT_WORKDIR} " || exit
311269 git config --global --add safe.directory " $GITHUB_WORKSPACE "
@@ -317,4 +275,4 @@ run_codenarc
317275run_reviewdog
318276check_blocking_rules
319277
320- echo " 🏁 Concluído com sucesso"
278+ echo " 🏁 Concluído com sucesso"
0 commit comments