name: Build

on:
  push:
    branches:
      - main
      - release/*
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-latest
          - ubuntu-20.04
        test-java-version:
          - 8
          - 11
          - 17
          - 19
        # Collect coverage on latest LTS
        include:
          - os: ubuntu-20.04
            test-java-version: 17
            coverage: true
    steps:
      - uses: actions/checkout@v3

      - id: setup-java-test
        name: Set up Java ${{ matrix.test-java-version }} for tests
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: ${{ matrix.test-java-version }}

      - id: setup-java
        name: Set up Java for build
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - uses: gradle/gradle-build-action@v2
        with:
          arguments: |
            build
            ${{ matrix.coverage && 'jacocoTestReport' || '' }}
            -PtestJavaVersion=${{ matrix.test-java-version }}
            -Porg.gradle.java.installations.paths=${{ steps.setup-java-test.outputs.path }},${{ steps.setup-java.outputs.path }}

      - name: Check for diff
        # The jApiCmp diff compares current to latest, which isn't appropriate for release branches
        if: ${{ !startsWith(github.ref_name, 'release/') && !startsWith(github.base_ref, 'release/') }}
        run: |
          # need to "git add" in case any generated files did not already exist
          git add docs/apidiffs
          if git diff --cached --quiet
          then 
            echo "No diff detected."
          else 
            echo "Diff detected - did you run './gradlew jApiCmp'?"
            echo $(git diff --cached --name-only)
            echo $(git diff --cached)
            exit 1
          fi

      - uses: codecov/codecov-action@v3
        if: ${{ matrix.coverage }}

      - uses: actions/upload-artifact@v3
        if: ${{ matrix.coverage }}
        with:
          name: coverage-report
          path: all/build/reports/jacoco/test/html

  markdown-link-check:
    # release branches are excluded to avoid unnecessary maintenance
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-markdown-link-check.yml

  misspell-check:
    # release branches are excluded to avoid unnecessary maintenance
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-misspell-check.yml

  publish-snapshots:
    # the condition is on the steps below instead of here on the job, because skipping the job
    # causes the job to show up as canceled in the GitHub UI which prevents the build section from
    # collapsing when everything (else) is green
    #
    # and the name is updated when the steps below are skipped which makes what's happening clearer
    # in the GitHub UI
    #
    # note: the condition below has to be written so that '' is last since it resolves to false
    # and so would not short-circuit if used in the second-last position
    name: publish-snapshots${{ (github.ref_name != 'main' || github.repository != 'open-telemetry/opentelemetry-java') && ' (skipped)' || '' }}
    # intentionally not blocking snapshot publishing on markdown-link-check or misspell-check
    needs: build
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3

      - id: setup-java
        name: Set up Java
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - uses: gradle/gradle-build-action@v2
        # skipping release branches because the versions in those branches are not snapshots
        # (also this skips pull requests)
        if: ${{ github.ref_name == 'main' && github.repository == 'open-telemetry/opentelemetry-java' }}
        with:
          arguments: assemble publishToSonatype
        env:
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}

  build-graal:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: graalvm/setup-graalvm@v1
        with:
          version: 'latest'
          java-version: '17'
          components: 'native-image'
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Running test
        run: |
          echo "GRAALVM_HOME: $GRAALVM_HOME"
          echo "JAVA_HOME: $JAVA_HOME"
          java --version
          gu --version
          native-image --version
          ./gradlew nativeTest

  required-status-check:
    # markdown-link-check is not required so pull requests are not blocked if external links break
    # misspell-check is not required so pull requests are not blocked if the misspell dictionary is
    # updated
    needs:
      - build
      - build-graal
    runs-on: ubuntu-latest
    if: always()
    steps:
      - if: needs.build.result != 'success'
        run: exit 1
