diff --git a/.github/workflows/check-url-changes.yml b/.github/workflows/check-url-changes.yml index a41cc3eaf..a560728fc 100644 --- a/.github/workflows/check-url-changes.yml +++ b/.github/workflows/check-url-changes.yml @@ -1,10 +1,12 @@ name: Check Documentation URL Changes + on: pull_request: branches: - master + jobs: check-url-changes: runs-on: ubuntu-latest @@ -14,59 +16,99 @@ jobs: with: fetch-depth: 0 + - name: Identify deleted and renamed files run: | - # Store deleted files - DELETED_FILES=$(git diff --name-status origin/master ${{ github.event.pull_request.head.sha }} | grep '^D.*\.md$' | cut -f2- || true) + BASE_BRANCH=${{ github.event.pull_request.base.ref }} + HEAD_SHA=${{ github.event.pull_request.head.sha }} + BASE_REF="origin/${BASE_BRANCH}" + + git fetch origin "${BASE_BRANCH}" --depth=1 + + DELETED_FILES=$(git diff --name-status "$BASE_REF" "$HEAD_SHA" | grep '^D.*\.md$' | cut -f2- || true) + RENAMED_FILES=$(git diff --name-status "$BASE_REF" "$HEAD_SHA" | grep '^R.*\.md$' | awk '{print $2 " -> " $3}' || true) + + + NAV_FILES=$(git diff --name-only "$BASE_REF" "$HEAD_SHA" -- '*.nav.yml' '*.nav.yaml' || true) + if [ -n "$NAV_FILES" ]; then + while IFS= read -r nav_file; do + [ -z "$nav_file" ] && continue + NAV_DELETED_LINES=$(git diff "$BASE_REF" "$HEAD_SHA" -- "$nav_file" | grep '^-' | grep '\.md' | cut -c2- || true) + NAV_ADDED_LINES=$(git diff "$BASE_REF" "$HEAD_SHA" -- "$nav_file" | grep '^+' | grep '\.md' | cut -c2- || true) + + + if [ -n "$NAV_DELETED_LINES" ]; then + while IFS= read -r line; do + [ -z "$line" ] && continue + entry="$nav_file: ${line# }" + DELETED_FILES="${DELETED_FILES:+$DELETED_FILES"$'\n'"}$entry" + done <<< "$NAV_DELETED_LINES" + fi + + + if [ -n "$NAV_ADDED_LINES" ]; then + while IFS= read -r line; do + [ -z "$line" ] && continue + entry="$nav_file: ${line# }" + RENAMED_FILES="${RENAMED_FILES:+$RENAMED_FILES"$'\n'"}$entry" + done <<< "$NAV_ADDED_LINES" + fi + done <<< "$NAV_FILES" + fi + + echo "DELETED_FILES<> $GITHUB_ENV echo "$DELETED_FILES" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV - - # Store renamed/moved files - RENAMED_FILES=$(git diff --name-status origin/master ${{ github.event.pull_request.head.sha }} | grep '^R.*\.md$' | awk '{print $2 " -> " $3}' || true) + + echo "RENAMED_FILES<> $GITHUB_ENV echo "$RENAMED_FILES" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV - - # Set warning flag if there are any changes - if [ ! -z "$DELETED_FILES" ] || [ ! -z "$RENAMED_FILES" ]; then + + + if [ -n "$DELETED_FILES" ] || [ -n "$RENAMED_FILES" ]; then echo "warning=true" >> $GITHUB_ENV else echo "warning=false" >> $GITHUB_ENV fi - - # Print to console for logging + + echo "Deleted files:" echo "$DELETED_FILES" echo -e "\nRenamed/Moved files:" echo "$RENAMED_FILES" + - name: Post PR warning if: env.warning == 'true' uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const issue_number = context.payload.pull_request.number; - const repo = context.repo; const deletedFiles = `${process.env.DELETED_FILES}`.trim(); const renamedFiles = `${process.env.RENAMED_FILES}`.trim(); - - let message = `🔍 **Documentation URL Checker**\n\nThis PR modifies documentation files in ways that could potentially create broken links.\n\n`; - + const sections = []; + + if (deletedFiles) { - message += `**Deleted files:**\n\`\`\`\n${deletedFiles}\n\`\`\`\n\n`; + sections.push(`**Deleted Markdown files:**\n\`\`\`\n${deletedFiles}\n\`\`\``); } - + + if (renamedFiles) { - message += `**Renamed/Moved files:**\n\`\`\`\n${renamedFiles}\n\`\`\`\n\n`; + sections.push(`**Renamed Markdown files:**\n\`\`\`\n${renamedFiles}\n\`\`\``); } - - message += `🚨 Please review these changes carefully 🚨\n\n If not handled properly, broken links (404 errors) could appear. To maintain a smooth user experience, consider:\n- Adding redirects in the \`mkdocs.yml\` file from the old URLs to the new ones\n- Updating internal references to these files`; - - github.rest.issues.createComment({ - owner: repo.owner, - repo: repo.repo, - issue_number: issue_number, - body: message - }); \ No newline at end of file + + + if (!sections.length) { + return; + } + + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + body: sections.join('\n\n') + });