name: "CI Build"

on:
  push:
    branches-ignore:
      - 'dependabot/**'
    paths-ignore:
      - '.gitignore'
      - '*.md'
      - '*.adoc'
      - '*.txt'
      - '.github/ISSUE_TEMPLATE/**'
  pull_request:
    paths-ignore:
      - '.gitignore'
      - '*.md'
      - '*.adoc'
      - '*.txt'
      - '.github/ISSUE_TEMPLATE/**'

env:
  LANG: en_US.UTF-8
jobs:
  # This is a hack to work around a GitHub API limitation:
  # when the PR is coming from another fork, the pull_requests field of the
  # workflow_run payload is empty.
  # For more details, see
  # https://github.community/t/pull-request-attribute-empty-in-workflow-run-event-object-for-pr-from-forked-repo/154682
  attach-pr-number:
    runs-on: ubuntu-latest
    name: Attach pull request number
    if: github.event_name == 'pull_request'
    steps:
      - name: Create file
        shell: bash
        run: |
          echo -n ${{ github.event.number }} > pull-request-number
      - name: Upload pull request number
        uses: actions/upload-artifact@v2
        with:
          name: pull-request-number-${{ github.event.number }}
          path: pull-request-number
          retention-days: 1
  build:
    name: Build - JDK ${{ matrix.java }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        java: [ 11 ]
    steps:
      - uses: actions/checkout@v2
      - name: Get Date
        id: get-date
        run: |
          echo "::set-output name=date::$(/bin/date -u "+%Y-%m")"
        shell: bash
      - name: Cache Maven Repository
        id: cache-maven
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          # refresh cache every month to avoid unlimited growth
          key: maven-repo-pr-${{ runner.os }}-${{ steps.get-date.outputs.date }}
      - name: Install JDK ${{ matrix.java }}
        uses: actions/setup-java@v2
        with:
          distribution: temurin
          java-version: ${{ matrix.java }}
      - name: Build with Maven
        run: MAVEN_OPTS=-Xmx3072m && ./mvnw -B clean install --settings .github/mvn-settings.xml --fail-at-end
      - name: Delete Local Artifacts From Cache
        shell: bash
        run: rm -rf ~/.m2/repository/io/quarkus/quarkus-universe-bom*
      - name: Upload build reports (if build failed)
        uses: actions/upload-artifact@v2
        if: ${{ failure() || cancelled() }}
        with:
          name: "build-reports-Build - JDK ${{ matrix.java }}"
          path: |
            **/target/*-reports/TEST-*.xml
            target/build-report.json
            LICENSE
          retention-days: 2
  refresh:
    name: Check generated project has been updated
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get Date
        id: get-date
        run: |
          echo "::set-output name=date::$(/bin/date -u "+%Y-%m")"
        shell: bash
      - name: Cache Maven Repository
        id: cache-maven
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          # refresh cache every month to avoid unlimited growth
          key: maven-repo-pr-${{ runner.os }}-${{ steps.get-date.outputs.date }}
      - name: Install JDK ${{ matrix.java }}
        uses: actions/setup-java@v2
        with:
          distribution: temurin
          java-version: 11
      - name: Build with Maven
        run: MAVEN_OPTS=-Xmx3072m && ./mvnw -B clean process-resources --settings .github/mvn-settings.xml

      - name: Get changes
        id: get-changes
        run: |
          echo "::set-output name=changes::$(git status -s | wc -l)"
        shell: bash
      - name: Display changes
        run: |
          git status
          git --no-pager diff
        shell: bash

      - name: Check that generated project is properly updated
        uses: actions/github-script@v5
        if: ${{ steps.get-changes.outputs.changes > 0 }}
        with:
          script: |
            throw new Error('Generated project needs an update. Please run ./mvnw -Dsync and include the result in your pull request.')
