generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 34
55 lines (45 loc) · 1.82 KB
/
formt-pr-files.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
name: Format PR Files
on:
pull_request:
paths:
- '**/*.md' # Trigger workflow only when .md files are modified in the PR
jobs:
whitespace:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
- 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/[[:space:]]*$//g' "$file"
fi
- name: Push changes
uses: EndBug/add-and-commit@v9
with:
message: Remove trailing spaces
default_author: github_actions