name: CI
on:
  push:
    branches:
      - '**'
      - '!integrated/**'
      - '!stl-preview-head/**'
      - '!stl-preview-base/**'
      - '!generated'
      - '!codegen/**'
      - 'codegen/stl/**'
  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) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')

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

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@0b6dd653ba04f4f93bf581ec31e66cbd7dcb644d # 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) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')

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

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@0b6dd653ba04f4f93bf581ec31e66cbd7dcb644d # 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' &&
          !startsWith(github.ref, 'refs/heads/stl/')
        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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

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

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

      - name: Run tests
        run: ./scripts/test
  examples:
    timeout-minutes: 20
    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) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')

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

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

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