Skip to content

Commit 645a9a1

Browse files
testando abordagem hibrida reviewdog
1 parent 0bc4145 commit 645a9a1

File tree

1 file changed

+108
-82
lines changed

1 file changed

+108
-82
lines changed

entrypoint.sh

Lines changed: 108 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
#!/bin/sh
22
set -e
3-
trap 'cleanup_temp_files' EXIT
43

4+
# Constants
55
CODENARC_RESULT="result.txt"
6+
LINE_VIOLATIONS="line_violations.txt"
7+
FILE_VIOLATIONS="file_violations.txt"
68
VIOLATIONS_FLAG="/tmp/found_violations.txt"
79
ALL_DIFF="/tmp/all_diff.txt"
810
CHANGED_LINES_CACHE="/tmp/changed_lines.txt"
911
CHANGED_FILES_CACHE="/tmp/changed_files.txt"
10-
TEMP_VIOLATIONS="/tmp/temp_violations.txt"
1112

1213
cleanup_temp_files() {
13-
rm -f "$CODENARC_RESULT" "$VIOLATIONS_FLAG" "$ALL_DIFF" "$CHANGED_LINES_CACHE" "$CHANGED_FILES_CACHE" "$TEMP_VIOLATIONS" >/dev/null 2>&1
14+
rm -f "$CODENARC_RESULT" "$LINE_VIOLATIONS" "$FILE_VIOLATIONS" "$VIOLATIONS_FLAG" \
15+
"$ALL_DIFF" "$CHANGED_LINES_CACHE" "$CHANGED_FILES_CACHE" >/dev/null 2>&1
1416
}
1517

18+
trap 'cleanup_temp_files' EXIT
19+
1620
run_codenarc() {
1721
report="${INPUT_REPORT:-compact:stdout}"
1822
includes_arg=""
1923

20-
if [ -n "$INPUT_SOURCE_FILES" ]; then
21-
includes_arg="-includes=${INPUT_SOURCE_FILES}"
22-
fi
24+
[ -n "$INPUT_SOURCE_FILES" ] && includes_arg="-includes=${INPUT_SOURCE_FILES}"
2325

2426
echo "🔍 Executando CodeNarc..."
2527
java -jar /lib/codenarc-all.jar \
@@ -30,52 +32,98 @@ run_codenarc() {
3032
> "$CODENARC_RESULT"
3133
}
3234

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
56+
}
57+
3358
run_reviewdog() {
3459
echo "📤 Enviando resultados para reviewdog..."
35-
< "$CODENARC_RESULT" reviewdog \
36-
-efm="%f:%l:%m" -efm="%f:%r:%m" \
37-
-name="codenarc" \
38-
-reporter="${INPUT_REPORTER:-github-pr-check}" \
39-
-filter-mode="${INPUT_FILTER_MODE}" \
40-
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
41-
-level="${INPUT_LEVEL}" \
42-
${INPUT_REVIEWDOG_FLAGS}
60+
61+
separate_violations
62+
63+
# Violações line-based com reporter configurado
64+
if [ -s "$LINE_VIOLATIONS" ]; then
65+
echo "📤 Enviando violações com linha (${INPUT_REPORTER:-github-pr-check})..."
66+
run_reviewdog_with_config "$LINE_VIOLATIONS" "%f:%l:%m" \
67+
"${INPUT_REPORTER:-github-pr-check}" "codenarc-lines" \
68+
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
69+
fi
70+
71+
# Violações file-based forçando github-pr-check
72+
if [ -s "$FILE_VIOLATIONS" ]; then
73+
echo "📤 Enviando violações file-based (github-pr-check)..."
74+
run_reviewdog_with_config "$FILE_VIOLATIONS" "%f::%m" \
75+
"github-pr-check" "codenarc-files" "nofilter" "warning"
76+
fi
77+
78+
# Fallback se não houver violações categorizadas
79+
if [ ! -s "$LINE_VIOLATIONS" ] && [ ! -s "$FILE_VIOLATIONS" ]; then
80+
echo "📝 Executando reviewdog padrão..."
81+
run_reviewdog_with_config "$CODENARC_RESULT" "%f:%l:%m" \
82+
"${INPUT_REPORTER:-github-pr-check}" "codenarc" \
83+
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
84+
fi
4385
}
4486

45-
generate_all_diff() {
87+
generate_git_diff() {
4688
if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
4789
git fetch origin "$GITHUB_BASE_SHA" --depth=1 2>/dev/null || true
4890
git fetch origin "$GITHUB_HEAD_SHA" --depth=1 2>/dev/null || true
49-
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- '*.groovy' > "$ALL_DIFF" 2>/dev/null || true
91+
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- '*.groovy'
92+
else
93+
git diff -U0 HEAD~1 -- '*.groovy'
94+
fi
95+
}
96+
97+
parse_diff_range() {
98+
range="$1"
99+
if echo "$range" | grep -q ","; then
100+
echo "$(echo "$range" | cut -d',' -f1) $(echo "$range" | cut -d',' -f2)"
50101
else
51-
git diff -U0 HEAD~1 -- '*.groovy' > "$ALL_DIFF" 2>/dev/null || true
102+
echo "$range 1"
52103
fi
53104
}
54105

55106
build_changed_lines_cache() {
56107
true > "$CHANGED_LINES_CACHE"
57108
true > "$CHANGED_FILES_CACHE"
58-
current_file=""
59109

110+
generate_git_diff > "$ALL_DIFF" 2>/dev/null || true
60111
[ ! -s "$ALL_DIFF" ] && return 0
61112

113+
current_file=""
62114
while read -r line; do
63-
if echo "$line" | grep -q "^diff --git"; then
64-
current_file=$(echo "$line" | sed 's|^diff --git a/\(.*\) b/.*|\1|')
65-
if [ -n "$current_file" ]; then
66-
echo "$current_file" >> "$CHANGED_FILES_CACHE"
67-
fi
68-
elif echo "$line" | grep -q "^@@"; then
69-
if [ -n "$current_file" ]; then
115+
case "$line" in
116+
"diff --git"*)
117+
current_file=$(echo "$line" | sed 's|^diff --git a/\(.*\) b/.*|\1|')
118+
[ -n "$current_file" ] && echo "$current_file" >> "$CHANGED_FILES_CACHE"
119+
;;
120+
"@@"*)
121+
[ -z "$current_file" ] && continue
70122
range=$(echo "$line" | sed 's/.*+\([0-9,]*\).*/\1/')
71-
if echo "$range" | grep -q ","; then
72-
start=$(echo "$range" | cut -d',' -f1)
73-
count=$(echo "$range" | cut -d',' -f2)
74-
else
75-
start="$range"
76-
count=1
77-
fi
78-
123+
range_info=$(parse_diff_range "$range")
124+
start=$(echo "$range_info" | cut -d' ' -f1)
125+
count=$(echo "$range_info" | cut -d' ' -f2)
126+
79127
case "$start" in ''|*[!0-9]*) continue ;; esac
80128
case "$count" in ''|*[!0-9]*) continue ;; esac
81129

@@ -84,17 +132,17 @@ build_changed_lines_cache() {
84132
echo "$current_file:$i" >> "$CHANGED_LINES_CACHE"
85133
i=$((i + 1))
86134
done
87-
fi
88-
fi
135+
;;
136+
esac
89137
done < "$ALL_DIFF"
90138
}
91139

92-
get_p1_violations_count() {
140+
get_p1_count() {
93141
p1_count=$(grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1)
94142
echo "${p1_count:-0}"
95143
}
96144

97-
parse_allowed_file_patterns() {
145+
get_allowed_patterns() {
98146
[ -n "$INPUT_SOURCE_FILES" ] && echo "$INPUT_SOURCE_FILES" | tr ',' '\n' | sed 's/\*\*/.*/g'
99147
}
100148

@@ -105,77 +153,53 @@ file_matches_patterns() {
105153
[ -z "$patterns" ] && return 0
106154

107155
for pattern in $patterns; do
108-
if echo "$file" | grep -Eq "$pattern"; then
109-
return 0
110-
fi
156+
echo "$file" | grep -Eq "$pattern" && return 0
111157
done
112-
113158
return 1
114159
}
115160

116-
line_is_in_changed_range() {
117-
target_line="$1"
118-
file="$2"
119-
120-
grep -q "^$file:$target_line$" "$CHANGED_LINES_CACHE"
161+
is_line_changed() {
162+
grep -q "^$2:$1$" "$CHANGED_LINES_CACHE"
121163
}
122164

123-
file_was_changed() {
124-
file="$1"
125-
grep -q "^$file$" "$CHANGED_FILES_CACHE"
165+
is_file_changed() {
166+
grep -q "^$1$" "$CHANGED_FILES_CACHE"
126167
}
127168

128169
check_blocking_rules() {
129170
echo "🔎 Verificando violações bloqueantes (priority 1)..."
130171

131-
[ ! -f "$CODENARC_RESULT" ] && echo "Arquivo de resultado não encontrado" && return 1
172+
[ ! -f "$CODENARC_RESULT" ] && echo "Resultado não encontrado" && return 1
132173

133-
p1_count=$(get_p1_violations_count)
134-
echo "📊 Total de P1 encontradas: $p1_count"
174+
p1_count=$(get_p1_count)
175+
echo "📊 Total de P1: $p1_count"
135176

136-
if [ "$p1_count" -eq 0 ]; then
137-
echo "✅ Nenhuma P1 detectada → merge permitido (análise de diff desnecessária)"
138-
return 0
139-
fi
177+
[ "$p1_count" -eq 0 ] && echo "✅ Nenhuma P1 → merge permitido" && return 0
140178

141-
echo "⚠️ P1s detectadas → verificando se estão em linhas alteradas..."
142-
generate_all_diff
179+
echo "⚠️ Verificando P1s em linhas alteradas..."
143180
build_changed_lines_cache
144181

145-
allowed_patterns=$(parse_allowed_file_patterns)
146-
if [ -n "$allowed_patterns" ]; then
147-
echo "🧩 Analisando apenas arquivos filtrados por INPUT_SOURCE_FILES"
148-
fi
182+
allowed_patterns=$(get_allowed_patterns)
183+
[ -n "$allowed_patterns" ] && echo "🧩 Filtrando por INPUT_SOURCE_FILES"
149184

150185
echo "0" > "$VIOLATIONS_FLAG"
151186

152-
grep -E ':[0-9]+:|:[0-9]*:' "$CODENARC_RESULT" > "$TEMP_VIOLATIONS"
153-
154-
[ ! -s "$TEMP_VIOLATIONS" ] && echo "✅ Nenhuma violação encontrada no resultado" && return 0
155-
156-
while IFS=: read -r file line rest; do
187+
grep -E ':[0-9]+:|:null:|\|\|' "$CODENARC_RESULT" | while IFS=: read -r file line rest; do
157188
[ -z "$file" ] && continue
158-
159-
if ! file_matches_patterns "$file" "$allowed_patterns"; then
160-
continue
161-
fi
189+
file_matches_patterns "$file" "$allowed_patterns" || continue
162190

163191
if [ -z "$line" ] || [ "$line" = "null" ]; then
164-
if file_was_changed "$file"; then
192+
if is_file_changed "$file"; then
165193
echo "📍 Violação file-based em arquivo alterado: $file"
166-
echo "1" > "$VIOLATIONS_FLAG"
167-
break
194+
echo "1" > "$VIOLATIONS_FLAG" && break
168195
fi
169-
elif line_is_in_changed_range "$line" "$file"; then
196+
elif is_line_changed "$line" "$file"; then
170197
echo "📍 Violação em linha alterada: $file:$line"
171-
echo "1" > "$VIOLATIONS_FLAG"
172-
break
198+
echo "1" > "$VIOLATIONS_FLAG" && break
173199
fi
174-
done < "$TEMP_VIOLATIONS"
175-
176-
violations_in_diff=$(cat "$VIOLATIONS_FLAG")
200+
done
177201

178-
if [ "$violations_in_diff" -eq 1 ]; then
202+
if [ "$(cat "$VIOLATIONS_FLAG")" -eq 1 ]; then
179203
echo "⛔ P1s existem E há violações em linhas alteradas → DEVERIA bloquear merge"
180204
echo "🔧 Exit desabilitado temporariamente para monitoramento"
181205
# exit 1
@@ -184,15 +208,17 @@ check_blocking_rules() {
184208
fi
185209
}
186210

211+
# Setup
187212
if [ -n "${GITHUB_WORKSPACE}" ]; then
188213
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
189214
git config --global --add safe.directory "$GITHUB_WORKSPACE"
190215
fi
191216

192217
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
193218

219+
# Execute pipeline
194220
run_codenarc
195221
run_reviewdog
196222
check_blocking_rules
197223

198-
echo "🏁 Concluído com sucesso"
224+
echo "🏁 Concluído com sucesso"

0 commit comments

Comments
 (0)