name: Daily Feed Update
on:
  schedule:
    - cron: '0 1 * * *'
  workflow_dispatch:
permissions:
  contents: write
jobs:
  update-feed:
    runs-on: ubuntu-latest
    container:
      image: node:18
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        server-url: 'https://git.mcdevitt.tech'
        repository: 'bpmcdevitt/github_poc_collector'

    - name: Setup SSH
      env:
        SSH_PRIVATE_KEY: ${{ secrets.TOKEN_NAME }}
      run: |
        mkdir -p ~/.ssh
        ssh-keyscan -p 3022 git.mcdevitt.tech >> ~/.ssh/known_hosts
        echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
        chmod 600 ~/.ssh/id_rsa

    - name: Setup Rust
      run: |
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
        [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
        export PATH="$HOME/.cargo/bin:$PATH"
        rustup default stable
        rustc --version
        cargo --version

    - name: Cache Cargo dependencies
      uses: actions/cache@v3
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

    - name: Run recent feed update
      env:
        GITHUB_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
      run: |
        [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
        export PATH="$HOME/.cargo/bin:$PATH"
        cargo run -- --feed recent

    - name: Commit and push results
      run: |
        git config user.name bpmcdevitt
        git config user.email brendan@mcdevitt.tech
        git add .
        git commit -m "Update recent feed data" || exit 0
        git push origin main
