Skip to content

Commit 693b302

Browse files
ajusta logica do bloqueio de merge do codenarc para quebrar apenas em p1 no diff
1 parent 6e8a0a2 commit 693b302

File tree

1 file changed

+95
-96
lines changed

1 file changed

+95
-96
lines changed

entrypoint.sh

Lines changed: 95 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,144 @@
11
#!/bin/sh
22
set -e
3-
trap 'rm -f result.txt reviewdog_output.txt /tmp/diff.txt /tmp/file_diff.txt /tmp/found_violations.txt >/dev/null 2>&1' EXIT
3+
trap 'cleanup_temp_files' EXIT
4+
5+
CODENARC_RESULT="result.txt"
6+
VIOLATIONS_FLAG="/tmp/found_violations.txt"
7+
FILE_DIFF="/tmp/file_diff.txt"
8+
9+
cleanup_temp_files() {
10+
rm -f "$CODENARC_RESULT" "$VIOLATIONS_FLAG" "$FILE_DIFF" >/dev/null 2>&1
11+
}
412

513
run_codenarc() {
614
report="${INPUT_REPORT:-compact:stdout}"
715
includes_arg=""
16+
817
if [ -n "$INPUT_SOURCE_FILES" ]; then
918
includes_arg="-includes=${INPUT_SOURCE_FILES}"
1019
fi
20+
1121
echo "🔍 Executando CodeNarc..."
1222
java -jar /lib/codenarc-all.jar \
1323
-report="$report" \
1424
-rulesetfiles="${INPUT_RULESETFILES}" \
1525
-basedir="." \
1626
$includes_arg \
17-
> result.txt
18-
19-
echo "📋 Resultado do CodeNarc:"
20-
cat result.txt
21-
echo "📋 Fim do resultado CodeNarc"
27+
> "$CODENARC_RESULT"
2228
}
2329

2430
run_reviewdog() {
2531
echo "📤 Enviando resultados para reviewdog..."
26-
< result.txt reviewdog \
32+
< "$CODENARC_RESULT" reviewdog \
2733
-efm="%f:%l:%m" -efm="%f:%r:%m" \
2834
-name="codenarc" \
2935
-reporter="${INPUT_REPORTER:-github-pr-check}" \
3036
-filter-mode="${INPUT_FILTER_MODE}" \
3137
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
3238
-level="${INPUT_LEVEL}" \
33-
${INPUT_REVIEWDOG_FLAGS} \
34-
-tee > reviewdog_output.txt
39+
${INPUT_REVIEWDOG_FLAGS}
3540
}
3641

37-
generate_diff() {
38-
echo "🔎 Gerando diff entre commits..."
42+
generate_git_diff() {
3943
if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
40-
echo " Base: $GITHUB_BASE_SHA"
41-
echo " Head: $GITHUB_HEAD_SHA"
42-
git fetch origin $GITHUB_BASE_SHA --depth=1 2>/dev/null || true
43-
git fetch origin $GITHUB_HEAD_SHA --depth=1 2>/dev/null || true
44-
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" > /tmp/diff.txt || true
44+
git fetch origin "$GITHUB_BASE_SHA" --depth=1 2>/dev/null || true
45+
git fetch origin "$GITHUB_HEAD_SHA" --depth=1 2>/dev/null || true
46+
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- "$1" 2>/dev/null || true
4547
else
46-
echo "⚠️ Refs base/head nao encontradas, usando HEAD~1..."
47-
git diff -U0 HEAD~1 > /tmp/diff.txt || true
48+
git diff -U0 HEAD~1 -- "$1" 2>/dev/null || true
4849
fi
4950
}
5051

51-
check_blocking_rules() {
52-
echo "🔎 Verificando violacoes bloqueantes (priority 1)..."
53-
54-
p1_total=$(grep -Eo "p1=[0-9]+" result.txt | cut -d'=' -f2 | head -1)
55-
p1_total=${p1_total:-0}
52+
get_p1_violations_count() {
53+
grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1 | grep -o '[0-9]*' || echo "0"
54+
}
5655

57-
echo "📊 Total de P1 encontradas pelo CodeNarc: ${p1_total}"
58-
[ "$p1_total" -eq 0 ] && echo "✅ Nenhuma violacao P1 detectada." && return 0
56+
parse_allowed_file_patterns() {
57+
[ -n "$INPUT_SOURCE_FILES" ] && echo "$INPUT_SOURCE_FILES" | tr ',' '\n' | sed 's/\*\*/.*/g'
58+
}
5959

60-
echo "🔍 Cruzando violacoes P1 com linhas alteradas..."
61-
found=0
60+
file_matches_patterns() {
61+
file="$1"
62+
patterns="$2"
63+
64+
[ -z "$patterns" ] && return 0
65+
66+
echo "$patterns" | while read -r pattern; do
67+
if echo "$file" | grep -Eq "$pattern"; then
68+
return 0
69+
fi
70+
done
71+
return 1
72+
}
6273

63-
allowed_files=""
64-
if [ -n "$INPUT_SOURCE_FILES" ]; then
65-
allowed_files=$(echo "$INPUT_SOURCE_FILES" | tr ',' '\n' | sed 's/\*\*/.*/g')
66-
echo "🧩 Filtrando apenas arquivos em INPUT_SOURCE_FILES:"
67-
echo "$allowed_files"
74+
parse_diff_range() {
75+
range=$(echo "$1" | sed 's/.*+\([0-9,]*\).*/\1/')
76+
77+
if echo "$range" | grep -q ","; then
78+
echo "$(echo "$range" | cut -d',' -f1) $(echo "$range" | cut -d',' -f2)"
79+
else
80+
echo "$range 1"
6881
fi
82+
}
6983

70-
echo "0" > /tmp/found_violations.txt
84+
line_is_in_changed_range() {
85+
target_line="$1"
86+
file="$2"
7187

72-
grep -E ':[0-9]+:' result.txt | while IFS=: read -r file line rest; do
73-
[ -z "$file" ] && continue
74-
echo "🔍 Analisando violacao: $file:$line"
75-
76-
if [ -n "$allowed_files" ]; then
77-
matched=0
78-
for pattern in $allowed_files; do
79-
if echo "$file" | grep -Eq "$pattern"; then
80-
matched=1
81-
echo "✅ Arquivo $file corresponde ao padrão $pattern"
82-
break
83-
fi
84-
done
85-
if [ "$matched" -eq 0 ]; then
86-
echo "⏭️ Arquivo $file não corresponde aos padrões permitidos"
87-
continue
88+
generate_git_diff "$file" > "$FILE_DIFF"
89+
90+
while read -r diff_line; do
91+
if echo "$diff_line" | grep -q "^@@"; then
92+
range_info=$(parse_diff_range "$diff_line")
93+
start=$(echo "$range_info" | cut -d' ' -f1)
94+
count=$(echo "$range_info" | cut -d' ' -f2)
95+
96+
if [ "$target_line" -ge "$start" ] && [ "$target_line" -lt "$((start + count))" ]; then
97+
return 0
8898
fi
8999
fi
100+
done < "$FILE_DIFF"
101+
102+
return 1
103+
}
90104

91-
if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
92-
git diff --no-color -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- "$file" > /tmp/file_diff.txt 2>/dev/null || true
93-
else
94-
git diff --no-color -U0 HEAD~1 -- "$file" > /tmp/file_diff.txt 2>/dev/null || true
95-
fi
96-
97-
echo "📄 Diff do arquivo $file:"
98-
cat /tmp/file_diff.txt
99-
echo "📄 Fim do diff"
100-
101-
match=""
102-
if grep -q "^@@" /tmp/file_diff.txt; then
103-
while read -r diff_line; do
104-
if echo "$diff_line" | grep -q "^@@"; then
105-
echo "🔍 Processando linha de diff: $diff_line"
106-
range=$(echo "$diff_line" | sed 's/.*+\([0-9,]*\).*/\1/')
107-
echo "📍 Range extraído: $range"
108-
109-
if echo "$range" | grep -q ","; then
110-
start=$(echo "$range" | cut -d',' -f1)
111-
count=$(echo "$range" | cut -d',' -f2)
112-
else
113-
start="$range"
114-
count=1
115-
fi
116-
117-
echo "📊 Verificando se linha $line está entre $start e $((start + count - 1))"
118-
if [ "$line" -ge "$start" ] && [ "$line" -lt "$((start + count))" ]; then
119-
match="hit"
120-
echo "🎯 MATCH! Linha $line está no range alterado"
121-
break
122-
fi
123-
fi
124-
done < /tmp/file_diff.txt
105+
check_blocking_rules() {
106+
107+
echo "🔎 Verificando violacoes bloqueantes (priority 1)..."
108+
109+
p1_count=$(get_p1_violations_count)
110+
echo "📊 Total de P1 encontradas: $p1_count"
111+
112+
[ "$p1_count" -eq 0 ] && echo "✅ Nenhuma violacao P1 detectada." && return 0
113+
114+
allowed_patterns=$(parse_allowed_file_patterns)
115+
if [ -n "$allowed_patterns" ]; then
116+
echo "🧩 Analisando apenas arquivos filtrados por INPUT_SOURCE_FILES"
117+
fi
118+
119+
echo "0" > "$VIOLATIONS_FLAG"
120+
121+
grep -E ':[0-9]+:' "$CODENARC_RESULT" | while IFS=: read -r file line rest; do
122+
[ -z "$file" ] && continue
123+
124+
if ! file_matches_patterns "$file" "$allowed_patterns"; then
125+
continue
125126
fi
126-
127-
if [ "$match" = "hit" ]; then
128-
echo "🚨 Violacao P1 no diff: $file:$line"
129-
echo "1" > /tmp/found_violations.txt
127+
128+
if line_is_in_changed_range "$line" "$file"; then
129+
echo "🚨 Violacao P1 em linha alterada: $file:$line"
130+
echo "1" > "$VIOLATIONS_FLAG"
130131
fi
131132
done
132133

133-
found=$(cat /tmp/found_violations.txt)
134-
135-
echo "🔍 Resultado final: found=$found"
136-
if [ "$found" -eq 1 ]; then
137-
echo "⛔ Foram encontradas violacoes P1 em linhas alteradas (arquivos filtrados)."
138-
echo "💡 Corrija as violacoes ou utilize o bypass autorizado."
134+
violations_in_diff=$(cat "$VIOLATIONS_FLAG")
135+
136+
if [ "$violations_in_diff" -eq 1 ]; then
137+
echo "⛔ Violacoes P1 encontradas em linhas alteradas - bloqueando merge"
138+
echo "💡 Corrija as violacoes ou utilize bypass autorizado"
139139
exit 1
140140
else
141-
echo "⚠️ Existem violacoes P1, mas fora das linhas alteradas ou fora dos arquivos analisados (nao bloqueia o merge)."
141+
echo "⚠️ Violacoes P1 existem mas fora das linhas alteradas - merge permitido"
142142
fi
143143
}
144144

@@ -151,7 +151,6 @@ export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
151151

152152
run_codenarc
153153
run_reviewdog
154-
generate_diff
155154
check_blocking_rules
156155

157-
echo "🏁 Finalizado com sucesso."
156+
echo "🏁 Concluido com sucesso"

0 commit comments

Comments
 (0)