name: Changelog

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

jobs:
  changelog:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: read
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Generate changelog
        uses: docker://toolhippie/calens:0.4.0
        with:
          entrypoint: /bin/sh
          args: -c "calens > 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
