Check and Resolve Duplicate Filenames #2
This file contains hidden or 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
name: Check and Resolve Duplicate Filenames | |
on: | |
pull_request: | |
types: [closed] | |
branches: | |
- master | |
workflow_dispatch: | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
BRANCH_PREFIX: fix-duplicate-files | |
jobs: | |
check-duplicates: | |
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true) | |
runs-on: ubuntu-latest | |
outputs: | |
duplicates_found: ${{ steps.check.outputs.duplicates_found }} | |
duplicate_list: ${{ steps.check.outputs.duplicate_list }} | |
files_to_delete: ${{ steps.check.outputs.files_to_delete }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Check for duplicate filenames | |
id: check | |
run: | | |
echo "Checking for duplicate filenames across HTML, HTML_見送り, and HTML_未着手 folders..." | |
# Get basenames from all three directories | |
html_files=$(find "WAIC-TEST/HTML" -name "*.md" ! -name "README.md" -exec basename {} \; 2>/dev/null | sort) | |
miokuri_files=$(find "WAIC-TEST/HTML_見送り" -name "*.md" -exec basename {} \; 2>/dev/null | sort) | |
michakushu_files=$(find "WAIC-TEST/HTML_未着手" -name "*.md" -exec basename {} \; 2>/dev/null | sort) | |
# Find duplicates and files to delete | |
issue_body="" | |
files_to_delete="" | |
duplicates_found=false | |
# Check HTML vs HTML_未着手 (delete from HTML_未着手) | |
html_vs_michakushu=$(comm -12 <(echo "$html_files") <(echo "$michakushu_files")) | |
if [ -n "$html_vs_michakushu" ]; then | |
echo "❌ Duplicates found between HTML and HTML_未着手:" | |
duplicates_found=true | |
for file in $html_vs_michakushu; do | |
echo " - $file" | |
html_path=$(find WAIC-TEST/HTML -name "$file" -exec echo {} \;) | |
michakushu_path=$(find WAIC-TEST/HTML_未着手 -name "$file" -exec echo {} \;) | |
echo " 📁 HTML: $html_path" | |
echo " 📁 HTML_未着手: $michakushu_path (削除対象)" | |
issue_body="$issue_body- \`$file\`\n - 📁 HTML: \`$html_path\`\n - 📁 HTML_未着手: \`$michakushu_path\` (削除対象)\n\n" | |
files_to_delete="$files_to_delete$michakushu_path\n" | |
done | |
echo "" | |
fi | |
# Check HTML vs HTML_見送り (delete from HTML_見送り) | |
html_vs_miokuri=$(comm -12 <(echo "$html_files") <(echo "$miokuri_files")) | |
if [ -n "$html_vs_miokuri" ]; then | |
echo "❌ Duplicates found between HTML and HTML_見送り:" | |
duplicates_found=true | |
for file in $html_vs_miokuri; do | |
echo " - $file" | |
html_path=$(find WAIC-TEST/HTML -name "$file" -exec echo {} \;) | |
miokuri_path=$(find WAIC-TEST/HTML_見送り -name "$file" -exec echo {} \;) | |
echo " 📁 HTML: $html_path" | |
echo " 📁 HTML_見送り: $miokuri_path (削除対象)" | |
issue_body="$issue_body- \`$file\`\n - 📁 HTML: \`$html_path\`\n - 📁 HTML_見送り: \`$miokuri_path\` (削除対象)\n\n" | |
files_to_delete="$files_to_delete$miokuri_path\n" | |
done | |
echo "" | |
fi | |
# Check HTML_見送り vs HTML_未着手 (delete from HTML_未着手) | |
miokuri_vs_michakushu=$(comm -12 <(echo "$miokuri_files") <(echo "$michakushu_files")) | |
if [ -n "$miokuri_vs_michakushu" ]; then | |
echo "❌ Duplicates found between HTML_見送り and HTML_未着手:" | |
duplicates_found=true | |
for file in $miokuri_vs_michakushu; do | |
echo " - $file" | |
miokuri_path=$(find WAIC-TEST/HTML_見送り -name "$file" -exec echo {} \;) | |
michakushu_path=$(find WAIC-TEST/HTML_未着手 -name "$file" -exec echo {} \;) | |
echo " 📁 HTML_見送り: $miokuri_path" | |
echo " 📁 HTML_未着手: $michakushu_path (削除対象)" | |
issue_body="$issue_body- \`$file\`\n - 📁 HTML_見送り: \`$miokuri_path\`\n - 📁 HTML_未着手: \`$michakushu_path\` (削除対象)\n\n" | |
files_to_delete="$files_to_delete$michakushu_path\n" | |
done | |
echo "" | |
fi | |
if [ "$duplicates_found" = true ]; then | |
echo "duplicates_found=true" >> $GITHUB_OUTPUT | |
echo "duplicate_list<<EOF" >> $GITHUB_OUTPUT | |
echo -e "$issue_body" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
echo "files_to_delete<<EOF" >> $GITHUB_OUTPUT | |
echo -e "$files_to_delete" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
else | |
echo "✅ No duplicate filenames found across all directories." | |
echo "duplicates_found=false" >> $GITHUB_OUTPUT | |
fi | |
auto-fix-duplicates: | |
needs: check-duplicates | |
if: needs.check-duplicates.outputs.duplicates_found == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Delete duplicate files | |
run: | | |
echo "Deleting duplicate files..." | |
# Delete files listed in files_to_delete | |
echo -e "${{ needs.check-duplicates.outputs.files_to_delete }}" | while read -r file; do | |
if [ -n "$file" ] && [ -f "$file" ]; then | |
echo "Deleting: $file" | |
rm "$file" | |
fi | |
done | |
- name: Create Pull Request for duplicate removal | |
uses: peter-evans/create-pull-request@v7 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: "🗑️ Remove duplicate files across HTML folders [skip ci]" | |
title: "🗑️ 重複ファイルの自動削除" | |
body: | | |
## 概要 | |
HTML、HTML_見送り、HTML_未着手 フォルダ間で重複するファイルを検出し、優先度の低いフォルダから自動削除しました。 | |
## 削除されたファイル | |
${{ needs.check-duplicates.outputs.duplicate_list }} | |
## 削除ルール | |
- 優先度: HTML > HTML_見送り > HTML_未着手 | |
- 重複が見つかった場合、優先度の低い方から削除 | |
## 確認事項 | |
- [ ] 削除されたファイルの内容が HTML_見送り のファイルと同一であることを確認 | |
- [ ] 削除により影響を受ける他のファイルがないことを確認 | |
## 自動処理情報 | |
- **処理日時**: $(date -u +"%Y-%m-%d %H:%M:%S UTC") | |
- **検出コミット**: ${{ github.sha }} | |
- **ワークフロー**: ${{ github.workflow }} | |
- **実行イベント**: ${{ github.event_name }} | |
> このPRはGitHub Actionsにより自動生成されました。マージ前に内容をご確認ください。 | |
branch: ${{ env.BRANCH_PREFIX }}-${{ github.run_number }} | |
base: master | |
delete-branch: true | |