Skip to content

Commit 33f1f1d

Browse files
refactor: consulta priority do XML ao inves de marcadores P1/P2
1 parent 9befd71 commit 33f1f1d

3 files changed

Lines changed: 101 additions & 61 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ CodeNarc image with reviewdog.
77

88
## Test local
99

10-
```
11-
# build image
10+
### build image
11+
12+
```bash
1213
docker build -t docker.io/asaasdev/codenarc .
14+
```
1315

1416
# run container
17+
18+
```bash
1519
docker run --rm \
1620
--workdir /testdata \
1721
-e INPUT_REPORTER=local \
1822
-e INPUT_FILTER_MODE=nofilter \
1923
-e INPUT_FAIL_ON_ERROR=false \
2024
-e INPUT_LEVEL=error \
2125
-e INPUT_RULESETFILES=file:basic.xml \
26+
-e INPUT_RULESETS_CONTENT="$(cat testdata/basic.xml)" \
2227
-v $(pwd)/testdata:/testdata \
2328
docker.io/asaasdev/codenarc
24-
2529
```

entrypoint.sh

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ run_reviewdog() {
8282
true > "${FILE_VIOLATIONS}.formatted"
8383
while read -r violation; do
8484
if echo "$violation" | grep -q '||'; then
85-
echo "$violation" | sed 's/||/::/'
85+
echo "$violation" | sed 's/||/::/g'
8686
else
87-
echo "$violation" | sed 's/:null:/::/'
87+
echo "$violation" | sed 's/:null:/::/g'
8888
fi
8989
done < "$FILE_VIOLATIONS" > "${FILE_VIOLATIONS}.formatted"
9090

@@ -170,6 +170,38 @@ build_changed_lines_cache() {
170170
}
171171

172172
# ========== FUNÇÕES AUXILIARES ==========
173+
get_rule_priority() {
174+
rule_name="$1"
175+
176+
# Busca por property name='RuleName' primeiro (override no XML)
177+
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep -B 2 "name='$rule_name'" | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
178+
179+
# Se não encontrou, busca por class que termina com RuleNameRule (adiciona sufixo Rule)
180+
if [ -z "$priority" ]; then
181+
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep "class='[^']*${rule_name}Rule'" -A 5 | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
182+
fi
183+
184+
# Se ainda não encontrou, tenta sem adicionar Rule (pode já ter o sufixo)
185+
if [ -z "$priority" ]; then
186+
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep "class='[^']*${rule_name}'" -A 5 | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
187+
fi
188+
189+
# Se ainda não encontrou, busca em rule-script com property name
190+
if [ -z "$priority" ]; then
191+
priority=$(echo "$INPUT_RULESETS_CONTENT" | grep -A 3 "path='[^']*${rule_name}" | grep -o 'priority" value="[0-9]' | head -1 | cut -d'"' -f3)
192+
fi
193+
194+
echo "${priority:-2}"
195+
}
196+
197+
extract_rule_name() {
198+
violation_line="$1"
199+
200+
# Formato: file:line:RuleName Message ou file:null:RuleName Message
201+
# Extrai apenas o RuleName (terceiro campo após os dois pontos)
202+
echo "$violation_line" | sed -E 's/^[^:]+:[^:]+:([A-Za-z0-9]+).*/\1/'
203+
}
204+
173205
get_p1_count() {
174206
p1_count=$(grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1)
175207
echo "${p1_count:-0}"
@@ -219,26 +251,30 @@ check_blocking_rules() {
219251

220252
while IFS=: read -r file line rest; do
221253
[ -z "$file" ] && continue
254+
255+
# Trata file-based violations (formato com ||)
222256
if echo "$file" | grep -q '||'; then
223257
file=$(echo "$file" | cut -d'|' -f1)
224258
line=""
225259
fi
260+
226261
file_matches_patterns "$file" "$allowed_patterns" || continue
227262

228-
priority_marker=$(echo "$rest" | grep -o '\[P[0-9]\]' | head -1)
263+
# Extrai o nome da regra e busca a priority no XML
264+
rule_name=$(extract_rule_name "$file:$line:$rest")
265+
priority=$(get_rule_priority "$rule_name")
229266

230-
if [ -n "$priority_marker" ]; then
231-
[ "$priority_marker" != "[P1]" ] && continue
232-
fi
267+
[ "$priority" != "1" ] && continue
233268

269+
# Verifica se é file-based ou line-based
234270
if [ -z "$line" ] || [ "$line" = "null" ]; then
235271
if is_file_changed "$file"; then
236-
echo "📍 Violação P1 file-based em arquivo alterado: $file"
272+
echo "📍 Violação P1 ($rule_name) file-based em arquivo alterado: $file"
237273
echo "1" > "$VIOLATIONS_FLAG"
238274
break
239275
fi
240276
elif is_line_changed "$line" "$file"; then
241-
echo "📍 Violação P1 em linha alterada: $file:$line"
277+
echo "📍 Violação P1 ($rule_name) em linha alterada: $file:$line"
242278
echo "1" > "$VIOLATIONS_FLAG"
243279
break
244280
fi

0 commit comments

Comments
 (0)