Skip to content

Commit

Permalink
feat: Add cspell and commitlint to pre-commit and ci (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt authored Dec 30, 2024
1 parent 255cb04 commit a95a41d
Show file tree
Hide file tree
Showing 8 changed files with 1,582 additions and 247 deletions.
10 changes: 10 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.2",
"language": "en",
"words": [
"githubusercontent",
"commitlint",
"EDITMSG",
"codespell"
]
}
29 changes: 29 additions & 0 deletions .github/hooks/todo-warning.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

ORANGE='\033[0;33m'
NC='\033[0m'
BOLD='\033[1m'
NORMAL='\033[0m'


echo $GIT_COMMIT

check_file() {
local file=$1
local match_pattern=$2

local file_changes_with_context=$(git diff -U999999999 -p --cached --color=always -- $file)
local matched_additions=$(echo "$file_changes_with_context" | grep -C4 $'^\e\\[32m\+.*'"$match_pattern")

if [ -n "$matched_additions" ]; then
echo -e "${ORANGE}[WARNING]${NC} ${BOLD}$file${NORMAL} contains some $match_pattern."
echo "$matched_additions"
echo -e "\n"
fi
}


for file in `git diff --cached -p --name-status | cut -c3-`; do
check_file $file 'TODO'
done
exit
72 changes: 72 additions & 0 deletions .github/workflows/validate-pr-title.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Validate PR Title
on:
pull_request:
types: [opened, edited, synchronize]
permissions:
pull-requests: write
contents: read
jobs:
lint:
name: 'Lint'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
- name: Install Commitlint and CSpell
run: npm install --save-dev @commitlint/{config-conventional,cli} cspell
- run: echo "${{ github.event.pull_request.title }}" > pr-title.txt
- name: Run Commitlint
id: commitlint
run: npx commitlint --edit pr-title.txt > commitlint_output.txt 2>&1
if: always()
- name: Run CSpell
id: cspell
run: npx cspell --config .cspell.json pr-title.txt > cspell_output.txt 2>&1
if: always()
- name: Delete Old Bot Comments
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Fetch all comments on the PR
COMMENTS=$(gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments --jq '.[] | select(.user.login == "github-actions[bot]") | .id')
# Delete comments authored by the bot
for COMMENT_ID in $COMMENTS; do
gh api repos/${{ github.repository }}/issues/comments/$COMMENT_ID -X DELETE
done
- name: Post PR Comment
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Initialize status messages
STATUS_COMMITLINT="PASSED"
STATUS_CSPELL="PASSED"
COMMITLINT_OUTPUT=$(cat commitlint_output.txt)
CSPELL_OUTPUT=$(cat cspell_output.txt)
COMMENT_BODY="### Found Issues In PR Title\n"
if [ "${{ steps.commitlint.outcome }}" == "failure" ]; then
STATUS_COMMITLINT="FAILED"
fi
if [ "${{ steps.cspell.outcome }}" == "failure" ]; then
STATUS_CSPELL="FAILED"
fi
if [ "$STATUS_COMMITLINT" == "FAILED" ]; then
COMMENT_BODY+="**❌ Conventional Commit Format**\n"
COMMENT_BODY+="\n\`\`\`\n$COMMITLINT_OUTPUT\n\`\`\`\n"
fi
if [ "$STATUS_CSPELL" == "FAILED" ]; then
COMMENT_BODY+="**❌ Spelling Error**\n"
COMMENT_BODY+="\n\`\`\`\n$CSPELL_OUTPUT\n\`\`\`\n"
COMMENT_BODY+="\n> If you believe the spelling error is a false positive, please add the word in **cspell.json** file.\n"
fi
if [ "$STATUS_COMMITLINT" == "FAILED" ] || [ "$STATUS_CSPELL" == "FAILED" ]; then
# Post the comment
echo -e "$COMMENT_BODY" | gh pr comment ${{ github.event.pull_request.number }} --body-file -
fi
32 changes: 30 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,38 @@ repos:
- id: check-toml
- id: check-yaml


- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.5
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- id: ruff-format

- repo: local
hooks:
- id: commitlint
name: check commit message format
entry: npx commitlint --edit .git/COMMIT_EDITMSG
language: system
stages: [commit-msg]
always_run: true

- id: cspell-commit-msg
name: check commit message spelling
entry: npx cspell --config .cspell.json .git/COMMIT_EDITMSG
language: system
stages: [commit-msg]
always_run: true

- id: cspell-modified-files
name: check spelling of files
entry: sh -c "npx cspell --config .cspell.json `git diff --cached -p --name-status | cut -c3- | tr '\n' ' '`"
language: system
stages: [pre-commit]

- id: todo-warning
name: check todos
entry: .github/hooks/todo-warning.sh
language: script
stages: [pre-commit]
verbose: true
28 changes: 7 additions & 21 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
module.exports = {
parserPreset: 'conventional-changelog-conventionalcommits',
extends: ['@commitlint/config-conventional'],
rules: {
'subject-empty': [2, 'never'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
],
],
'header-max-length': [2, 'always', 72],
'subject-case': [2, 'always', 'sentence-case'],
'scope-case': [2, 'always', 'kebab-case'],
'body-case': [2, 'always', 'sentence-case'],
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [2, 'always'],
},
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"@tailwindcss/typography": "^0.5.1",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.6",
"tailwindcss": "^3.2"
"tailwindcss": "^3.2",
"@commitlint/config-conventional": "^19.6.0",
"commitlint": "^19.6.1",
"cspell": "^8.17.1"
},
"dependencies": {
"dayjs": "^1.11.10",
Expand Down
15 changes: 15 additions & 0 deletions setup-pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# !/usr/bin/env bash

echo "Installing python dev dependencies"
pip install -r dev-requirements.txt

echo "Installing nodejs dev dependencies"
yarn install --frozen-lockfile --dev

echo "Setting up pre-commit hooks"
pre-commit install

echo "Setting up commit-msg hooks"
pre-commit install -t commit-msg

echo "Setup complete"
Loading

0 comments on commit a95a41d

Please sign in to comment.