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

static-file-name-checker #5

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
73 changes: 49 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name: Check Kebab Case for Markdown Files in PR Commits
name: MarkDown filename validation

on:
pull_request: # Trigger on PR events
pull_request:
paths:
- '**/*.md' # Trigger only when .md files are changed
- '**/*.md'
- '**/*.jpg'
- '**/*.png'
- '**/*.jpeg'
- '**/*.svg'

jobs:
kebab-case-check:
Expand All @@ -12,31 +16,52 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Get list of changed Markdown files
id: changed-files
- name: Check filenames for kebab case
run: |
# Get the list of added or modified .md files in the PR
git diff --name-only --diff-filter=AM origin/main | grep '\.md$' > changed_files.txt || true
if [ ! -s changed_files.txt ]; then
echo "No Markdown files to check."
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Reset='\033[0m' # Reset

echo -e "${Blue} Checking all changed Markdown files ..."

changed_files=$(git diff --name-only HEAD^1 HEAD | grep -E '\.(md|jpg|png|jpeg|svg)$')

if [ -z "$changed_files" ]; then
echo -e "${Green} No changed files in this PR.${Reset}"
exit 0
fi

- name: Check filenames for kebab case
run: |
# Find all .md files excluding node_modules, README.md, and CONTRIBUTING.md, and check filenames for kebab case
invalid_files=$(find . -type f -name '*.md' \
-not -path "./node_modules/*" \
-not -name "README.md" \
-not -name "CONTRIBUTING.md" \
-exec basename {} \; | grep -vE '^[a-z0-9]+(-[a-z0-9]+)*\.md$')

if [ -n "$invalid_files" ]; then
echo "The following Markdown filenames are not in kebab case:"
echo "$invalid_files"

x=1
echo -e "${Blue} Changed static files are :${Reset}"
echo "$changed_files" | while read -r file; do
dir=$(dirname "$file")
base=$(basename "$file")
parent_folder=$(basename "$dir")
if [ "$parent_folder" = "." ]; then
parent_folder="root"
fi
combined_info="$parent_folder/$base" # Treat folder name as part of the file name
echo -e "${Yellow} $x. $combined_info${Reset}"
x=$((x + 1))
done

# Find invalid filenames
invalid_kebab_case=$(echo "$changed_files" | grep -vE '^([a-z0-9]+(-[a-z0-9]+)*)(/([a-z0-9]+(-[a-z0-9]+)*))*\.(md|jpg|png|jpeg|svg)$' || true)

# Check for filenames that are not in kebab case
if [ -n "$invalid_kebab_case" ]; then
echo -e "${Blue} The following path is not in kebab case:${Reset}"
x=1
echo "$invalid_kebab_case" | while read -r file; do
echo -e "${Red}$x. $file${Reset}"
x=$((x + 1))
done
failure_flag=1
else
echo "All Markdown filenames are in kebab case."
echo -e "${Green} All changed filenames are in kebab case."
fi

Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ export default {
],
};
```

git add .
git commit -m "static-file-name-checker"
git push
File renamed without changes.
File renamed without changes
File renamed without changes
Loading