Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

196 lines
7.8 KiB

name: Auto Add SEO Descriptions
on:
pull_request:
paths:
- 'docs/en/**/*.md'
branches:
- 'rel-*'
- 'dev'
types: [closed]
jobs:
add-seo-descriptions:
if: |
github.event.pull_request.merged == true &&
!startsWith(github.event.pull_request.head.ref, 'auto-docs-seo/')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.merge_commit_sha }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install openai
- name: Create new branch for SEO updates
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# Checkout base branch first
git checkout ${{ github.event.pull_request.base.ref }}
# Create new branch from base
BRANCH_NAME="auto-docs-seo/${{ github.event.pull_request.number }}"
git checkout -b $BRANCH_NAME
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
- name: Get changed markdown files from merged PR
id: changed-files
run: |
# Get the list of commits in the PR
PR_HEAD_SHA="${{ github.event.pull_request.head.sha }}"
PR_BASE_SHA="${{ github.event.pull_request.base.sha }}"
echo "PR commits range: $PR_BASE_SHA..$PR_HEAD_SHA"
# Get files changed in the PR commits only (only Added and Modified, exclude Deleted)
FILES=$(git diff --name-only --diff-filter=AM $PR_BASE_SHA..$PR_HEAD_SHA | grep 'docs/en/.*\.md$' || true)
echo "Files changed in the merged PR (added/modified only):"
echo "$FILES"
echo ""
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
echo "$FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
if [ -z "$FILES" ]; then
echo "has_files=false" >> $GITHUB_OUTPUT
echo "No markdown files changed in docs/en/"
else
echo "has_files=true" >> $GITHUB_OUTPUT
# Checkout the changed files from merge commit to get the merged content
echo ""
echo "Checking out changed files from merge commit..."
while IFS= read -r file; do
if [ -n "$file" ]; then
echo " Checking out: $file"
# Create directory if it doesn't exist
mkdir -p "$(dirname "$file")"
# Checkout the file from merge commit
if ! git show "${{ github.event.pull_request.merge_commit_sha }}:$file" > "$file" 2>err.log; then
echo " Warning: Could not checkout $file"
echo " Reason: $(cat err.log)"
fi
fi
done <<< "$FILES"
echo ""
echo "Files now in working directory:"
git status --short
fi
- name: Process changed files and add SEO descriptions
if: steps.changed-files.outputs.has_files == 'true'
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
IGNORED_FOLDERS: ${{ vars.DOCS_SEO_IGNORED_FOLDERS }}
CHANGED_FILES: ${{ steps.changed-files.outputs.changed_files }}
run: |
python3 .github/scripts/add_seo_descriptions.py
- name: Commit and push changes
if: steps.changed-files.outputs.has_files == 'true'
run: |
git add -A docs/en/
if git diff --staged --quiet; then
echo "No changes to commit"
echo "has_commits=false" >> $GITHUB_ENV
else
git commit -m "docs: Add SEO descriptions to modified documentation files" -m "Related to PR #${{ github.event.pull_request.number }}"
git push origin ${{ env.BRANCH_NAME }}
echo "has_commits=true" >> $GITHUB_ENV
fi
- name: Create Pull Request
if: env.has_commits == 'true'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const stats = fs.readFileSync('/tmp/seo_stats.txt', 'utf8').split('\n');
const processedCount = parseInt(stats[0]) || 0;
const skippedCount = parseInt(stats[1]) || 0;
const skippedTooShort = parseInt(stats[2]) || 0;
const skippedIgnored = parseInt(stats[3]) || 0;
const prNumber = ${{ github.event.pull_request.number }};
const baseRef = '${{ github.event.pull_request.base.ref }}';
const branchName = '${{ env.BRANCH_NAME }}';
if (processedCount > 0) {
// Read the actually updated files list (not all changed files)
const updatedFilesStr = fs.readFileSync('/tmp/seo_updated_files.txt', 'utf8');
const updatedFiles = updatedFilesStr.trim().split('\n').filter(f => f.trim());
let prBody = '🤖 **Automated SEO Descriptions**\n\n';
prBody += `This PR automatically adds SEO descriptions to documentation files that were modified in PR #${prNumber}.\n\n`;
prBody += '## 📊 Summary\n';
prBody += `- ✅ **Updated:** ${processedCount} file(s)\n`;
prBody += `- ⏭️ **Skipped (total):** ${skippedCount} file(s)\n`;
if (skippedTooShort > 0) {
prBody += ` - ⏭️ Content < 200 chars: ${skippedTooShort} file(s)\n`;
}
if (skippedIgnored > 0) {
prBody += ` - 🚫 Ignored folders: ${skippedIgnored} file(s)\n`;
}
prBody += '\n## 📝 Modified Files\n';
prBody += updatedFiles.slice(0, 20).map(f => `- \`${f}\``).join('\n');
if (updatedFiles.length > 20) {
prBody += `\n- ... and ${updatedFiles.length - 20} more`;
}
prBody += '\n\n## 🔧 Details\n';
prBody += `- **Related PR:** #${prNumber}\n\n`;
prBody += 'These descriptions were automatically generated to improve SEO and search engine visibility. 🚀';
const { data: pr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `docs: Add SEO descriptions (from PR ${prNumber})`,
head: branchName,
base: baseRef,
body: prBody
});
console.log(`✅ Created PR: ${pr.html_url}`);
// Add reviewers to the PR (from GitHub variable)
const reviewersStr = '${{ vars.DOCS_SEO_REVIEWERS || '' }}';
const reviewers = reviewersStr.split(',').map(r => r.trim()).filter(r => r);
if (reviewers.length === 0) {
console.log('⚠️ No reviewers specified in DOCS_SEO_REVIEWERS variable.');
return;
}
try {
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
reviewers: reviewers,
team_reviewers: []
});
console.log(`✅ Added reviewers (${reviewers.join(', ')}) to PR ${pr.number}`);
} catch (error) {
console.log(`⚠️ Could not add reviewers: ${error.message}`);
}
}