# This workflow includes more expensive tests than what is run on every PR, that are unlikely to break on most changes.
# It also optionally publishes a nightly prerelease.

name: Nightly test and release workflow (Reusable Workflow)

on:
  workflow_call:
    inputs:
      ref:
        required: true
        type: string
      publish-prerelease:
        required: false
        type: boolean
        default: false
    secrets:
      # Required if publish-prerelease is true
      nuget_api_key:
        required: false

jobs:
  deep-integration-tests:
    if: github.repository_owner == 'dafny-lang'
    uses: ./.github/workflows/integration-tests-reusable.yml
    with:
      ref: ${{ inputs.ref }}
      all_platforms: true
      num_shards: 10
      # Omit Rust because Rust is known to have random issues
      compilers: cs,java,go,js,cpp,dfy,py

  determine-vars:
    if: github.repository_owner == 'dafny-lang' && inputs.publish-prerelease
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout Dafny
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          submodules: recursive

      - name: Get short sha
        run: echo "sha_short=`git rev-parse --short HEAD`" >> $GITHUB_ENV

      - name: Get current date
        run: echo "date=`date +'%Y-%m-%d'`" >> $GITHUB_ENV

    outputs:
      name: nightly-${{ env.date }}-${{ env.sha_short }}

  publish-release:
    uses: ./.github/workflows/publish-release-reusable.yml
    needs: [deep-integration-tests, determine-vars]
    with:
      name: ${{ needs.determine-vars.outputs.name }}
      ref: ${{ inputs.ref }}
      tag_name: nightly
      release_nuget: true
      draft: false
      release_notes: "This is an automatically published nightly release. This release may not be as stable as versioned releases and does not contain release notes."
      prerelease: true
    secrets:
      nuget_api_key: ${{ secrets.nuget_api_key }}
