name: Changelog

on:
  pull_request:
  push:
    branches: [ master ]
    tags: [ "*" ]

jobs:
  changelog:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: read

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ssh-key: ${{ secrets.DEPLOYMENT_SSH_KEY }}
          fetch-depth: 0

      - name: Generate changelog
        uses: actionhippie/calens@244f3e5c328b842a740113859b87bbebf697f63b # v1.13.1
        with:
          target: CHANGELOG.md

      - name: Show diff
        run: git diff

      - name: Output
        run: cat CHANGELOG.md

      - name: Publish
        if: >
          github.event_name == 'push' &&
          github.ref == 'refs/heads/master' &&
          !startsWith(github.ref, 'refs/tags/') &&
          !startsWith(github.ref, 'refs/pull/')
        run: |
          git config user.name "ownClouders"
          git config user.email "devops@owncloud.com"
          git add CHANGELOG.md
          if git diff --staged --quiet; then
            echo "No changes to commit"
            exit 0
          fi
          git commit -m "Automated changelog update [skip ci]"
          git push origin master
