name: Test
on:
  # Trigger the workflow on push,
  # but only for the master branch
  push:
    branches:
      - master
    
permissions: read-all

jobs:
  test:
    name: Testing
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Unpack the required data for the tests
        run: |
          gpg --quiet --batch --yes --decrypt --passphrase="$TESTING_PASSPHRASE" --output bin/testing_required_data.zip bin/testing_required_data.zip.gpg
          sudo apt install unzip
          unzip -q bin/testing_required_data.zip -d bin
        env:
          TESTING_PASSPHRASE: ${{ secrets.TESTING_PASSPHRASE }}
      - name: Test the code
        run: "docker run --rm -u root -v ${PWD}:/home/m2gba/src lorenzooone/m2gba_translation:tester"
      - name: Archive test results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: test-report
          path: out/test.log

  issue_creation:
    name: Create issue on failure
    runs-on: ubuntu-latest
    permissions:
      issues: write
    needs: test
    if: failure()
    steps:
      - name: Download a single artifact
        uses: actions/download-artifact@v3
        with:
          name: test-report
      - name: Create the issue
        uses: peter-evans/create-issue-from-file@v4
        with:
          title: Test failure
          content-filepath: test.log
          labels: |
            report
            automated issue
