Sync News to Profile README #40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Sync News to Profile README | |
| on: | |
| schedule: | |
| - cron: '0 */6 * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Fetch latest news from website repo | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| mkdir -p /tmp/news | |
| items=$(gh api "repos/SagaSmithAI/SagaSmithAI.github.io/contents/news?ref=main" --jq '.[].name' 2>/dev/null || true) | |
| while IFS= read -r file; do | |
| if [ -z "$file" ]; then continue; fi | |
| content=$(gh api "repos/SagaSmithAI/SagaSmithAI.github.io/contents/news/$file?ref=main" --jq '.content' 2>/dev/null || true) | |
| if [ -n "$content" ]; then | |
| printf '%s' "$content" | base64 --decode > "/tmp/news/$file" | |
| fi | |
| done <<< "$items" | |
| find /tmp/news -maxdepth 1 -type f -printf '%f\n' | sort || true | |
| - name: Generate news markdown | |
| run: | | |
| NEWS_FILE=$(find /tmp/news -maxdepth 1 -type f -printf '%f\n' | sort -r | head -1) | |
| if [ -z "$NEWS_FILE" ]; then | |
| echo "No news files found" | |
| exit 0 | |
| fi | |
| : > /tmp/news_block.md | |
| while IFS= read -r f; do | |
| CONTENT=$(cat "/tmp/news/$f") | |
| TITLE=$(printf '%s\n' "$CONTENT" | grep "^title:" | head -1 | sed 's/title: *//') | |
| DATE=$(printf '%s\n' "$CONTENT" | grep "^date:" | head -1 | sed 's/date: *//') | |
| BODY=$(printf '%s\n' "$CONTENT" | sed -n '/^---$/,/^---$/!p' | sed '/./,$!d') | |
| printf '\n\n### %s — %s\n\n%s\n' "$DATE" "$TITLE" "$BODY" >> /tmp/news_block.md | |
| done < <(find /tmp/news -maxdepth 1 -type f -printf '%f\n' | sort -r | head -5) | |
| echo "News block generated" | |
| - name: Update README | |
| run: | | |
| README="profile/README.md" | |
| # Check if markers exist | |
| if grep -q "NEWS_START" "$README"; then | |
| # Replace between markers | |
| python3 - "$README" /tmp/news_block.md <<'PY' | |
| import re | |
| import sys | |
| from pathlib import Path | |
| readme = Path(sys.argv[1]) | |
| news = Path(sys.argv[2]).read_text(encoding="utf-8") | |
| content = readme.read_text(encoding="utf-8") | |
| content = re.sub( | |
| r'<!-- NEWS_START -->.*?<!-- NEWS_END -->', | |
| '<!-- NEWS_START -->\n' + news + '\n<!-- NEWS_END -->', | |
| content, | |
| flags=re.DOTALL, | |
| ) | |
| readme.write_text(content, encoding="utf-8") | |
| PY | |
| else | |
| echo "No NEWS markers found in README" | |
| exit 0 | |
| fi | |
| - name: Commit and push if changed | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add profile/README.md | |
| if ! git diff --cached --quiet; then | |
| git commit -m "docs: sync news $(date -u +%Y-%m-%d)" | |
| git push | |
| else | |
| echo "No changes" | |
| fi |