|
| 1 | +name: Conventional Commits Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + push: |
| 7 | + branches: [master] |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-commits: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Validate commit messages |
| 20 | + run: | |
| 21 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 22 | + BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| 23 | + HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| 24 | + COMMITS=$(git log --oneline $BASE_SHA..$HEAD_SHA) |
| 25 | + else |
| 26 | + COMMITS=$(git log --oneline -1) |
| 27 | + fi |
| 28 | + |
| 29 | + FAILED_COMMITS=() |
| 30 | + while IFS= read -r commit_line; do |
| 31 | + if [ -n "$commit_line" ]; then |
| 32 | + COMMIT_MSG=$(echo "$commit_line" | cut -d' ' -f2-) |
| 33 | + if ! echo "$COMMIT_MSG" | grep -qE "^(feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert)(\(.+\))?: .+"; then |
| 34 | + FAILED_COMMITS+=("$commit_line") |
| 35 | + fi |
| 36 | + fi |
| 37 | + done <<< "$COMMITS" |
| 38 | + |
| 39 | + if [ ${#FAILED_COMMITS[@]} -gt 0 ]; then |
| 40 | + echo "Conventional commit check failed!" |
| 41 | + echo "Invalid commits:" |
| 42 | + for commit in "${FAILED_COMMITS[@]}"; do |
| 43 | + echo " - $commit" |
| 44 | + done |
| 45 | + echo "" |
| 46 | + echo "Required format: type(scope): description" |
| 47 | + echo "Valid types: feat, fix, docs, style, refactor, perf, test, chore, ci, build, revert" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Comment on PR |
| 52 | + if: github.event_name == 'pull_request' && failure() |
| 53 | + uses: actions/github-script@v7 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const { data: comments } = await github.rest.issues.listComments({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + issue_number: context.issue.number, |
| 60 | + }); |
| 61 | + |
| 62 | + const botComment = comments.find(comment => |
| 63 | + comment.user.type === 'Bot' && |
| 64 | + comment.body.includes('Conventional Commit Check Failed') |
| 65 | + ); |
| 66 | + |
| 67 | + const commentBody = `## Conventional Commit Check Failed |
| 68 | + |
| 69 | + Some commits in this PR do not follow the [conventional commit](https://www.conventionalcommits.org/) format. |
| 70 | + |
| 71 | + **Required format:** \`type(scope): description\` |
| 72 | + |
| 73 | + **Valid types:** |
| 74 | + - \`feat\`: A new feature |
| 75 | + - \`fix\`: A bug fix |
| 76 | + - \`docs\`: Documentation only changes |
| 77 | + - \`style\`: Changes that do not affect the meaning of the code |
| 78 | + - \`refactor\`: A code change that neither fixes a bug nor adds a feature |
| 79 | + - \`perf\`: A code change that improves performance |
| 80 | + - \`test\`: Adding missing tests or correcting existing tests |
| 81 | + - \`chore\`: Changes to the build process or auxiliary tools |
| 82 | + - \`ci\`: Changes to CI configuration files and scripts |
| 83 | + - \`build\`: Changes that affect the build system or external dependencies |
| 84 | + - \`revert\`: Reverts a previous commit |
| 85 | + |
| 86 | + **Examples:** |
| 87 | + - \`feat: add ESP32-S3 target support\` |
| 88 | + - \`fix: resolve serial monitor connection issue\` |
| 89 | + - \`docs: update installation guide for macOS\` |
| 90 | + - \`chore: update ESP-IDF version to 5.3\` |
| 91 | + - \`feat(debug): add JTAG debugging support\` |
| 92 | + - \`fix(flash): resolve flashing timeout on Windows\` |
| 93 | + - \`test: add unit tests for toolchain manager\` |
| 94 | + - \`ci: update GitHub Actions runners\` |
| 95 | + - \`build: update Maven dependencies\` |
| 96 | + - \`refactor(ui): simplify project creation wizard\` |
| 97 | + |
| 98 | + Please update your commit messages to follow this format.`; |
| 99 | + |
| 100 | + if (botComment) { |
| 101 | + await github.rest.issues.updateComment({ |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + comment_id: botComment.id, |
| 105 | + body: commentBody |
| 106 | + }); |
| 107 | + } else { |
| 108 | + await github.rest.issues.createComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + issue_number: context.issue.number, |
| 112 | + body: commentBody |
| 113 | + }); |
| 114 | + } |
0 commit comments