Skip to content
Merged
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
67 changes: 62 additions & 5 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Precommit Hook
name: Conventional Commits Check

on:
pull_request:
Expand All @@ -10,15 +10,72 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install pre-commit
run: pip install pre-commit

- name: Run pre-commit hook
run: pre-commit run --all-files
- name: Validate commit messages
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
COMMITS=$(git log --oneline $BASE_SHA..$HEAD_SHA)
else
COMMITS=$(git log --oneline -1)
fi
Comment on lines +27 to +33
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Ensure commit collection fails fast when the range is missing.

If git log cannot resolve ${{ github.event.pull_request.head.sha }} (common for forked PRs unless we fetch the head ref), the command exits 128 but still assigns an empty string to COMMITS, so the loop never runs and every commit slips past validation. Please fail fast when the log command errors (and likewise in the non-PR branch) so we never treat “couldn’t fetch commits” as “all commits valid”.

-          COMMITS=$(git log --oneline $BASE_SHA..$HEAD_SHA)
+          if ! COMMITS=$(git log --oneline "${BASE_SHA}..${HEAD_SHA}"); then
+            echo "Failed to list commits between ${BASE_SHA} and ${HEAD_SHA}. Did we fetch the head ref?" >&2
+            exit 1
+          fi
@@
-          COMMITS=$(git log --oneline -1)
+          if ! COMMITS=$(git log --oneline -1); then
+            echo "Failed to read the latest commit." >&2
+            exit 1
+          fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
COMMITS=$(git log --oneline $BASE_SHA..$HEAD_SHA)
else
COMMITS=$(git log --oneline -1)
fi
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
if ! COMMITS=$(git log --oneline "${BASE_SHA}..${HEAD_SHA}"); then
echo "Failed to list commits between ${BASE_SHA} and ${HEAD_SHA}. Did we fetch the head ref?" >&2
exit 1
fi
else
if ! COMMITS=$(git log --oneline -1); then
echo "Failed to read the latest commit." >&2
exit 1
fi
fi
🤖 Prompt for AI Agents
.github/workflows/pre-commit.yml around lines 27 to 33: the git log commands
that populate COMMITS can fail (exit 128) and leave COMMITS empty which causes
the script to silently accept no commits; change the logic to fail fast when git
log errors by checking the git log exit status and exiting the workflow with a
non-zero code if it fails (or use a shell option like set -e), e.g., run git log
into a temporary variable and if the command returns non-zero, echo an error and
exit 1, otherwise assign COMMITS to that output; apply the same check in both
the pull_request and non-PR branches.


FAILED_COMMITS=()
while IFS= read -r commit_line; do
if [ -n "$commit_line" ]; then
COMMIT_MSG=$(echo "$commit_line" | cut -d' ' -f2-)
COMMIT_HASH=$(echo "$commit_line" | cut -d' ' -f1)

# Create temporary commit message file for pre-commit validation
echo "$COMMIT_MSG" > /tmp/commit_msg

# Run the conventional commit linter
if ! pre-commit run conventional-precommit-linter --hook-stage commit-msg --commit-msg-filename /tmp/commit_msg; then
FAILED_COMMITS+=("$commit_line")
fi

rm -f /tmp/commit_msg
fi
done <<< "$COMMITS"

if [ ${#FAILED_COMMITS[@]} -gt 0 ]; then
echo "Conventional commit check failed!"
echo "Invalid commits:"
for commit in "${FAILED_COMMITS[@]}"; do
echo " - $commit"
done
echo ""
echo "Required format: type(scope): description"
echo ""
echo "Valid types: feat, fix, docs, style, refactor, perf, test, chore, ci, build, revert"
echo ""
echo "Examples for ESP-IDF Eclipse Plugin:"
echo " feat: add ESP32-S3 target support"
echo " fix: resolve serial monitor connection issue"
echo " docs: update installation guide for macOS"
echo " chore: update ESP-IDF version to 5.3"
echo " feat(debug): add JTAG debugging support"
echo " fix(flash): resolve flashing timeout on Windows"
echo " test: add unit tests for toolchain manager"
echo " ci: update GitHub Actions runners"
echo " build: update Maven dependencies"
echo " refactor(ui): simplify project creation wizard"
echo ""
echo "To fix commits, you can:"
echo " 1. Use 'git commit --amend' for the last commit"
echo " 2. Use 'git rebase -i HEAD~n' to edit multiple commits"
echo " 3. Use 'git reset --soft HEAD~n' and recommit"
exit 1
fi
Loading