|
| 1 | +name: Documentation Freshness Check |
| 2 | +on: |
| 3 | + schedule: |
| 4 | + - cron: '0 0 1 * *' # Run monthly on the 1st at midnight |
| 5 | + workflow_dispatch: # Allow manual trigger |
| 6 | + |
| 7 | +jobs: |
| 8 | + check_freshness: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/checkout@v3 |
| 12 | + with: |
| 13 | + fetch-depth: 0 # Need full history for git log |
| 14 | + |
| 15 | + - uses: actions/setup-python@v4 |
| 16 | + with: |
| 17 | + python-version: '3.x' |
| 18 | + |
| 19 | + - name: Create scripts directory |
| 20 | + run: mkdir -p .github/scripts |
| 21 | + |
| 22 | + - name: Copy Python script |
| 23 | + run: | |
| 24 | + cat > .github/scripts/check_freshness.py << 'EOL' |
| 25 | + import os |
| 26 | + import subprocess |
| 27 | + from datetime import datetime, timedelta |
| 28 | +
|
| 29 | + FRESHNESS_PERIOD = timedelta(days=180) # 6 months |
| 30 | + IGNORED_FILES = ["_index.md"] # Add any files to ignore |
| 31 | + now = datetime.now() |
| 32 | +
|
| 33 | + def get_last_modified_time(file_path): |
| 34 | + result = subprocess.run( |
| 35 | + ['git', 'log', '-1', '--format=%ct', file_path], |
| 36 | + stdout=subprocess.PIPE, |
| 37 | + text=True |
| 38 | + ) |
| 39 | + if not result.stdout.strip(): |
| 40 | + return now # Return current time for new files |
| 41 | + timestamp = int(result.stdout.strip()) |
| 42 | + return datetime.fromtimestamp(timestamp) |
| 43 | +
|
| 44 | + def check_freshness(directory): |
| 45 | + stale_files = [] |
| 46 | + for root, _, files in os.walk(directory): |
| 47 | + for file in files: |
| 48 | + if file.endswith(".md"): |
| 49 | + relative_path = os.path.relpath(os.path.join(root, file), start=directory) |
| 50 | + if file in IGNORED_FILES: |
| 51 | + continue |
| 52 | + last_modified = get_last_modified_time(os.path.join(root, file)) |
| 53 | + time_diff = now - last_modified |
| 54 | + if time_diff > FRESHNESS_PERIOD: |
| 55 | + stale_files.append((relative_path, last_modified)) |
| 56 | + return stale_files |
| 57 | +
|
| 58 | + docs_directory = "docs/content/master" # Crossplane docs directory |
| 59 | + stale_docs = check_freshness(docs_directory) |
| 60 | + sorted_stale_docs = sorted(stale_docs, key=lambda x: x[1]) |
| 61 | +
|
| 62 | + if sorted_stale_docs: |
| 63 | + issue_body = "## Stale Documentation Alert\n\n" |
| 64 | + issue_body += "The following documentation files have not been modified in the last 6 months:\n\n" |
| 65 | + for doc, mod_time in sorted_stale_docs: |
| 66 | + issue_body += f"- `{doc}` (Last Modified: {mod_time.strftime('%Y-%m-%d')})\n" |
| 67 | + issue_body += "\nPlease review these documents to ensure they are up-to-date and accurate." |
| 68 | + |
| 69 | + with open("issue_body.txt", "w") as f: |
| 70 | + f.write(issue_body) |
| 71 | + |
| 72 | + with open("has_stale.txt", "w") as f: |
| 73 | + f.write("true") |
| 74 | + else: |
| 75 | + with open("has_stale.txt", "w") as f: |
| 76 | + f.write("false") |
| 77 | + EOL |
| 78 | +
|
| 79 | + - name: Run freshness check |
| 80 | + run: python3 .github/scripts/check_freshness.py |
| 81 | + |
| 82 | + - name: Check for stale docs |
| 83 | + id: check |
| 84 | + run: | |
| 85 | + HAS_STALE=$(cat has_stale.txt) |
| 86 | + echo "has_stale=$HAS_STALE" >> $GITHUB_OUTPUT |
| 87 | +
|
| 88 | + - name: Create Issue |
| 89 | + if: steps.check.outputs.has_stale == 'true' |
| 90 | + uses: actions/github-script@v6 |
| 91 | + with: |
| 92 | + script: | |
| 93 | + const fs = require('fs'); |
| 94 | + const issueBody = fs.readFileSync('issue_body.txt', 'utf8'); |
| 95 | + |
| 96 | + await github.rest.issues.create({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + title: '📚 Documentation Freshness Check Results', |
| 100 | + body: issueBody, |
| 101 | + labels: ['documentation', 'maintenance'] |
| 102 | + }); |
0 commit comments