name: Update Documentation

on:
  push:
    branches:
      - main

jobs:
  build:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
      - uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1
        with:
          distribution: 'temurin'
          java-version: '21'
          cache: 'maven'
      - name: Compile and Build OpenAPI file
        run: ./mvnw compile
      - name: Update Documentation
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          cp -r api-doc/target/openapi/signal-server-openapi.yaml /tmp/
          git config user.email "github@signal.org"
          git config user.name "Documentation Updater"
          git fetch origin gh-pages
          git checkout gh-pages
          cp /tmp/signal-server-openapi.yaml .
          git diff --quiet || git commit -a -m "Updating documentation"
          git push origin gh-pages -q
