Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 61 additions & 38 deletions entrypoint.sh
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

${INPUT_REVIEWDOG_FLAGS} || true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

${INPUT_REVIEWDOG_FLAGS} || true

Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#!/bin/sh
set -e

# ========== ARQUIVOS TEMPORÁRIOS ==========
CODENARC_RESULT="result.txt"
LINE_VIOLATIONS="line_violations.txt"
FILE_VIOLATIONS="file_violations.txt"
VIOLATIONS_FLAG="/tmp/found_violations.txt"
ALL_DIFF="/tmp/all_diff.txt"
CHANGED_LINES_CACHE="/tmp/changed_lines.txt"
CHANGED_FILES_CACHE="/tmp/changed_files.txt"
TMP_VIOLATIONS="/tmp/violations.tmp"

cleanup_temp_files() {
rm -f "$CODENARC_RESULT" "$LINE_VIOLATIONS" "$FILE_VIOLATIONS" "$VIOLATIONS_FLAG" \
"$ALL_DIFF" "$CHANGED_LINES_CACHE" "$CHANGED_FILES_CACHE" \
"${FILE_VIOLATIONS}.formatted" >/dev/null 2>&1
"${FILE_VIOLATIONS}.formatted" "$TMP_VIOLATIONS" >/dev/null 2>&1
}

trap 'cleanup_temp_files' EXIT

# ========== ETAPA 1 - EXECUTA CODENARC ==========
run_codenarc() {
report="${INPUT_REPORT:-compact:stdout}"
includes_arg=""

[ -n "$INPUT_SOURCE_FILES" ] && includes_arg="-includes=${INPUT_SOURCE_FILES}"

echo "🔍 Executando CodeNarc..."
java -jar /lib/codenarc-all.jar \
-report="$report" \
Expand All @@ -32,14 +34,15 @@ run_codenarc() {
> "$CODENARC_RESULT"
}

# ========== ETAPA 2 - REVIEWDOG ==========
run_reviewdog_with_config() {
input_file="$1"
efm="$2"
reporter="$3"
name="$4"
filter_mode="$5"
level="$6"

< "$input_file" reviewdog \
-efm="$efm" \
-reporter="$reporter" \
Expand All @@ -57,16 +60,16 @@ separate_violations() {

run_reviewdog() {
echo "📤 Enviando resultados para reviewdog..."

separate_violations

if [ -s "$LINE_VIOLATIONS" ]; then
echo "📤 Enviando violações line-based (${INPUT_REPORTER:-github-pr-check})..."
run_reviewdog_with_config "$LINE_VIOLATIONS" "%f:%l:%m" \
"${INPUT_REPORTER:-github-pr-check}" "codenarc" \
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
fi

if [ -s "$FILE_VIOLATIONS" ]; then
true > "${FILE_VIOLATIONS}.formatted"
while read -r violation; do
Expand All @@ -76,33 +79,38 @@ run_reviewdog() {
echo "$violation" | sed 's/:null:/::/'
fi
done < "$FILE_VIOLATIONS" > "${FILE_VIOLATIONS}.formatted"

if [ "${INPUT_REPORTER}" = "local" ]; then
echo "📤 Enviando violações file-based (local)..."
run_reviewdog_with_config "${FILE_VIOLATIONS}.formatted" "%f::%m" \
"local" "codenarc" "nofilter" "${INPUT_LEVEL}"
else
echo "📤 Enviando violações file-based (github-pr-check)..."
run_reviewdog_with_config "${FILE_VIOLATIONS}.formatted" "%f::%m" \
"github-pr-check" "codenarc" "nofilter" "warning"
fi
fi

# fallback se nao houver violacoes categorizadas

if [ ! -s "$LINE_VIOLATIONS" ] && [ ! -s "$FILE_VIOLATIONS" ]; then
echo "📝 Executando reviewdog padrão..."
run_reviewdog_with_config "$CODENARC_RESULT" "%f:%l:%m" \
"${INPUT_REPORTER:-github-pr-check}" "codenarc" \
"${INPUT_FILTER_MODE}" "${INPUT_LEVEL}"
fi
}

generate_git_diff() {
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "⚠️ Diretório não é um repositório Git; nenhuma comparação de diff será feita."
return 0
fi

if [ -n "$GITHUB_BASE_SHA" ] && [ -n "$GITHUB_HEAD_SHA" ]; then
git fetch origin "$GITHUB_BASE_SHA" --depth=1 2>/dev/null || true
git fetch origin "$GITHUB_HEAD_SHA" --depth=1 2>/dev/null || true
git diff -U0 "$GITHUB_BASE_SHA" "$GITHUB_HEAD_SHA" -- '*.groovy'
else
if ! git rev-parse HEAD~1 >/dev/null 2>&1; then
echo "⚠️ Nenhum commit anterior para comparar; diff vazio."
return 0
fi
git diff -U0 HEAD~1 -- '*.groovy'
fi
}
Expand All @@ -119,10 +127,13 @@ parse_diff_range() {
build_changed_lines_cache() {
true > "$CHANGED_LINES_CACHE"
true > "$CHANGED_FILES_CACHE"

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

[ ! -s "$ALL_DIFF" ] && {
echo "ℹ️ Nenhum diff detectado; prosseguindo com cache vazio."
return 0
}

current_file=""
while read -r line; do
case "$line" in
Expand Down Expand Up @@ -150,6 +161,7 @@ build_changed_lines_cache() {
done < "$ALL_DIFF"
}

# ========== FUNÇÕES AUXILIARES ==========
get_p1_count() {
p1_count=$(grep -Eo "p1=[0-9]+" "$CODENARC_RESULT" | cut -d'=' -f2 | head -1)
echo "${p1_count:-0}"
Expand Down Expand Up @@ -179,52 +191,63 @@ is_file_changed() {
grep -q "^$1$" "$CHANGED_FILES_CACHE"
}

# ========== ETAPA 4 - BLOQUEIO POR P1 ==========
check_blocking_rules() {
echo "🔎 Verificando violações bloqueantes (priority 1)..."

[ ! -f "$CODENARC_RESULT" ] && echo "❌ Resultado não encontrado" && return 1

p1_count=$(get_p1_count)
echo "📊 Total de P1 encontradas: $p1_count"

[ "$p1_count" -eq 0 ] && echo "✅ Nenhuma P1 detectada → merge permitido" && return 0

echo "⚠️ Verificando P1s em linhas alteradas..."
echo "📊 Total de P1 encontradas no resumo: ${p1_count:-0}"
[ "$p1_count" -eq 0 ] && { echo "✅ Nenhuma P1 detectada → merge permitido"; return 0; }

echo "⚙️ Preparando diff..."
build_changed_lines_cache

allowed_patterns=$(get_allowed_patterns)
[ -n "$allowed_patterns" ] && echo "🧩 Analisando apenas arquivos filtrados por INPUT_SOURCE_FILES"
[ -n "$allowed_patterns" ] && echo "🧩 Filtrando por INPUT_SOURCE_FILES"

echo "0" > "$VIOLATIONS_FLAG"

grep -E ':[0-9]+:|:null:|\|\|' "$CODENARC_RESULT" | while IFS=: read -r file line rest; do
grep -E ':[0-9]+:|:null:|\|\|' "$CODENARC_RESULT" > "$TMP_VIOLATIONS" || true

while IFS=: read -r file line rest; do
[ -z "$file" ] && continue
if echo "$file" | grep -q '||'; then
file=$(echo "$file" | cut -d'|' -f1)
line=""
fi
[ -z "$file" ] && continue
file_matches_patterns "$file" "$allowed_patterns" || continue

priority_marker=$(echo "$rest" | grep -o '\[P[0-9]\]' | head -1)

if [ -n "$priority_marker" ]; then
[ "$priority_marker" != "[P1]" ] && continue
fi

if [ -z "$line" ] || [ "$line" = "null" ]; then
if is_file_changed "$file"; then
echo "📍 Violação file-based em arquivo alterado: $file"
echo "1" > "$VIOLATIONS_FLAG" && break
echo "📍 Violação P1 file-based em arquivo alterado: $file"
echo "1" > "$VIOLATIONS_FLAG"
break
fi
elif is_line_changed "$line" "$file"; then
echo "📍 Violação em linha alterada: $file:$line"
echo "1" > "$VIOLATIONS_FLAG" && break
echo "📍 Violação P1 em linha alterada: $file:$line"
echo "1" > "$VIOLATIONS_FLAG"
break
fi
done

done < "$TMP_VIOLATIONS"

rm -f "$TMP_VIOLATIONS"

if [ "$(cat "$VIOLATIONS_FLAG")" -eq 1 ]; then
echo "⛔ P1s existem E há violações em linhas alteradas"
echo "💡 Corrija as violacoes ou use o bypass autorizado pelo coordenador."
echo "⛔ P1 encontrada em linha/arquivo alterado"
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'P1 encontrada' uses singular form, but there could be multiple P1 violations. Consider using plural 'P1s encontradas' or 'Violação(ões) P1 encontrada(s)' for accuracy.

Suggested change
echo "⛔ P1 encontrada em linha/arquivo alterado"
echo "Violação(ões) P1 encontrada(s) em linha/arquivo alterado(s)"

Copilot uses AI. Check for mistakes.
echo "💡 Corrija as violações ou utilize o bypass autorizado."
exit 1
else
echo "✅ P1s existem mas fora das linhas alteradas → merge permitido"
echo "✅ Existência de P1 fora das linhas alteradas → merge permitido"
fi
}

# ========== EXECUÇÃO PRINCIPAL ==========
if [ -n "${GITHUB_WORKSPACE}" ]; then
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
git config --global --add safe.directory "$GITHUB_WORKSPACE"
Expand Down
Loading
Loading