-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add action and script to check folder name
- Loading branch information
1 parent
2b0eeaf
commit ca8354b
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |