diff --git a/.github/workflows/conventional-commits-checker.yaml b/.github/workflows/conventional-commits-checker.yaml new file mode 100644 index 0000000..387c336 --- /dev/null +++ b/.github/workflows/conventional-commits-checker.yaml @@ -0,0 +1,31 @@ +name: "Commit Lint" +on: + pull_request: + branches: + - main + - development + - staging + types: + - opened + - reopened + - labeled + - unlabeled + - edited + - synchronize +jobs: + custom_test: + runs-on: ubuntu-latest + name: We test it locally with act + steps: + - uses: actions/checkout@v3 + with: + token: ${{ secrets.PAT }} + fetch-depth: 0 + - name: Conventional Commits Checker + uses: ./ # Uses an action in the root directory + id: commits-check + with: + target-branch: ${{ github.event.pull_request.base.ref }} + current-branch: ${{ github.event.pull_request.head.ref }} + - name: Get the Result + run: echo "${{ steps.commits-check.outputs.commit-checker }}" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e372598 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# Base image +FROM alpine:latest + +RUN apk add --no-cache \ + bash \ + ca-certificates \ + curl \ + git + +COPY entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..6f8faee --- /dev/null +++ b/action.yaml @@ -0,0 +1,21 @@ +# action.yaml +name: 'Conventional Commits Checker' +description: 'Check that all pull request commits are following conventional commits' +inputs: + target-branch: + description: 'target branch' + required: true + default: 'main' + current-branch: + description: 'current-branch' + required: true + default: 'main' +outputs: + commit-checker: + description: 'true or false' +runs: + using: docker + image: Dockerfile + args: + - ${{ inputs.target-branch }} + - ${{ inputs.current-branch }} diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..98a5fed --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,47 @@ +#!/bin/bash +set -e + +git config --global --add safe.directory /github/workspace + +# Define the branch names +main_branch=main=$1 + +echo $1 +echo $2 + +# Get all commit messages from the feature branch that are not in the main branch +commits=($(git log --pretty="%s EON" origin/$2 ^origin/$1)) + +pattern="(feat|fix|ci|chore|docs|test|style|refactor): +#([0-9]+) -.{1,}$" + +# Loop through the array and create other structure data +commits_struct=() +for commit in "${commits[@]}"; do + if [ $commit != "EON" ]; then + commit_msg+=" $commit" + fi + if [ $commit == "EON" ]; then + commits_struct+=("${commit_msg}") + commit_msg="" + fi +done + +all_commits_ok="true" +echo "| commit | status" +for msg in "${commits_struct[@]}"; do + if [[ $msg =~ $pattern ]]; then + echo "$msg | ok" + else + echo "$msg | invalid" + all_commits_ok="false" + fi + echo "--" +done + +if [ $all_commits_ok == "true" ]; then + echo "commit-checker=true" >>$GITHUB_OUTPUT + exit 0 +else + echo "commit-checker=false" >>$GITHUB_OUTPUT + exit 1 +fi