name: Ubuntu

on:
  push:
  pull_request:

jobs:
  test:
    name: Test on Python ${{ matrix.python-version }} and ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: [ 3.7, 3.8, 3.9, '3.10' ]
        os: [ ubuntu-latest ]
    env:
      APKTOOL_VERSION: 2.6.0
      BUILD_TOOLS_VERSION: 32.0.0

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          architecture: x64

      - name: Install Python packages
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r ./src/requirements.txt

      - name: Install dependencies
        run: |
          # Install Apktool.
          sudo wget -q https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/linux/apktool -O /usr/local/bin/apktool
          sudo chmod a+x /usr/local/bin/apktool
          sudo wget -q "https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_${APKTOOL_VERSION}.jar" -O /usr/local/bin/apktool.jar
          sudo chmod a+x /usr/local/bin/apktool.jar
          # Install BundleDecompiler.
          sudo wget -q https://raw.githubusercontent.com/TamilanPeriyasamy/BundleDecompiler/master/build/libs/BundleDecompiler-0.0.2.jar -O /usr/local/bin/BundleDecompiler.jar
          sudo chmod a+x /usr/local/bin/BundleDecompiler.jar

      - name: Run tests
        run: |
          export PATH="${PATH}:${ANDROID_HOME}/build-tools/${BUILD_TOOLS_VERSION}"
          pytest --verbose --cov=./ --cov-report xml

      - name: Upload tests coverage to Codacy
        if: contains(github.event_name, 'pull_request') == false
        env:
          CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
        run: |
          bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l Python -r ./coverage.xml

      - name: Upload tests coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          fail_ci_if_error: false

  publish:
    # Run only if the previous job succeeds (only master branch or new tags, no pull requests).
    if: (endsWith(github.ref, '/master') || startsWith(github.ref, 'refs/tags/v')) && contains(github.event_name, 'pull_request') == false
    needs: test
    name: Push updated version to Docker Hub
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: claudiugeorgiu
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: claudiugeorgiu/obfuscapk
          tags: |
            type=raw,value=latest
            type=semver,pattern={{version}}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: ./src/
          push: true
          tags: ${{ steps.meta.outputs.tags }}
