Skip to content

Commit 9d388c4

Browse files
ajusta logica do bloqueio de merge do codenarc para quebrar apenas em p1 no diff
1 parent dceefde commit 9d388c4

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

entrypoint.sh

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ trap 'cleanup_temp_files' EXIT
44

55
CODENARC_RESULT="result.txt"
66
VIOLATIONS_FLAG="/tmp/found_violations.txt"
7-
FILE_DIFF="/tmp/file_diff.txt"
7+
ALL_DIFF="/tmp/all_diff.txt"
8+
CHANGED_LINES_CACHE="/tmp/changed_lines.txt"
89

910
cleanup_temp_files() {
10-
rm -f "$CODENARC_RESULT" "$VIOLATIONS_FLAG" "$FILE_DIFF" >/dev/null 2>&1
11+
rm -f "$CODENARC_RESULT" "$VIOLATIONS_FLAG" "$ALL_DIFF" "$CHANGED_LINES_CACHE" >/dev/null 2>&1
1112
}
1213

1314
run_codenarc() {
@@ -39,16 +40,44 @@ run_reviewdog() {
3940
${INPUT_REVIEWDOG_FLAGS}
4041
}
4142

42-
generate_git_diff() {
43+
generate_all_diff() {
4344
if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
4445
git fetch origin "$GITHUB_BASE_SHA" --depth=1 2>/dev/null || true
4546
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
47+
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- '*.groovy' > "$ALL_DIFF" 2>/dev/null || true
4748
else
48-
git diff -U0 HEAD~1 -- "$1" 2>/dev/null || true
49+
git diff -U0 HEAD~1 -- '*.groovy' > "$ALL_DIFF" 2>/dev/null || true
4950
fi
5051
}
5152

53+
build_changed_lines_cache() {
54+
> "$CHANGED_LINES_CACHE"
55+
current_file=""
56+
57+
while read -r line; do
58+
if echo "$line" | grep -q "^diff --git"; then
59+
current_file=$(echo "$line" | sed 's|^diff --git a/\(.*\) b/.*|\1|')
60+
elif echo "$line" | grep -q "^@@"; then
61+
if [ -n "$current_file" ]; then
62+
range=$(echo "$line" | sed 's/.*+\([0-9,]*\).*/\1/')
63+
if echo "$range" | grep -q ","; then
64+
start=$(echo "$range" | cut -d',' -f1)
65+
count=$(echo "$range" | cut -d',' -f2)
66+
else
67+
start="$range"
68+
count=1
69+
fi
70+
71+
i="$start"
72+
while [ "$i" -lt "$((start + count))" ]; do
73+
echo "$current_file:$i" >> "$CHANGED_LINES_CACHE"
74+
i=$((i + 1))
75+
done
76+
fi
77+
fi
78+
done < "$ALL_DIFF"
79+
}
80+
5281
get_p1_violations_count() {
5382
grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1 | grep -o '[0-9]*' || echo "0"
5483
}
@@ -86,32 +115,21 @@ line_is_in_changed_range() {
86115
target_line="$1"
87116
file="$2"
88117

89-
generate_git_diff "$file" > "$FILE_DIFF"
90-
91-
while read -r diff_line; do
92-
if echo "$diff_line" | grep -q "^@@"; then
93-
range_info=$(parse_diff_range "$diff_line")
94-
start=$(echo "$range_info" | cut -d' ' -f1)
95-
count=$(echo "$range_info" | cut -d' ' -f2)
96-
97-
if [ "$target_line" -ge "$start" ] && [ "$target_line" -lt "$((start + count))" ]; then
98-
return 0
99-
fi
100-
fi
101-
done < "$FILE_DIFF"
102-
103-
return 1
118+
grep -q "^$file:$target_line$" "$CHANGED_LINES_CACHE"
104119
}
105120

106121
check_blocking_rules() {
107-
108122
echo "🔎 Verificando violacoes bloqueantes (priority 1)..."
109123

110124
p1_count=$(get_p1_violations_count)
111125
echo "📊 Total de P1 encontradas: $p1_count"
112126

113127
[ "$p1_count" -eq 0 ] && echo "✅ Nenhuma violacao P1 detectada." && return 0
114128

129+
echo "🔎 Gerando cache de linhas alteradas..."
130+
generate_all_diff
131+
build_changed_lines_cache
132+
115133
allowed_patterns=$(parse_allowed_file_patterns)
116134
if [ -n "$allowed_patterns" ]; then
117135
echo "🧩 Analisando apenas arquivos filtrados por INPUT_SOURCE_FILES"
@@ -122,6 +140,8 @@ check_blocking_rules() {
122140
grep -E ':[0-9]+:' "$CODENARC_RESULT" | while IFS=: read -r file line rest; do
123141
[ -z "$file" ] && continue
124142

143+
echo "$file" | grep -q '\.groovy$' || continue
144+
125145
if ! file_matches_patterns "$file" "$allowed_patterns"; then
126146
continue
127147
fi

0 commit comments

Comments
 (0)