# This workflow is triggered by new releases and on a daily schedule.
# It builds, unit tests, live tests and published the Open AI nuget package.
# For daily runs, the package is published to the GitHub package registry.
# For releases, the package is published to the NuGet package registry.
name: Release package

on:
  release:
    types: [published]
  schedule:
    # run every day at 00:00
    - cron: '0 0 * * *'

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    environment: Live Testing
    env:
      version_suffix_args: ${{ github.event_name == 'schedule' && format('/p:VersionSuffix="alpha.{0}"', github.run_number) || '' }}
    permissions:
      packages: write
      contents: write
    steps:
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '8.x'

      - name: Checkout code
        uses: actions/checkout@v2

      # Pack the client nuget package and include url back to the repository and release tag
      - name: Build and Pack
        run: dotnet pack
          --configuration Release
          --output "${{ github.workspace }}/artifacts/packages"
          /p:PackageProjectUrl="${{ github.server_url }}/${{ github.repository }}/tree/${{ github.event.release.tag_name }}"
          /p:PackageReleaseNotes="${{ github.server_url }}/${{ github.repository }}/blob/${{ github.event.release.tag_name }}/CHANGELOG.md"
          ${{ env.version_suffix_args }}

      - name: Unit Test
        run: dotnet test
          --configuration Release
          --filter="TestCategory=Smoke&TestCategory!=Manual"
          --logger "trx;LogFileName=${{ github.workspace }}/artifacts/test-results/smoke.trx"
          ${{ env.version_suffix_args }}

      - name: Run Live Tests
        run: dotnet test ./tests/OpenAI.Tests.csproj
          --configuration Release
          --filter="TestCategory!=Smoke&TestCategory!=Assistants&TestCategory!=Images&TestCategory!=Uploads&TestCategory!=Moderations&TestCategory!=FineTuning&TestCategory!=Conversation&TestCategory!=Manual"
          --logger "trx;LogFilePrefix=live"
          --results-directory ${{ github.workspace }}/artifacts/test-results
          ${{ env.version_suffix_args }}
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        if: ${{ !cancelled() }}
        with:
          name: build-artifacts
          path: ${{ github.workspace }}/artifacts

  deploy:
    name: Publish Package
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Download build artifacts
        uses: actions/download-artifact@v4

      - name: Upload release asset
        if: github.event_name == 'release'
        run: gh release upload ${{ github.event.release.tag_name }}
          ${{ github.workspace }}/build-artifacts/packages/*.*nupkg
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: NuGet authenticate
        run: dotnet nuget add source
          "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
          --name "github"
          --username ${{ github.actor }}
          --password ${{ secrets.GITHUB_TOKEN }}
          --store-password-in-clear-text

      - name: Publish package to local feed
        run: dotnet nuget push
            ${{github.workspace}}/build-artifacts/packages/*.nupkg
            --source "github"
            --api-key ${{ secrets.GITHUB_TOKEN }}
            --skip-duplicate

      - name: Publish package to nuget.org
        if: github.event_name == 'release'
        run: dotnet nuget push
            ${{github.workspace}}/build-artifacts/packages/*.nupkg
            --source https://api.nuget.org/v3/index.json
            --api-key ${{ secrets.NUGET_API_KEY }}
            --skip-duplicate
