|
| 1 | +name: Comment on removed .md files |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + detect-removed-markdown: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | + steps: |
| 14 | + - uses: actions/github-script@v7 |
| 15 | + with: |
| 16 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + script: | |
| 18 | + const prNumber = context.payload.pull_request.number; |
| 19 | +
|
| 20 | + // Get all files in the PR |
| 21 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 22 | + owner: context.repo.owner, |
| 23 | + repo: context.repo.repo, |
| 24 | + pull_number: prNumber |
| 25 | + }); |
| 26 | +
|
| 27 | + // Filter for removed .md files |
| 28 | + const removedMd = files |
| 29 | + .filter(f => f.status === 'removed' && f.filename.endsWith('.md')) |
| 30 | + .map(f => `- \`${f.filename}\``); |
| 31 | + |
| 32 | + // Filter for renamed .md files |
| 33 | + const renamedMd = files |
| 34 | + .filter(f => f.status === 'renamed' && f.filename.endsWith('.md')) |
| 35 | + .map(f => `- \`${f.previous_filename}\` → \`${f.filename}\``); |
| 36 | +
|
| 37 | + // Create a comment if there are any removed or renamed files |
| 38 | + if (removedMd.length > 0 || renamedMd.length > 0) { |
| 39 | + // Build sections based on what changes were detected |
| 40 | + const sections = []; |
| 41 | + |
| 42 | + // Add removed files section if there are any |
| 43 | + if (removedMd.length > 0) { |
| 44 | + sections.push( |
| 45 | + removedMd.length === 1 |
| 46 | + ? "The following Markdown file was **removed** in this PR:" |
| 47 | + : `The following ${removedMd.length} Markdown files were **removed** in this PR:`, |
| 48 | + ...removedMd, |
| 49 | + "" |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + // Add renamed files section if there are any |
| 54 | + if (renamedMd.length > 0) { |
| 55 | + sections.push( |
| 56 | + renamedMd.length === 1 |
| 57 | + ? "The following Markdown file was **renamed** in this PR:" |
| 58 | + : `The following ${renamedMd.length} Markdown files were **renamed** in this PR:`, |
| 59 | + ...renamedMd, |
| 60 | + "" |
| 61 | + ); |
| 62 | + } |
| 63 | +
|
| 64 | + // Prepare comment body |
| 65 | + const body = [ |
| 66 | + "## :warning: Markdown file changes detected", |
| 67 | + "", |
| 68 | + ...sections, |
| 69 | + "### Action Required", |
| 70 | + "", |
| 71 | + "We currently do not have an easy way to implement redirects for removed or renamed files. If possible, please:", |
| 72 | + "", |
| 73 | + "- Keep files whenever possible and hide them from the TOC by using [`hidden`](https://elastic.github.io/docs-builder/configure/content-set/navigation/#hidden-files)", |
| 74 | + "- Add a notice at the top of the file indicating that the page has moved with a link to the new location", |
| 75 | + "- Ensure you've updated `redirects.yml` accordingly if files must be removed or renamed", |
| 76 | + "", |
| 77 | + "Thank you!" |
| 78 | + ].join("\n"); |
| 79 | +
|
| 80 | + // Post the comment on the PR |
| 81 | + await github.rest.issues.createComment({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + issue_number: prNumber, |
| 85 | + body |
| 86 | + }); |
| 87 | + |
| 88 | + console.log("Comment added about renamed or removed Markdown files"); |
| 89 | + } else { |
| 90 | + console.log("No Markdown files were removed in this PR"); |
| 91 | + } |
0 commit comments