name: Create releases
on:
  schedule:
    - cron: '0 5 * * *' # every day at 5am UTC
  push:
    branches:
      - main

jobs:
  release:
    name: release
    if: github.ref == 'refs/heads/main' && github.repository == 'openai/openai-java'
    runs-on: ubuntu-latest
    environment: publish

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - uses: stainless-api/trigger-release-please@bb6677c5a04578eec1ccfd9e1913b5b78ed64c61 # v1.4.0
        id: release
        with:
          repo: ${{ github.event.repository.full_name }}
          stainless-api-key: ${{ secrets.STAINLESS_API_KEY }}

      - name: Set up Java
        if: ${{ steps.release.outputs.releases_created }}
        uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
        with:
          distribution: temurin
          java-version: |
            8
            21
          cache: gradle

      - name: Set up Gradle
        if: ${{ steps.release.outputs.releases_created }}
        uses: gradle/gradle-build-action@fe583dc97e032f41ccc310ea5176f2d7306abbc4 # v2

      - name: Compile the openai-java-core project
        run: |
          ./gradlew :openai-java-core:compileJava :openai-java-core:compileTestJava -x test

      - name: Run the mock server
        run: |
          ./scripts/mock --daemon

      - name: Setup GraalVM
        uses: graalvm/setup-graalvm@03e8abf916fd0e281b2efe7b2da3378bb0a1d085 # v1
        with:
          java-version: 21
          distribution: 'graalvm-community'
          cache: gradle

      - name: Run tests on the openai-java-core project with the GraalVM native-image agent
        run: |
          ./gradlew :openai-java-core:test -x compileJava -x compileTestJava -x compileKotlin -x compileTestKotlin -PgraalvmAgent

      - name: Check generated GraalVM file
        run: |
          echo "Checking for GraalVM agent metadata files..."
          DIRECTORY=openai-java-core/src/main/resources/META-INF/native-image
          if [ -d "$DIRECTORY" ] && [ "$(ls -A $DIRECTORY)" ]; then
            echo "Files found in $DIRECTORY:"
            ls -l $DIRECTORY
          else
            echo "No files found in $DIRECTORY"
            exit 1
          fi

      - name: Publish to Sonatype
        if: ${{ steps.release.outputs.releases_created }}
        run: |-
          export -- GPG_SIGNING_KEY_ID
          printenv -- GPG_SIGNING_KEY | gpg --batch --passphrase-fd 3 --import 3<<< "$GPG_SIGNING_PASSWORD"
          GPG_SIGNING_KEY_ID="$(gpg --with-colons --list-keys | awk -F : -- '/^pub:/ { getline; print "0x" substr($10, length($10) - 7) }')"
          ./gradlew publishAndReleaseToMavenCentral --stacktrace -PmavenCentralUsername="$SONATYPE_USERNAME" -PmavenCentralPassword="$SONATYPE_PASSWORD" --no-configuration-cache
        env:
          SONATYPE_USERNAME: ${{ secrets.OPENAI_SONATYPE_USERNAME || secrets.SONATYPE_USERNAME }}
          SONATYPE_PASSWORD: ${{ secrets.OPENAI_SONATYPE_PASSWORD || secrets.SONATYPE_PASSWORD }}
          GPG_SIGNING_KEY: ${{ secrets.OPENAI_SONATYPE_GPG_SIGNING_KEY || secrets.GPG_SIGNING_KEY }}
          GPG_SIGNING_PASSWORD: ${{ secrets.OPENAI_SONATYPE_GPG_SIGNING_PASSWORD || secrets.GPG_SIGNING_PASSWORD }}
