Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .github/workflows/comment-on-removed-files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Comment on removed .md files

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
detect-removed-markdown:
runs-on: ubuntu-latest
permissions:
pull-requests: write

steps:
- uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;

// Get all files in the PR
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});

// Filter for removed .md files
const removedMd = files
.filter(f => f.status === 'removed' && f.filename.endsWith('.md'))
.map(f => `- \`${f.filename}\``);

// Filter for renamed .md files
const renamedMd = files
.filter(f => f.status === 'renamed' && f.filename.endsWith('.md'))
.map(f => `- \`${f.previous_filename}\` → \`${f.filename}\``);

// Create a comment if there are any removed or renamed files
if (removedMd.length > 0 || renamedMd.length > 0) {
// Build sections based on what changes were detected
const sections = [];

// Add removed files section if there are any
if (removedMd.length > 0) {
sections.push(
removedMd.length === 1
? "The following Markdown file was **removed** in this PR:"
: `The following ${removedMd.length} Markdown files were **removed** in this PR:`,
...removedMd,
""
);
}

// Add renamed files section if there are any
if (renamedMd.length > 0) {
sections.push(
renamedMd.length === 1
? "The following Markdown file was **renamed** in this PR:"
: `The following ${renamedMd.length} Markdown files were **renamed** in this PR:`,
...renamedMd,
""
);
}

// Prepare comment body
const body = [
"## :warning: Markdown file changes detected",
"",
...sections,
"### Action Required",
"",
"We currently do not have an easy way to implement redirects for removed or renamed files. If possible, please:",
"",
"- 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)",
"- Add a notice at the top of the file indicating that the page has moved with a link to the new location",
"- Ensure you've updated `redirects.yml` accordingly if files must be removed or renamed",
"",
"Thank you!"
].join("\n");

// Post the comment on the PR
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body
});

console.log("Comment added about renamed or removed Markdown files");
} else {
console.log("No Markdown files were removed in this PR");
}
Loading