name: Build pull request

on:
  pull_request:

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

jobs:
  common:
    uses: ./.github/workflows/build-common.yml
    with:
      # it's rare for only the openj9 tests or the windows smoke tests to break
      skip-openj9-tests: ${{ !contains(github.event.pull_request.labels.*.name, 'test openj9') }}
      skip-windows-smoke-tests: ${{ !contains(github.event.pull_request.labels.*.name, 'test windows') }}
      cache-read-only: true

  test-latest-deps:
    uses: ./.github/workflows/reusable-test-latest-deps.yml
    with:
      cache-read-only: true

  test-indy:
    uses: ./.github/workflows/reusable-test-indy.yml
    with:
      cache-read-only: true

  test-native:
    uses: ./.github/workflows/reusable-native-tests.yml
    with:
      skip-native-tests: ${{ !contains(github.event.pull_request.labels.*.name, 'test native') }}

  muzzle:
    uses: ./.github/workflows/reusable-muzzle.yml
    with:
      cache-read-only: true

  shell-script-check:
    uses: ./.github/workflows/reusable-shell-script-check.yml

  # this is not a required check to avoid blocking pull requests if external links break
  markdown-link-check:
    # release branches are excluded because the README.md javaagent download link has to be updated
    # on release branches before the release download has been published
    if: "!startsWith(github.ref_name, 'release/') && !startsWith(github.base_ref, 'release/')"
    uses: ./.github/workflows/reusable-markdown-link-check.yml

  markdown-lint-check:
    uses: ./.github/workflows/reusable-markdown-lint-check.yml

  misspell-check:
    uses: ./.github/workflows/reusable-misspell-check.yml

  required-status-check:
    # test-latest-deps is not included in the required status checks
    # because any time a new library version is released to maven central
    # it can fail due to test code incompatibility with the new library version,
    # or due to slight changes in emitted telemetry
    # (muzzle can also fail when a new library version is released to maven central
    # but that happens much less often)
    #
    # only the "common" checks are required for release branch PRs in order to avoid any unnecessary
    # release branch maintenance (especially for patches)
    needs:
      - common
      - muzzle
      - shell-script-check
      - markdown-lint-check
      - misspell-check
    runs-on: ubuntu-latest
    if: always()
    steps:
      - if: |
          needs.common.result != 'success' ||
          (
            !startsWith(github.base_ref, 'release/') &&
            (
              needs.muzzle.result != 'success' ||
              needs.shell-script-check.result != 'success' ||
              needs.markdown-lint-check.result != 'success' ||
              needs.misspell-check.result != 'success'
            )
          )
        run: exit 1 # fail
