|
| 1 | +name: Update Last Updated Date |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize] |
| 6 | + branches: |
| 7 | + - main |
| 8 | + |
| 9 | +jobs: |
| 10 | + update-last-updated: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + ref: ${{ github.head_ref }} |
| 20 | + fetch-depth: 0 |
| 21 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + |
| 23 | + - name: Get changed MDX files |
| 24 | + id: changed-files |
| 25 | + uses: tj-actions/changed-files@v45 |
| 26 | + with: |
| 27 | + files: | |
| 28 | + **/*.mdx |
| 29 | +
|
| 30 | + - name: Update last-updated frontmatter |
| 31 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 32 | + run: | |
| 33 | + # Get current date in "Month Day, Year" format |
| 34 | + CURRENT_DATE=$(date +"%B %-d, %Y") |
| 35 | + echo "Current date: $CURRENT_DATE" |
| 36 | +
|
| 37 | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 38 | + echo "Processing: $file" |
| 39 | +
|
| 40 | + if [ ! -f "$file" ]; then |
| 41 | + echo "File not found, skipping: $file" |
| 42 | + continue |
| 43 | + fi |
| 44 | +
|
| 45 | + # Check if file has frontmatter (starts with ---) |
| 46 | + if ! head -1 "$file" | grep -q "^---"; then |
| 47 | + echo "No frontmatter found, skipping: $file" |
| 48 | + continue |
| 49 | + elif grep -q "^last-updated:" "$file"; then |
| 50 | + echo "Updating existing last-updated field" |
| 51 | + # Update existing last-updated field |
| 52 | + sed -i "s/^last-updated:.*$/last-updated: $CURRENT_DATE/" "$file" |
| 53 | + else |
| 54 | + echo "Adding last-updated field to existing frontmatter" |
| 55 | + # Add last-updated after the opening --- |
| 56 | + sed -i "0,/^---$/!{0,/^---$/b;s/^---$/last-updated: $CURRENT_DATE\n---/}" "$file" |
| 57 | + # If the above didn't work (complex sed), try awk |
| 58 | + if ! grep -q "^last-updated:" "$file"; then |
| 59 | + awk -v date="$CURRENT_DATE" ' |
| 60 | + NR==1 && /^---$/ { print; getline; print "last-updated: " date; print; next } |
| 61 | + { print } |
| 62 | + ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" |
| 63 | + fi |
| 64 | + fi |
| 65 | + done |
| 66 | +
|
| 67 | + - name: Commit changes |
| 68 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 69 | + run: | |
| 70 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 71 | + git config --local user.name "github-actions[bot]" |
| 72 | + git add -A |
| 73 | + if git diff --staged --quiet; then |
| 74 | + echo "No changes to commit" |
| 75 | + else |
| 76 | + git commit -m "chore: update last-updated date in MDX files" |
| 77 | + git push |
| 78 | + fi |
0 commit comments