From c457b8facfbb67e5a7af12be4b261aac9ab2065a Mon Sep 17 00:00:00 2001 From: Louie Zhang Date: Tue, 28 Apr 2026 14:08:57 -0700 Subject: [PATCH] ci: add FAQ last_modified workflow --- .github/workflows/update-last-modified.yml | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 .github/workflows/update-last-modified.yml diff --git a/.github/workflows/update-last-modified.yml b/.github/workflows/update-last-modified.yml new file mode 100644 index 00000000..3411f573 --- /dev/null +++ b/.github/workflows/update-last-modified.yml @@ -0,0 +1,96 @@ +name: Update last_modified + +on: + pull_request: + branches: + - master + types: + - opened + - synchronize + - reopened + paths: + - "_articles/faq/*.md" + +concurrency: + group: update-last-modified-${{ github.event.pull_request.number }} + cancel-in-progress: true + +permissions: + contents: write + pull-requests: read + +jobs: + update-on-master-pr: + if: github.actor != 'github-actions[bot]' && github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + steps: + - name: Checkout PR head branch + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + fetch-depth: 0 + + - name: Collect target files + id: targets + shell: bash + run: | + set -euo pipefail + + git fetch origin "${{ github.event.pull_request.base.ref }}" + + git diff --name-only "origin/${{ github.event.pull_request.base.ref }}"...HEAD \ + | grep -E '^_articles/faq/[^/]+\.md$' \ + | sort -u > changed_files.txt || true + + count="$(wc -l < changed_files.txt | tr -d ' ')" + echo "count=$count" >> "$GITHUB_OUTPUT" + + - name: Update front matter + if: steps.targets.outputs.count != '0' + shell: bash + run: | + set -euo pipefail + timestamp="$(TZ=UTC date '+%Y-%m-%d %H:%M:%S +0000')" + + while IFS= read -r file; do + [[ -f "$file" ]] || continue + + TS="$timestamp" perl -0777 -i -pe ' + BEGIN { $ts = $ENV{"TS"}; } + + s{\A---\R(.*?)\R---(?:\R|$)}{ + my $fm = $1; + + if ($fm =~ /^last_modified:.*$/m) { + $fm =~ s/^last_modified:.*$/last_modified: $ts/m; + } elsif ($fm =~ /^date:.*$/m) { + $fm =~ s/^(date:.*)$/$1\nlast_modified: $ts/m; + } else { + $fm .= "\nlast_modified: $ts"; + } + + "---\n$fm\n---\n"; + }es; + ' "$file" + done < changed_files.txt + + - name: Commit and push to PR branch + if: steps.targets.outputs.count != '0' + shell: bash + run: | + set -euo pipefail + + if git diff --quiet; then + echo "No timestamp updates needed." + exit 0 + fi + + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + while IFS= read -r file; do + [[ -f "$file" ]] || continue + git add "$file" + done < changed_files.txt + git commit -m "chore: update last_modified timestamps" + git pull --rebase origin "${{ github.event.pull_request.head.ref }}" + git push origin HEAD:${{ github.event.pull_request.head.ref }}