name: Java CI with Gradle in api directory
on:
  push:
    branches: [ '*' ]
  pull_request:
    branches: [ main ]
permissions:
  contents: read
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '21'
          distribution: 'temurin'
      - name: Grant execute permission for Gradle wrapper
        run: chmod +x ./gradlew
        working-directory: app/backend/api
      - name: Build with Gradle in api directory
        run: ./gradlew build -x test -x jigReports
        working-directory: app/backend/api

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '21'
          distribution: 'temurin'
      - name: Grant execute permission for Gradle wrapper
        run: chmod +x ./gradlew
        working-directory: app/backend/api
      - name: Test with Gradle in api directory
        run: ./gradlew test
        working-directory: app/backend/api

  coverage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '21'
          distribution: 'temurin'
      - name: Grant execute permission for Gradle wrapper
        run: chmod +x ./gradlew
        working-directory: app/backend/api

      - name: Gradle build
        run: ./gradlew clean build jacocoTestReport
        working-directory: app/backend/api

      - name: Download and set up Code Climate Test Reporter
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter
        working-directory: app/backend/api

      - name: Set jacoco.xml path for code climate
        env:
          JACOCO_SOURCE_PATH: src/main/java
        run: ./cc-test-reporter format-coverage ./build/reports/jacoco/test/jacocoTestReport.xml --input-type jacoco
        working-directory: app/backend/api

      - name: Upload code climate report
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: ./cc-test-reporter upload-coverage -r $CC_TEST_REPORTER_ID
        working-directory: app/backend/api
        if: success()