name: Build, Test and Deploy GumTree

on:
  push:
    branches: [ main ]
    tags:
      - 'v*'
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '59 23 * * SUN'
  workflow_dispatch:

jobs:
  build-test-deploy:
    runs-on: ubuntu-latest
    container: gumtreediff/gumtree:latest
    environment: MavenCentral
    if: "!(contains(github.event.head_commit.message, '[no ci]') || startsWith(github.event.head_commit.message, 'doc'))"
    steps:
      - name: checkout gumtree
        uses: actions/checkout@v2
        with:
          submodules: recursive
      - name: retrieve gumtree version
        id: version
        run: echo "::set-output name=version::$(cat build.gradle | grep "projectsVersion =" | cut -f 2 -d "'")"
        shell: bash
      - name: build gumtree
        run: ./gradlew -PtestNative build :benchmark:runOnDefects4J :benchmark:ciDefects4JAnalysis
      - name: upload defects4j results
        uses: actions/upload-artifact@v2
        with:
          name: defects4j-report
          path: benchmark/build/reports/defects4j*
      - name: generate jacoco report
        run: ./gradlew jacocoRootReport
      - name: upload coverage report
        uses: actions/upload-artifact@v2
        with:
          name: coverage-report
          path: build/reports/jacoco/jacocoRootReport/html/
      - name: generate test report
        run: ./gradlew testReport
      - name: upload test report
        uses: actions/upload-artifact@v2
        with:
          name: test-report
          path: build/reports/tests
      - name: upload snapshot package
        if: "github.event_name == 'schedule' && contains(steps.version.outputs.version, 'SNAPSHOT')"
        run: ./gradlew publish
        env:
          ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEUSERNAME }}
          ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEPASSWORD }}
      - name: upload release package
        if: "github.event_name == 'push' && !contains(steps.version.outputs.version, 'SNAPSHOT') && endsWith(github.ref, steps.version.outputs.version)"
        run: ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository
        env:
          ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEUSERNAME }}
          ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEPASSWORD }}
          ORG_GRADLE_PROJECT_gumtreeKey: ${{ secrets.GUMTREE_KEY }}
          ORG_GRADLE_PROJECT_gumtreeKeyPassphrase: ${{ secrets.GUMTREE_KEY_PASSPHRASE }}
      - name: create github release
        if: "github.event_name == 'push' && !contains(steps.version.outputs.version, 'SNAPSHOT') && endsWith(github.ref, steps.version.outputs.version)"
        uses: ncipollo/release-action@v1
        with:
          artifacts: "dist/build/distributions/gumtree*.zip"
          bodyFile: "CHANGELOG.md"
          token: ${{ secrets.GITHUB_TOKEN }}