name: Linux tests

on:
  push:
    branches:
      - master
      - '[5-9]+.[0-9]+.x'
  pull_request:
    branches:
      - master
      - '[5-9]+.[0-9]+.x'

jobs:
  build:
    name: 'Build: JDK ${{ matrix.java }} (${{ matrix.os }})'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        java: [11, 17]
        os: [ubuntu-latest]
    steps:
      - name: Check out code
        uses: actions/checkout@v3

      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: ${{ matrix.java }}

      - uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('pom.xml', 'modules/**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      - uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('modules/openapi-generator-gradle-plugin/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Setup Maven
        uses: s4u/setup-maven-action@v1.8.0
        with:
          java-version: ${{ matrix.java }}
          maven-version: 3.8.8

      - name: Build with Maven
        shell: bash
        run: ./mvnw clean -nsu -B --quiet -Dorg.slf4j.simpleLogger.defaultLogLevel=error --no-transfer-progress install --file pom.xml
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}

      - name: Upload Maven build artifact
        uses: actions/upload-artifact@v3
        if: matrix.java == '11' && matrix.os == 'ubuntu-latest'
        with:
          name: artifact
          path: modules/openapi-generator-cli/target/openapi-generator-cli.jar

      - name: Test Gradle plugin usage
        shell: bash
        run: gradle -b modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle buildGoSdk --stacktrace

      - name: Test Maven plugin integration
        if: matrix.java == '11'
        shell: bash
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
        run: |
          cd modules/openapi-generator-maven-plugin
          mvn clean verify -Pintegration

  verify:
    name: Verify outputs on ${{ matrix.os }}
    needs: build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
    steps:
      - name: Check out code
        uses: actions/checkout@v3
      - name: Setup Maven
        uses: s4u/setup-maven-action@v1.8.0
        with:
          java-version: 11
          maven-version: 3.8.8
      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: artifact
      - name: Run Ensures Script
        shell: bash
        run: |
          git config --global core.fileMode false
          git config --global core.safecrlf false
          git config --global core.autocrlf true
          ./mvnw clean package -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
          # test with java (jersey2) client generation only as ensure-up-to-date script is run in another job instead
          ./bin/generate-samples.sh ./bin/configs/java-jersey2-8.yaml
          # test debugSupportingFiles
          ./bin/generate-samples.sh ./bin/configs/go-petstore.yaml -- --global-property debugSupportingFiles
