Skip to content

Commit

Permalink
add action and script to check folder name
Browse files Browse the repository at this point in the history
  • Loading branch information
Aibono1225 committed May 13, 2024
1 parent 2b0eeaf commit ca8354b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/check-folder-name.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Rename Folder

on:
pull_request:
paths:
- '**/rule.md'
types: [opened, synchronize]

jobs:
rename-folder:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
repository-projects: read
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- name: Check for modified rule.md
id: check_modified
run: |
if git diff --name-only $(git merge-base origin/main HEAD) | grep -q 'rule.md'; then
echo "modified=true" >> "$GITHUB_OUTPUT"
else
echo "modified=false" >> "$GITHUB_OUTPUT"
fi
- name: Rename folders if necessary
id: rename_folders
if: steps.check_modified.outputs.modified == 'true'
run: |
# chmod +x .workflow/rename-folders.sh
sh .workflow/rename-folders.sh
- name: Commit and push changes
if: steps.rename_folders.outputs.rename == 'true'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "Rename folder to match URI"
git push || true
echo "Auto-fix applied"
29 changes: 29 additions & 0 deletions .workflow/rename-folders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

is_rename=false

for rule_file in $(git diff --name-only $(git merge-base origin/main HEAD) | grep 'rule.md'); do
folder_path=$(dirname $rule_file)
folder_name=${folder_path#rules/}
echo "Folder path: $folder_path"
echo "Folder name: $folder_name"

uri=$(grep -m 1 '^uri:' "${GITHUB_WORKSPACE}/$rule_file" | awk '{print $2}')
echo "URI: $uri"

if [ "$folder_name" != "$uri" ]; then
mv "${GITHUB_WORKSPACE}/$folder_path" "${GITHUB_WORKSPACE}/$uri"
is_rename=true

if grep -q '^redirects:' "${GITHUB_WORKSPACE}/$uri/rule.md"; then
if grep -q "^ *- $folder_name$" "${GITHUB_WORKSPACE}/$uri/rule.md"; then
echo "Old folder name is already in redirects"
else
sed -i "/^redirects:/a \ \ - $folder_name" "${GITHUB_WORKSPACE}/$uri/rule.md"
fi
else
sed -i "/uri:.*/a\\redirects:\\n - $folder_name" "${GITHUB_WORKSPACE}/$uri/rule.md"
fi
fi
done
echo "rename=$is_rename" >> "$GITHUB_OUTPUT"

0 comments on commit ca8354b

Please sign in to comment.