Skip to content

Commit

Permalink
create an action to remove trailing spaces (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaliu666 authored Dec 11, 2024
1 parent d7e50f7 commit 441ea7d
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/formt-pr-files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Format PR Files

on:
pull_request:
types:
- opened
paths:
- '**/*.md' # Trigger workflow only when .md files are modified in the PR

jobs:
whitespace:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Git config
run: |
git config --global user.name "miyaliu666"
git config --global user.email "[email protected]"
- name: Remove trailing whitespaces from Markdown files
run: |
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
echo "Head branch: ${{ github.event.pull_request.head.ref }}"
# Get the base commit between the target branch (main) and the source branch (HEAD)
base_branch="${{ github.event.pull_request.base.ref }}"
head_branch="${{ github.event.pull_request.head.ref }}"
git fetch origin $base_branch
git fetch origin $head_branch
base_commit=$(git merge-base origin/$base_branch origin/$head_branch)
echo "Base commit: $base_commit"
# Get the changed files in the current PR
echo "Checking changes in PR..."
files=$(git diff --name-only $base_commit origin/$head_branch)
echo "Changed files: $files"
# Filter out .md files
file=$(echo "$files" | grep '\.md$')
echo "Filtered .md file: $file"
if [ -n "$file" ]; then
echo "Processing file: $file"
# Remove trailing spaces from each line in the file
sed -i 's/ $//g' "$file"
# Switch to the source branch and push the changes
git checkout $head_branch
git add "$file"
git commit -m "Remove trailing spaces from $file"
git push origin $head_branch
else
echo "No .md files found or no changes to commit."
fi

0 comments on commit 441ea7d

Please sign in to comment.