Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added commit summary restrictions #0 #36

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions .github/workflows/on-pr-open-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
delete_user_name: sonarcloud[bot]
issue_number: ${{ github.event.number }}

- name: Validate Commit Messages
if: ${{ github.event.number }}
run: ./tools/scripts/validate-commit-msgs.sh

- name: Install go
uses: actions/setup-go@v5
with:
Expand Down
55 changes: 55 additions & 0 deletions tools/scripts/validate-commit-msgs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

commitSummaryRegex="^(\+|\-)\s[a-z0-9]+\s(.+)$"
commitTypeRegex="^(chore|docs|feat|fix|perf|ref|revert|style|test|wip)"
commitScopeRegex="^[a-z]+(\((a-rule|a-schema|acl|app-scope|bot-cfg|bot-rule|bot|c-rule|cache|cdn|ci-cd|client|common|config|deployment|e-rule|env-var|env|internal|m-role|org|property|r-rule|tls|token|util)\))?\:\s"
issueNumberRegex="\s(\[#[0-9]+\]|\(#[0-9]+\))$"
defectiveCommitSummaries=()

while IFS= read -r line; do
[[ $line =~ $commitSummaryRegex ]]
commitSummary="${BASH_REMATCH[2]}"

if [ ${#commitSummary} -ge 72 ]; then
defectiveCommitSummaries+=("Commit summary \`$commitSummary\` is more than 72 characters long")
fi

if [[ !($commitSummary =~ $commitTypeRegex) ]]; then
defectiveCommitSummaries+=("Commit summary \`$commitSummary\` does not respect commit type restrictions")
fi

if [[ !($commitSummary =~ $commitScopeRegex) ]]; then
defectiveCommitSummaries+=("Commit summary \`$commitSummary\` does not respect commit scope restrictions")
fi

if [[ !($commitSummary =~ $issueNumberRegex) ]]; then
defectiveCommitSummaries+=("Commit summary \`$commitSummary\` does include an issue number")
fi
done <<< "$(git cherry -v origin/beta)"

echo "### Commit messages check result" >> $GITHUB_STEP_SUMMARY

if [ ${#defectiveCommitSummaries[@]} -eq 0 ]; then
echo "All commit messages are valid" >> $GITHUB_STEP_SUMMARY
exit 0
else
echo "There are ${#defectiveCommitSummaries[@]} uncompliant commit messages" >> $GITHUB_STEP_SUMMARY
echo "#### Checks performed" >> $GITHUB_STEP_SUMMARY

echo "- Commit summary size: 72 characters maximum" >> $GITHUB_STEP_SUMMARY
echo "- Commit type constraints: \`/$commitTypeRegex/\`" >> $GITHUB_STEP_SUMMARY
echo "- Commit scope constraints: \`/$commitScopeRegex/\`" >> $GITHUB_STEP_SUMMARY
echo "- Issue number presence: \`/$issueNumberRegex/\`" >> $GITHUB_STEP_SUMMARY

echo "#### Invalid commit messages" >> $GITHUB_STEP_SUMMARY

for invalid in "${defectiveCommitSummaries[@]}"; do
echo "- $invalid" >> $GITHUB_STEP_SUMMARY
done

echo "" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Please, rebase your branch fixing the problematic commit summaries and update your PR :)" >> $GITHUB_STEP_SUMMARY

exit 1
fi
Loading