Skip to content

Commit 0d57370

Browse files
refactor: consulta priority do XML ao inves de marcadores P1/P2
1 parent f9232ab commit 0d57370

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

entrypoint.sh

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ run_codenarc() {
3333
$includes_arg \
3434
> "$CODENARC_RESULT"
3535

36+
echo ""
3637
echo ""
3738
echo "📋 Saída do CodeNarc:"
3839
echo ""
40+
echo ""
3941
cat "$CODENARC_RESULT"
4042
echo ""
43+
echo ""
4144
}
4245

4346
# ========== ETAPA 2 - REVIEWDOG ==========
@@ -60,8 +63,8 @@ run_reviewdog_with_config() {
6063
}
6164

6265
separate_violations() {
63-
grep -E ':[0-9]+:' "$CODENARC_RESULT" > "$LINE_VIOLATIONS" || true
64-
grep -E ':null:|\|\|' "$CODENARC_RESULT" > "$FILE_VIOLATIONS" || true
66+
grep -E '^[^:]+:[0-9]+:' "$CODENARC_RESULT" > "$LINE_VIOLATIONS" || true
67+
grep -E '^[^:]+:(null:|\|\|)' "$CODENARC_RESULT" > "$FILE_VIOLATIONS" || true
6568
}
6669

6770
run_reviewdog() {
@@ -72,6 +75,8 @@ run_reviewdog() {
7275
fi
7376

7477
echo "📤 Enviando resultados para reviewdog..."
78+
echo ""
79+
echo ""
7580

7681
if [ -s "$LINE_VIOLATIONS" ]; then
7782
run_reviewdog_with_config "$LINE_VIOLATIONS" "%f:%l:%m" \
@@ -97,8 +102,6 @@ run_reviewdog() {
97102
"github-pr-check" "codenarc" "nofilter" "warning"
98103
fi
99104
fi
100-
101-
echo ""
102105
}
103106

104107
generate_git_diff() {
@@ -152,10 +155,10 @@ build_changed_lines_cache() {
152155
range_info=$(parse_diff_range "$range")
153156
start=$(echo "$range_info" | cut -d' ' -f1)
154157
count=$(echo "$range_info" | cut -d' ' -f2)
155-
158+
156159
case "$start" in ''|*[!0-9]*) continue ;; esac
157160
case "$count" in ''|*[!0-9]*) continue ;; esac
158-
161+
159162
i="$start"
160163
while [ "$i" -lt "$((start + count))" ]; do
161164
echo "$current_file:$i" >> "$CHANGED_LINES_CACHE"
@@ -169,31 +172,31 @@ build_changed_lines_cache() {
169172
# ========== FUNÇÕES AUXILIARES ==========
170173
get_rule_priority() {
171174
rule_name="$1"
172-
175+
173176
# Busca por property name='RuleName' primeiro (override no XML)
174177
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep -B 2 "name='$rule_name'" | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
175-
178+
176179
# Se não encontrou, busca por class que termina com RuleNameRule (adiciona sufixo Rule)
177180
if [ -z "$priority" ]; then
178181
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep "class='[^']*${rule_name}Rule'" -A 5 | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
179182
fi
180-
183+
181184
# Se ainda não encontrou, tenta sem adicionar Rule (pode já ter o sufixo)
182185
if [ -z "$priority" ]; then
183186
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep "class='[^']*${rule_name}'" -A 5 | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
184187
fi
185-
188+
186189
# Se ainda não encontrou, busca em rule-script com property name
187190
if [ -z "$priority" ]; then
188191
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep -A 3 "path='[^']*${rule_name}" | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
189192
fi
190-
193+
191194
echo "${priority:-2}"
192195
}
193196

194197
extract_rule_name() {
195198
violation_line="$1"
196-
199+
197200
# Formato: file:line:RuleName Message ou file:null:RuleName Message
198201
# Extrai apenas o RuleName (terceiro campo após os dois pontos)
199202
echo "$violation_line" | sed -E 's/^[^:]+:[^:]+:([A-Za-z0-9]+).*/\1/'
@@ -211,9 +214,9 @@ get_allowed_patterns() {
211214
file_matches_patterns() {
212215
file="$1"
213216
patterns="$2"
214-
217+
215218
[ -z "$patterns" ] && return 0
216-
219+
217220
for pattern in $patterns; do
218221
echo "$file" | grep -Eq "$pattern" && return 0
219222
done
@@ -231,14 +234,15 @@ is_file_changed() {
231234
# ========== ETAPA 4 - BLOQUEIO POR P1 ==========
232235
check_blocking_rules() {
233236
echo "🔎 Verificando violações bloqueantes (priority 1)..."
234-
237+
235238
[ ! -f "$CODENARC_RESULT" ] && echo "❌ Resultado não encontrado" && return 1
236239

237240
p1_count=$(get_p1_count)
238-
241+
239242
if [ "$p1_count" -eq 0 ]; then
240243
echo "✅ Nenhuma violação P1 detectada"
241244
echo ""
245+
echo ""
242246
return 0
243247
fi
244248

@@ -250,23 +254,23 @@ check_blocking_rules() {
250254

251255
echo "0" > "$VIOLATIONS_FLAG"
252256
p1_in_diff=0
253-
grep -E ':[0-9]+:|:null:|\|\|' "$CODENARC_RESULT" > "$TMP_VIOLATIONS" || true
257+
grep -E '^[^:]+:[0-9]+:|^[^:]+:(null:|\|\|)' "$CODENARC_RESULT" > "$TMP_VIOLATIONS" || true
254258

255259
while IFS=: read -r file line rest; do
256260
[ -z "$file" ] && continue
257-
261+
258262
# Trata file-based violations (formato com ||)
259263
if echo "$file" | grep -q '||'; then
260264
file=$(echo "$file" | cut -d'|' -f1)
261265
line=""
262266
fi
263-
267+
264268
file_matches_patterns "$file" "$allowed_patterns" || continue
265269

266270
# Extrai o nome da regra e busca a priority no XML
267271
rule_name=$(extract_rule_name "$file:$line:$rest")
268272
priority=$(get_rule_priority "$rule_name")
269-
273+
270274
[ "$priority" != "1" ] && continue
271275

272276
# Verifica se é file-based ou line-based
@@ -285,6 +289,7 @@ check_blocking_rules() {
285289

286290
rm -f "$TMP_VIOLATIONS"
287291

292+
echo ""
288293
echo ""
289294
if [ "$(cat "$VIOLATIONS_FLAG")" -eq 1 ]; then
290295
echo "❌ BLOQUEIO: $p1_in_diff violação(ões) P1 encontrada(s) em linhas/arquivos alterados do PR"
@@ -295,7 +300,8 @@ check_blocking_rules() {
295300
echo "✅ APROVADO: Nenhuma violação P1 em linhas/arquivos alterados do PR"
296301
[ "$p1_outside_diff" -gt 0 ] && echo "ℹ️ ${p1_outside_diff} violação(ões) P1 em código não modificado (não bloqueia)"
297302
fi
298-
303+
304+
echo ""
299305
echo ""
300306
}
301307

0 commit comments

Comments
 (0)