on:
  release:
    types: [published]
  push:
    branches: [master, main]
    paths:
      - DESCRIPTION
      - inst/CITATION
      - README.md
  workflow_dispatch:

name: Update codemeta.json

jobs:
  update-codemeta:
    runs-on: macos-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: r-lib/actions/setup-r@v2
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          extra-packages: |
            any::codemetar

      - name: Update codemeta.json
        run: codemetar::write_codemeta()
        shell: Rscript {0}

      - name: Commit results
        run: |
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add codemeta.json
          git commit -m 'Update codemeta.json' || echo "No changes to commit"
          git push origin || echo "No changes to commit"
