name: CI
on:
  push:
    branches-ignore:
      - 'generated'
      - 'codegen/**'
      - 'integrated/**'
      - 'stl-preview-head/**'
      - 'stl-preview-base/**'
  pull_request:
    branches-ignore:
      - 'stl-preview-head/**'
      - 'stl-preview-base/**'

jobs:
  lint:
    timeout-minutes: 15
    name: lint
    runs-on: ${{ github.repository == 'stainless-sdks/openai-java' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.fork

    steps:
      - uses: actions/checkout@v6

      - name: Set up Java
        uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: |
            8
            21
          cache: gradle

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v4

      - name: Run lints
        run: ./scripts/lint

  build:
    timeout-minutes: 30
    name: build
    permissions:
      contents: read
      id-token: write
    runs-on: ${{ github.repository == 'stainless-sdks/openai-java' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.fork

    steps:
      - uses: actions/checkout@v6

      - name: Set up Java
        uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: |
            8
            21
          cache: gradle

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v4

      - name: Build SDK
        # disable gradle daemon in CI because it is flakey
        env:
          GRADLE_OPTS: "-Dkotlin.compiler.execution.strategy=in-process"
        run: ./scripts/build

      - name: Build and upload Maven artifacts
        if: github.repository == 'stainless-sdks/openai-java'
        env:
          URL: https://pkg.stainless.com/s
          SHA: ${{ github.sha }}
          PROJECT: openai-java
        run: |
          set -euo pipefail

          max_retries=5
          attempt=1

          while true; do
            echo "Maven artifact upload attempt ${attempt}/${max_retries}"

            oidc_response="$(curl -sSf \
              -H "Authorization: bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" \
              "${ACTIONS_ID_TOKEN_REQUEST_URL}")"
            github_token="$(printf '%s' "$oidc_response" | jq -er '.value')"

            upload_log="$(mktemp)"
            set +e
            AUTH="$github_token" ./scripts/upload-artifacts 2>&1 | tee "$upload_log"
            upload_status=${PIPESTATUS[0]}
            set -e

            if [ "$upload_status" -eq 0 ]; then
              break
            fi

            if [ "$attempt" -lt "$max_retries" ] && \
              grep -q "Failed to get valid signed URL" "$upload_log" && \
              grep -q "GitHub JWT is missing or invalid\\." "$upload_log"; then
              sleep_seconds=$((3 * (1 << (attempt - 1))))
              echo "Retrying after fresh OIDC token due to transient GitHub JWT validation failure"
              echo "Sleeping ${sleep_seconds}s before retry"
              sleep "$sleep_seconds"
              attempt=$((attempt + 1))
              continue
            fi

            exit "$upload_status"
          done
  test:
    timeout-minutes: 30
    name: test
    runs-on: ${{ github.repository == 'stainless-sdks/openai-java' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
    steps:
      - uses: actions/checkout@v6

      - name: Set up Java
        uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: |
            8
            21
          cache: gradle

      - name: Set up Gradle
        uses: gradle/gradle-build-action@v2

      - name: Run tests
        run: ./scripts/test
  examples:
    timeout-minutes: 10
    name: examples
    runs-on: ${{ github.repository == 'stainless-sdks/openai-java' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
    if: github.repository == 'openai/openai-java' && (github.event_name == 'push' || github.event.pull_request.head.repo.fork)

    steps:
      - uses: actions/checkout@v6

      - name: Set up Java
        uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: |
            8
            21
          cache: gradle
      - name: Set up Gradle
        uses: gradle/gradle-build-action@v2

      - env: 
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
            ./gradlew :openai-java-example:run
