Skip to content

Commit 3ce90bd

Browse files
Merge pull request #61 from asaasdev/fix-codenarc-bloqueio-merge
fix: implementa bloqueio inteligente de P1s apenas em linhas alteradas no CodeNarc
2 parents 131454d + daf4cb3 commit 3ce90bd

File tree

1 file changed

+203
-24
lines changed

1 file changed

+203
-24
lines changed

entrypoint.sh

Lines changed: 203 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,227 @@
11
#!/bin/sh
22
set -e
3-
trap 'rm -f result.txt >/dev/null 2>&1' EXIT
3+
4+
CODENARC_RESULT="result.txt"
5+
LINE_VIOLATIONS="line_violations.txt"
6+
FILE_VIOLATIONS="file_violations.txt"
7+
VIOLATIONS_FLAG="/tmp/found_violations.txt"
8+
ALL_DIFF="/tmp/all_diff.txt"
9+
CHANGED_LINES_CACHE="/tmp/changed_lines.txt"
10+
CHANGED_FILES_CACHE="/tmp/changed_files.txt"
11+
12+
cleanup_temp_files() {
13+
rm -f "$CODENARC_RESULT" "$LINE_VIOLATIONS" "$FILE_VIOLATIONS" "$VIOLATIONS_FLAG" \
14+
"$ALL_DIFF" "$CHANGED_LINES_CACHE" "$CHANGED_FILES_CACHE" \
15+
"${FILE_VIOLATIONS}.formatted" >/dev/null 2>&1
16+
}
17+
18+
trap 'cleanup_temp_files' EXIT
419

520
run_codenarc() {
621
report="${INPUT_REPORT:-compact:stdout}"
722
includes_arg=""
8-
9-
if [ -n "$INPUT_SOURCE_FILES" ]; then
10-
includes_arg="-includes=${INPUT_SOURCE_FILES}"
11-
fi
12-
23+
24+
[ -n "$INPUT_SOURCE_FILES" ] && includes_arg="-includes=${INPUT_SOURCE_FILES}"
25+
1326
echo "🔍 Executando CodeNarc..."
1427
java -jar /lib/codenarc-all.jar \
1528
-report="$report" \
1629
-rulesetfiles="${INPUT_RULESETFILES}" \
1730
-basedir="." \
1831
$includes_arg \
19-
> result.txt
32+
> "$CODENARC_RESULT"
33+
}
34+
35+
run_reviewdog_with_config() {
36+
input_file="$1"
37+
efm="$2"
38+
reporter="$3"
39+
name="$4"
40+
filter_mode="$5"
41+
level="$6"
42+
43+
< "$input_file" reviewdog \
44+
-efm="$efm" \
45+
-reporter="$reporter" \
46+
-name="$name" \
47+
-filter-mode="$filter_mode" \
48+
-fail-on-error="false" \
49+
-level="$level" \
50+
${INPUT_REVIEWDOG_FLAGS} || true
51+
}
52+
53+
separate_violations() {
54+
grep -E ':[0-9]+:' "$CODENARC_RESULT" > "$LINE_VIOLATIONS" || true
55+
grep -E ':null:|\|\|' "$CODENARC_RESULT" > "$FILE_VIOLATIONS" || true
2056
}
2157

2258
run_reviewdog() {
2359
echo "📤 Enviando resultados para reviewdog..."
24-
< result.txt reviewdog -efm="%f:%l:%m" -efm="%f:%r:%m" \
25-
-name="codenarc" \
26-
-reporter="${INPUT_REPORTER:-github-pr-check}" \
27-
-filter-mode="${INPUT_FILTER_MODE}" \
28-
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
29-
-level="${INPUT_LEVEL}" \
30-
${INPUT_REVIEWDOG_FLAGS}
60+
61+
separate_violations
62+
63+
if [ -s "$LINE_VIOLATIONS" ]; then
64+
echo "📤 Enviando violações line-based (${INPUT_REPORTER:-github-pr-check})..."
65+
run_reviewdog_with_config "$LINE_VIOLATIONS" "%f:%l:%m" \
66+
"${INPUT_REPORTER:-github-pr-check}" "codenarc" \
67+
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
68+
fi
69+
70+
if [ -s "$FILE_VIOLATIONS" ]; then
71+
true > "${FILE_VIOLATIONS}.formatted"
72+
while read -r violation; do
73+
if echo "$violation" | grep -q '||'; then
74+
echo "$violation" | sed 's/||/::/'
75+
else
76+
echo "$violation" | sed 's/:null:/::/'
77+
fi
78+
done < "$FILE_VIOLATIONS" > "${FILE_VIOLATIONS}.formatted"
79+
80+
if [ "${INPUT_REPORTER}" = "local" ]; then
81+
echo "📤 Enviando violações file-based (local)..."
82+
run_reviewdog_with_config "${FILE_VIOLATIONS}.formatted" "%f::%m" \
83+
"local" "codenarc" "nofilter" "${INPUT_LEVEL}"
84+
else
85+
echo "📤 Enviando violações file-based (github-pr-check)..."
86+
run_reviewdog_with_config "${FILE_VIOLATIONS}.formatted" "%f::%m" \
87+
"github-pr-check" "codenarc" "nofilter" "warning"
88+
fi
89+
fi
90+
91+
# fallback se nao houver violacoes categorizadas
92+
if [ ! -s "$LINE_VIOLATIONS" ] && [ ! -s "$FILE_VIOLATIONS" ]; then
93+
echo "📝 Executando reviewdog padrão..."
94+
run_reviewdog_with_config "$CODENARC_RESULT" "%f:%l:%m" \
95+
"${INPUT_REPORTER:-github-pr-check}" "codenarc" \
96+
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
97+
fi
3198
}
3299

33-
check_blocking_rules() {
34-
echo "🔎 Verificando violacoes bloqueantes (priority 1)..."
100+
generate_git_diff() {
101+
if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
102+
git fetch origin "$GITHUB_BASE_SHA" --depth=1 2>/dev/null || true
103+
git fetch origin "$GITHUB_HEAD_SHA" --depth=1 2>/dev/null || true
104+
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- '*.groovy'
105+
else
106+
git diff -U0 HEAD~1 -- '*.groovy'
107+
fi
108+
}
109+
110+
parse_diff_range() {
111+
range="$1"
112+
if echo "$range" | grep -q ","; then
113+
echo "$(echo "$range" | cut -d',' -f1) $(echo "$range" | cut -d',' -f2)"
114+
else
115+
echo "$range 1"
116+
fi
117+
}
118+
119+
build_changed_lines_cache() {
120+
true > "$CHANGED_LINES_CACHE"
121+
true > "$CHANGED_FILES_CACHE"
122+
123+
generate_git_diff > "$ALL_DIFF" 2>/dev/null || true
124+
[ ! -s "$ALL_DIFF" ] && return 0
125+
126+
current_file=""
127+
while read -r line; do
128+
case "$line" in
129+
"diff --git"*)
130+
current_file=$(echo "$line" | sed 's|^diff --git a/\(.*\) b/.*|\1|')
131+
[ -n "$current_file" ] && echo "$current_file" >> "$CHANGED_FILES_CACHE"
132+
;;
133+
"@@"*)
134+
[ -z "$current_file" ] && continue
135+
range=$(echo "$line" | sed 's/.*+\([0-9,]*\).*/\1/')
136+
range_info=$(parse_diff_range "$range")
137+
start=$(echo "$range_info" | cut -d' ' -f1)
138+
count=$(echo "$range_info" | cut -d' ' -f2)
139+
140+
case "$start" in ''|*[!0-9]*) continue ;; esac
141+
case "$count" in ''|*[!0-9]*) continue ;; esac
142+
143+
i="$start"
144+
while [ "$i" -lt "$((start + count))" ]; do
145+
echo "$current_file:$i" >> "$CHANGED_LINES_CACHE"
146+
i=$((i + 1))
147+
done
148+
;;
149+
esac
150+
done < "$ALL_DIFF"
151+
}
152+
153+
get_p1_count() {
154+
p1_count=$(grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1)
155+
echo "${p1_count:-0}"
156+
}
157+
158+
get_allowed_patterns() {
159+
[ -n "$INPUT_SOURCE_FILES" ] && echo "$INPUT_SOURCE_FILES" | tr ',' '\n' | sed 's/\*\*/.*/g'
160+
}
161+
162+
file_matches_patterns() {
163+
file="$1"
164+
patterns="$2"
165+
166+
[ -z "$patterns" ] && return 0
167+
168+
for pattern in $patterns; do
169+
echo "$file" | grep -Eq "$pattern" && return 0
170+
done
171+
return 1
172+
}
35173

36-
p1_count=$(grep -Eo "p1=[0-9]+" result.txt | cut -d'=' -f2 | head -1)
37-
p1_count=${p1_count:-0}
174+
is_line_changed() {
175+
grep -q "^$2:$1$" "$CHANGED_LINES_CACHE"
176+
}
38177

39-
echo "📊 Resumo CodeNarc → priority 1=${p1_count}"
178+
is_file_changed() {
179+
grep -q "^$1$" "$CHANGED_FILES_CACHE"
180+
}
40181

41-
if [ "$p1_count" -gt 0 ]; then
42-
echo "⛔ Foram encontradas violacoes bloqueantes (priority 1)."
43-
echo "💡 Corrija as violacoes ou use o bypass autorizado pelo coordenador."
182+
check_blocking_rules() {
183+
echo "🔎 Verificando violações bloqueantes (priority 1)..."
184+
185+
[ ! -f "$CODENARC_RESULT" ] && echo "❌ Resultado não encontrado" && return 1
186+
187+
p1_count=$(get_p1_count)
188+
echo "📊 Total de P1 encontradas: $p1_count"
189+
190+
[ "$p1_count" -eq 0 ] && echo "✅ Nenhuma P1 detectada → merge permitido" && return 0
191+
192+
echo "⚠️ Verificando P1s em linhas alteradas..."
193+
build_changed_lines_cache
194+
195+
allowed_patterns=$(get_allowed_patterns)
196+
[ -n "$allowed_patterns" ] && echo "🧩 Analisando apenas arquivos filtrados por INPUT_SOURCE_FILES"
197+
198+
echo "0" > "$VIOLATIONS_FLAG"
199+
200+
grep -E ':[0-9]+:|:null:|\|\|' "$CODENARC_RESULT" | while IFS=: read -r file line rest; do
201+
if echo "$file" | grep -q '||'; then
202+
file=$(echo "$file" | cut -d'|' -f1)
203+
line=""
204+
fi
205+
[ -z "$file" ] && continue
206+
file_matches_patterns "$file" "$allowed_patterns" || continue
207+
208+
if [ -z "$line" ] || [ "$line" = "null" ]; then
209+
if is_file_changed "$file"; then
210+
echo "📍 Violação file-based em arquivo alterado: $file"
211+
echo "1" > "$VIOLATIONS_FLAG" && break
212+
fi
213+
elif is_line_changed "$line" "$file"; then
214+
echo "📍 Violação em linha alterada: $file:$line"
215+
echo "1" > "$VIOLATIONS_FLAG" && break
216+
fi
217+
done
218+
219+
if [ "$(cat "$VIOLATIONS_FLAG")" -eq 1 ]; then
220+
echo "⛔ P1s existem E há violações em linhas alteradas → DEVERIA bloquear merge"
221+
echo "🔧 Exit desabilitado temporariamente para monitoramento"
222+
# exit 1
44223
else
45-
echo "Nenhuma violacao bloqueante (priority 1) encontrada."
224+
echo "P1s existem mas fora das linhas alteradas → merge permitido"
46225
fi
47226
}
48227

@@ -57,4 +236,4 @@ run_codenarc
57236
run_reviewdog
58237
check_blocking_rules
59238

60-
echo "🏁 Finalizado com sucesso."
239+
echo "🏁 Concluído com sucesso"

0 commit comments

Comments
 (0)