name: OpenAPI Generator

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

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
      - name: Cache maven dependencies
        uses: actions/cache@v3
        env:
          cache-name: cache-maven-repository
        with:
          path: |
            ~/.m2/repository
            ~/.gradle
            !~/.gradle/caches/*/plugin-resolution/
            !~/.m2/repository/org/openapitools/
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
      - name: Run maven
        run: ./mvnw clean --no-snapshot-updates --batch-mode --quiet install -DskipTests -Dorg.slf4j.simpleLogger.defaultLogLevel=error
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      - run: ls -la  modules/openapi-generator-cli/target
      - name: Upload openapi-generator-cli.jar artifact
        uses: actions/upload-artifact@v3
        with:
          name: openapi-generator-cli.jar
          path: modules/openapi-generator-cli/target/openapi-generator-cli.jar
          retention-days: 1

  test:
    name: Unit tests
    runs-on: ubuntu-latest
    needs:
      - build
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
      - name: Cache maven dependencies
        uses: actions/cache@v3
        env:
          cache-name: cache-maven-repository
        with:
          path: |
            ~/.m2/repository
            ~/.gradle
            !~/.gradle/caches/*/plugin-resolution/
            !~/.m2/repository/org/openapitools/
          key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
      - name: Run unit tests
        run: ./mvnw clean --no-snapshot-updates --batch-mode --quiet --fail-at-end test -Dorg.slf4j.simpleLogger.defaultLogLevel=error
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      - name: Publish unit test reports
        if: ${{ always() }}
        uses: actions/upload-artifact@v3
        with:
          name: surefire-test-results
          path: '**/surefire-reports/TEST-*.xml'

  documentation:
    name: Docs up-to-date
    runs-on: ubuntu-latest
    needs:
      - build
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
      - name: Download openapi-generator-cli.jar artifact
        uses: actions/download-artifact@v3
        with:
          name: openapi-generator-cli.jar
          path: modules/openapi-generator-cli/target
      - name: Generate docs
        run: |
          bash bin/meta-codegen.sh
          bash bin/utils/export_docs_generators.sh
          bash bin/utils/copy-to-website.sh
          bash bin/utils/export_generators_readme.sh
      - name: Verify git status
        run: |
          if [[ "$(git status --porcelain)" != "" ]]; then
            echo "UNCOMMITTED CHANGES ERROR"
            echo "There are uncommitted changes in working tree after execution of 'bin/ensure-up-to-date'"
            echo "Perform git diff"
            git --no-pager diff
            echo "Perform git status"
            git status
            echo -e "\nThis script runs in pull requests against the anticipated merge commit (as if the PR was merged now)."
            echo "When you see unexpected files here, it likely means that there are newer commits in master that you need to "
            echo -e "rebase or merge into your branch.\n"
            echo "Please run 'bin/utils/ensure-up-to-date' locally and commit changes (UNCOMMITTED CHANGES ERROR)"
            exit 1
          fi

  samples:
    name: Samples up-to-date
    needs:
      - build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
      - name: Download openapi-generator-cli.jar artifact
        uses: actions/download-artifact@v3
        with:
          name: openapi-generator-cli.jar
          path: modules/openapi-generator-cli/target
      - name: Generate samples
        run: bash bin/generate-samples.sh
      - name: Verify git status
        run: |
          if [[ "$(git status --porcelain)" != "" ]]; then
            echo "UNCOMMITTED CHANGES ERROR"
            echo "There are uncommitted changes in working tree after execution of 'bin/generate-samples.sh'"
            echo "Perform git diff"
            git --no-pager diff
            echo "Perform git status"
            git status
            echo -e "\nThis script runs in pull requests against the anticipated merge commit (as if the PR was merged now)."
            echo "When you see unexpected files here, it likely means that there are newer commits in master that you need to "
            echo -e "rebase or merge into your branch.\n"
            echo "Please run 'bin/generate-samples.sh' locally and commit changes (UNCOMMITTED CHANGES ERROR)"
            exit 1
          fi
