Skip to content

Commit e0ba630

Browse files
authored
feat: add lint-changed.sh shell script
1 parent d040710 commit e0ba630

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

.github/scripts/lint-changed.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit immediately if a command fails, or if an unset variable is used
4+
set -euo pipefail
5+
6+
EVENT_NAME="${1:-local}"
7+
BASE_REF="${2:-main}"
8+
BEFORE_SHA="${3:-}"
9+
10+
echo "Configuring target diff for event: $EVENT_NAME"
11+
12+
# Determine the base branch/commit to diff against
13+
if [ "$EVENT_NAME" = "pull_request" ]; then
14+
# Ensure we have the target branch metadata fetched.
15+
# Note: If this fails due to shallow clone issues, the script will exit safely via set -e
16+
git fetch origin "$BASE_REF" --depth=1 --quiet
17+
BASE_SHA="origin/$BASE_REF"
18+
else
19+
BASE_SHA="$BEFORE_SHA"
20+
# Fallback if it's a direct push without a prior SHA, or a local run
21+
if [ "$BASE_SHA" = "0000000000000000000000000000000000000000" ] || [ -z "$BASE_SHA" ]; then
22+
BASE_SHA="HEAD~1"
23+
fi
24+
fi
25+
26+
# Extract changed Python files into a Bash array
27+
mapfile -t CHANGED_FILES < <(git diff --name-only --diff-filter=d "$BASE_SHA" -- '*.py' 2>/dev/null || true)
28+
29+
# Execute linters if files exist
30+
if [ ${#CHANGED_FILES[@]} -gt 0 ]; then
31+
echo "Files to lint:"
32+
printf ' - %s\n' "${CHANGED_FILES[@]}"
33+
34+
# Track execution success manually so both tools get a chance to run
35+
BLACK_EXIT=0
36+
LINT_EXIT=0
37+
38+
# Pass the array safely using "${CHANGED_FILES[@]}"
39+
nox -s blacken -- "${CHANGED_FILES[@]}" || BLACK_EXIT=$?
40+
nox -s lint -- "${CHANGED_FILES[@]}" || LINT_EXIT=$?
41+
42+
if [ $BLACK_EXIT -ne 0 ] || [ $LINT_EXIT -ne 0 ]; then
43+
echo "❌ One or more linting checks failed."
44+
exit 1
45+
fi
46+
else
47+
echo "✅ No Python files changed in this scope. Skipping checks."

0 commit comments

Comments
 (0)