name: Knit readme

on:
  push:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  knit:
    runs-on: macOS-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - uses: r-lib/actions/setup-pandoc@v2
      - uses: r-lib/actions/setup-r@v2
      - uses: r-lib/actions/setup-renv@v2
               
      - name: Render readme files
        run: rmarkdown::render("README.Rmd", output_format = "github_document")
        shell: Rscript {0}
        
      - name: Commit data
        env:
          GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git commit README.md -m 'Update README list' || echo "No changes to commit"
          git push origin || echo "Nothing to push"    
          
      - name: Trigger test build
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GLOBAL_GHA_PAT }}
          script: |
            await github.rest.actions.createWorkflowDispatch({
              owner: 'rladies',
              repo: 'rladies.github.io',
              workflow_id: 'build.yaml',
              ref: 'main'
            })
