name: Auto Merge
on: [pull_request_target, workflow_dispatch]

permissions:
  contents: write
  pull-requests: write

jobs:
  automerge:
    runs-on: ubuntu-latest
    # Run only if PR is inside JabRef's main repository and created by dependabot or by an update workflow
    if: >
      (github.repository == 'JabRef/jabref') &&
      (github.event.pull_request.head.repo.full_name == 'JabRef/jabref') &&
      (
        (github.actor == 'dependabot[bot]') ||
        (
          startsWith(github.event.pull_request.title, '[Bot] ') ||
          startsWith(github.event.pull_request.title, 'Bump ') ||
          startsWith(github.event.pull_request.title, 'New Crowdin updates') ||
          startsWith(github.event.pull_request.title, 'Update Gradle Wrapper from')
        )
      )
    steps:
      - name: Approve PR
        run: gh pr review --approve "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GH_TOKEN: ${{secrets.GH_TOKEN_JABREF_MACHINE_PR_APPROVE}}
      - name: Merge PR
        run: gh pr merge --auto --squash "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GH_TOKEN: ${{secrets.GH_TOKEN_UPDATE_GRADLE_WRAPPER}}