Skip to content

docs: improve README files for batch 01 (apache_hbase → gnews) #23

docs: improve README files for batch 01 (apache_hbase → gnews)

docs: improve README files for batch 01 (apache_hbase → gnews) #23

name: README
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
check-readme-update:
runs-on: ubuntu-latest
name: Check Updates for New Examples
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for proper diff
- name: Expose PR SHAs
run: |
echo "BASE_SHA=${{ github.event.pull_request.base.sha }}" >> $GITHUB_ENV
echo "HEAD_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
echo "HEAD_REPO=${{ github.event.pull_request.head.repo.clone_url }}" >> $GITHUB_ENV
- name: Ensure SHAs are fetched
run: |
git fetch --no-tags --prune origin $BASE_SHA
git fetch --no-tags --prune $HEAD_REPO $HEAD_SHA
- name: Create check script
id: create-script
run: |
cat > check-readme-update.js << 'EOF'
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Get the list of files changed in the PR
function getChangedFiles() {
try {
// Get the base branch (target) of the PR
const baseSha = process.env.BASE_SHA;
// Get current branch (source) of the PR
const headSha = process.env.HEAD_SHA;
// Use Git directly to get all changed files between branches
const gitDiff = execSync(`git diff --name-status ${baseSha}...${headSha}`,
{ encoding: 'utf-8' });
// Parse the git diff output
return gitDiff.trim().split('\n').filter(line => line).map(line => {
const [status, filename] = line.split('\t');
return { status, filename };
});
} catch (error) {
console.error('Error getting changed files:', error);
process.exit(1);
}
}
// Check if a directory exists before our PR
function directoryExistsInBase(dirPath) {
try {
const baseSha = process.env.BASE_SHA;
// Check if this directory exists in the base branch
const result = execSync(
`git ls-tree -d ${baseSha} ${dirPath}`,
{ encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }
);
return result.trim() !== '';
} catch (error) {
// If we get an error, the directory doesn't exist in base
return false;
}
}
// Main function to check if directories were added without README update
function checkReadmeUpdate() {
// Define directories to ignore (infrastructure directories)
const ignoredDirectories = [
'git_hooks',
'.github',
'.github/workflows',
'.circleci',
'.devops',
'ci',
'.ci',
'scripts'
];
// Get all files changed in this PR
const changedFiles = getChangedFiles();
// Track added directories
const addedDirs = new Set();
// Check for README.md update
let isReadmeUpdated = false;
// Process each changed file
changedFiles.forEach(file => {
// Skip deleted files
if (file.status === 'D') return;
// Check if this is the root README.md
if (file.filename === 'README.md' && !file.filename.includes('/')) {
isReadmeUpdated = true;
}
// Check if this file adds a new directory
const dirPath = path.dirname(file.filename);
if (dirPath !== '.') {
// Add this directory and all parent directories, but only if they're new
let currentPath = dirPath;
while (currentPath && currentPath !== '.') {
// Only add if this directory doesn't exist in the base branch
// and is not in our ignore list (including subdirectories)
if (
!directoryExistsInBase(currentPath) &&
!ignoredDirectories.some(
ignored => currentPath === ignored || currentPath.startsWith(ignored + '/')
)
) {
addedDirs.add(currentPath);
}
currentPath = path.dirname(currentPath);
}
}
});
// Log what we found
if (addedDirs.size > 0) {
console.log(`New directories added: ${Array.from(addedDirs).join(', ')}`);
} else {
console.log('No new directories added.');
}
// If we have new directories but README isn't updated, fail the check
if (addedDirs.size > 0 && !isReadmeUpdated) {
console.error(`::error::PR adds new directories (${Array.from(addedDirs).join(', ')}) but README.md is not updated. Please update README.md to document these new directories.`);
process.exit(1);
}
console.log('Check passed!');
}
// Run the check
checkReadmeUpdate();
EOF
- name: Run directory check script
run: node check-readme-update.js
- name: Debug information
if: always()
run: |
echo "BASE_SHA: $BASE_SHA"
echo "HEAD_SHA: $HEAD_SHA"
echo "HEAD_REPO: $HEAD_REPO"
echo "Git Branches:"
git branch -a