name: Build main target

on:
  push:
    branches:
      - master
      - develop
  pull_request:
    types: [synchronize, opened, reopened, ready_for_review]
    branches:
      - master
      - develop

jobs:
  build-and-test:
    name: Build and Test
    runs-on: macOS-latest
    if: github.event.pull_request.draft == false
    env:
      PROJECT: Nextcloud.xcodeproj
      DESTINATION: platform=iOS Simulator,name=iPhone 14
      SCHEME: Nextcloud
    steps:
    - name: Set env var
      run: echo "DEVELOPER_DIR=$(xcode-select --print-path)" >> $GITHUB_ENV
    - uses: actions/checkout@v3
    - name: Setup Bundler and Install Gems
      run: |
        gem install bundler
        bundle install
        bundle update
    - name: Restore Carhage Cache
      uses: actions/cache@v3
      id: carthage-cache
      with:
        path: Carthage
        key: ${{ runner.os }}-carthage-${{ hashFiles('**/Cartfile.resolved') }}
        restore-keys: |
          ${{ runner.os }}-carthage-
    - name: Carthage
      if: steps.carthage-cache.outputs.cache-hit != 'true'
      run: carthage bootstrap --use-xcframeworks --platform iOS
    - name: Download GoogleService-Info.plist
      run: wget "https://raw.githubusercontent.com/firebase/quickstart-ios/master/mock-GoogleService-Info.plist" -O GoogleService-Info.plist
    - name: Install docker
      run: |
        brew install docker
        colima start
    - name: Create docker test server and export enviroment variables
      run: |
        source ./create-docker-test-server.sh
        if [ ! -f ".env-vars" ]; then
            touch .env-vars
            echo "export TEST_SERVER_URL=$TEST_SERVER_URL" >> .env-vars
            echo "export TEST_USER=$TEST_USER" >> .env-vars
            echo "export TEST_APP_PASSWORD=$TEST_APP_PASSWORD" >> .env-vars
        fi
    - name: Build & Test Nextcloud iOS
      run: |
        set -o pipefail && xcodebuild test -project $PROJECT \
        -scheme "$SCHEME" \
        -destination "$DESTINATION" \
        -enableCodeCoverage YES \
        -test-iterations 3 \
        -retry-tests-on-failure \
        | xcpretty
    - name: Upload coverage to codecov
      run: |
        bundle exec slather
        bash <(curl -s https://codecov.io/bash) -f ./cobertura.xml -X coveragepy -X gcov -X xcode -t ${{ secrets.CODECOV_TOKEN }}

