From 5c0e032ac8a02adeefa16d6483704e23b82a6891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Mikul=C3=A1=C5=A1ek?= Date: Mon, 11 Nov 2024 13:49:18 +0100 Subject: [PATCH] chore(commit-lint): introduce commit lint written in bash JIRA: STL-894 --- .github/workflows/commit-lint-shell.yaml | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/commit-lint-shell.yaml diff --git a/.github/workflows/commit-lint-shell.yaml b/.github/workflows/commit-lint-shell.yaml new file mode 100644 index 0000000..3266320 --- /dev/null +++ b/.github/workflows/commit-lint-shell.yaml @@ -0,0 +1,64 @@ +--- +name: Lint Commit Messages (in shell) + +on: + pull_request: + merge_group: + +jobs: + commit-lint: + runs-on: + group: infra1-runners-arc + labels: runners-small + permissions: + contents: read + pull-requests: read + steps: + - uses: actions/checkout@v4 + - shell: bash + run: | + # Define conventional commit regex with optional breaking change mark + CONVENTIONAL_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.+\))?!?: .{1,70}" + + # Get commit subjects and hashes + COMMITS=$(git log --reverse --pretty=format:"%h %s" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}) + + # Initialize violation flag and message + VIOLATIONS="" + + # Check each commit subject + while IFS= read -r COMMIT; do + COMMIT_HASH=$(echo "$COMMIT" | awk '{print $1}') + COMMIT_SUBJECT=$(echo "$COMMIT" | cut -d' ' -f2-) + COMMIT_VIOLATIONS="" + + if ! [[ "$COMMIT_SUBJECT" =~ $CONVENTIONAL_REGEX ]]; then + COMMIT_VIOLATIONS+=":x: does not follow Conventional Commits guidelines\n" + fi + if [[ ! "$COMMIT_SUBJECT" =~ ^chore\(deps\): ]] && [[ ${#COMMIT_SUBJECT} -gt 70 ]]; then + COMMIT_VIOLATIONS+=":x: exceeds 70 characters\n" + fi + if [[ ! "$COMMIT_SUBJECT" =~ ^chore\(update\): ]]; then + TRAILERS=$(git show -s --format=%B "$COMMIT_HASH" | git interpret-trailers --parse) + if ! echo "$TRAILERS" | grep -iqE '^risk:\s*(nonprod|low|high)'; then + COMMIT_VIOLATIONS+=":x: does not contain a valid risk trailer\n" + fi + fi + + if [[ -n "$COMMIT_VIOLATIONS" ]]; then + VIOLATIONS+="\`$COMMIT_HASH $COMMIT_SUBJECT\`\n$COMMIT_VIOLATIONS\n" + fi + done <<< "$COMMITS" + + # Output violations if any + if [[ -n "$VIOLATIONS" ]]; then + echo -e "$VIOLATIONS" + echo "## Commit Lint Results" >> $GITHUB_STEP_SUMMARY + echo "### Violations" >> $GITHUB_STEP_SUMMARY + echo -e "$VIOLATIONS" >> $GITHUB_STEP_SUMMARY + exit 1 + fi + + echo "All commit messages follow Conventional Commits guidelines." + echo "## Commit Lint Results" >> $GITHUB_STEP_SUMMARY + echo ":white_check_mark: All commit messages follow Conventional Commits guidelines." >> $GITHUB_STEP_SUMMARY