Skip to content

GitHub Actions - Automate npm package updates and release notes #7

GitHub Actions - Automate npm package updates and release notes

GitHub Actions - Automate npm package updates and release notes #7

name: Check for Conventional Commits
on: pull_request
jobs:
check_commits:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to get all commits in the PR
- name: Get PR commits
id: get_commits
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_OUTPUT: ${{ github.output }}
run: |
PR_NUMBER="${{ github.event.number }}"
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
if [[ -z "$PR_NUMBER" ]]; then
PR_COMMITS=$(git log --pretty=format:'%s' $BASE_SHA...$HEAD_SHA)
else
PR_COMMITS=$(gh api -X GET /repos/${{ github.repository }}/pulls/$PR_NUMBER/commits --jq '.[].commit.message')
fi
echo '<<EOF' >> $GITHUB_OUTPUT
echo "PR_COMMITS=$PR_COMMITS" >> "$GITHUB_OUTPUT"
echo 'EOF' >> $GITHUB_OUTPUT
- name: Check for conventional commits
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_COMMITS: ${{ steps.get_commits.outputs.PR_COMMITS }}
run: |
IFS=$'\n' read -r -d '' -a commit_array <<< "$PR_COMMITS"
has_conventional_commit=false
for commit_message in "${commit_array[@]}"; do
if [[ "$commit_message" =~ ^(feat|fix|build|chore|ci|docs|perf|refactor|revert|style|test)(\([^)]+\))?: .* ]]; then
has_conventional_commit=true
break
fi
done
if [[ "$has_conventional_commit" == "false" ]]; then
echo "::warning::Pull request does not contain at least one conventional commit. A new package version won't be created for this pull request."
exit 1
else
echo "::notice::Pull request contains at least one conventional commit. A new package version will be created for this pull request."
fi