name: Tests

on: [push, workflow_dispatch]

permissions: {}
jobs:
  runPush:
    permissions:
      contents: write # for Update bundles

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Trigger BE build
      if: ${{ github.repository == 'Anuken/Mindustry' }}
      run: |
        git clone --depth=1 --branch=master https://github.com/Anuken/MindustryBuilds ../MindustryBuilds
        cd ../MindustryBuilds
        BNUM=$(($GITHUB_RUN_NUMBER + 20000))
        git tag ${BNUM}
        git config --global user.name "Github Actions"
        git push https://Anuken:${{ secrets.API_TOKEN_GITHUB }}@github.com/Anuken/MindustryBuilds ${BNUM}
    - name: Set up JDK 17
      uses: actions/setup-java@v1
      with:
        java-version: 17
    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2
    - name: Update bundles
      if: ${{ github.repository == 'Anuken/Mindustry' }}
      run: |
        ./gradlew updateBundles

        if [ -n "$(git status --porcelain)" ]; then
          git config --global user.name "Github Actions"
          git config --global user.email "actions@github.com"
          git add core/assets/bundles/*
          git commit -m "Automatic bundle update"
          git push
        fi
    - name: Update JITpack repo
      if: ${{ github.repository == 'Anuken/Mindustry' }}
      run: |
        git config --global user.name "Github Actions"
        git config --global user.email "actions@github.com"
        cd ../
        cp -r ./Mindustry ./MindustryJitpack
        cd MindustryJitpack
        git clone --depth 1 https://github.com/Anuken/MindustryJitpack.git
        rm -rf .git
        cp -r ./MindustryJitpack/.git ./.git
        rm -rf MindustryJitpack
        rm -rf .github
        rm README.md
        git add .
        git commit --allow-empty -m "Updating"
        git push https://Anuken:${{ secrets.API_TOKEN_GITHUB }}@github.com/Anuken/MindustryJitpack
        cd ../Mindustry
    - name: Run unit tests
      run: ./gradlew tests:test --rerun --stacktrace
