|
| 1 | +# This workflow ensures that all commit messages in a pull request adhere to the Conventional Commits specification. |
| 2 | + |
| 3 | +name: Commit Message Validation |
| 4 | + |
| 5 | +on: |
| 6 | + # Allows you to run this workflow manually from the Actions tab |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | + # Triggers the workflow on pull request events targeting the main branch |
| 10 | + pull_request: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + types: |
| 14 | + - opened |
| 15 | + - synchronize |
| 16 | + - reopened |
| 17 | + |
| 18 | +jobs: |
| 19 | + validate-commits: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v3 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Validate commit messages |
| 29 | + run: | |
| 30 | + # Regex for Conventional Commits specification |
| 31 | + COMMIT_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([^\)]*\))?:\s?(EXPOSED-[0-9]+\s?)?.+$" |
| 32 | + |
| 33 | + # Get all commits in the pull request (from base to head) |
| 34 | + COMMITS=$(git log --format=%s --no-merges origin/main..${{ github.sha }}) |
| 35 | +
|
| 36 | + # Initialize counters and store invalid commits |
| 37 | + INVALID_COMMITS=() |
| 38 | + VALID_COMMITS=() |
| 39 | + |
| 40 | + echo "ℹ️ Checking if commit messages are following the Conventional Commits specification..." |
| 41 | +
|
| 42 | + # Loop through each commit message |
| 43 | + IFS=$'\n' |
| 44 | + for COMMIT_MSG in $COMMITS; do |
| 45 | + # Check if commit message matches the regex |
| 46 | + if [[ ! "$COMMIT_MSG" =~ $COMMIT_REGEX ]]; then |
| 47 | + INVALID_COMMITS+=("$COMMIT_MSG") |
| 48 | + echo -e "❌ $COMMIT_MSG" |
| 49 | + else |
| 50 | + VALID_COMMITS+=("$COMMIT_MSG") |
| 51 | + echo -e "✅ $COMMIT_MSG" |
| 52 | + fi |
| 53 | + done |
| 54 | +
|
| 55 | + # If there are invalid commits, print the summary |
| 56 | + if [ ${#INVALID_COMMITS[@]} -gt 0 ]; then |
| 57 | + echo "" |
| 58 | + echo "🛑 Some commit messages are not following the Conventional Commits specification." |
| 59 | + echo "" |
| 60 | + echo "Valid commit message format: <type>(<optional scope>): <subject>" |
| 61 | + echo "Example: fix: Bug in insert" |
| 62 | + echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +
|
| 66 | + echo "🎉 All commit messages are following the Conventional Commits specification." |
0 commit comments