# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: combine

permissions:
  contents: read

on:
  workflow_dispatch:
  pull_request:
    paths:
    # Combine sources
    - 'FirebaseCombineSwift/**'

    # Podspecs
    - 'FirebaseCombineSwift.podspec'

    # This workflow
    - '.github/workflows/combine.yml'

    # Rebuild on Ruby infrastructure changes.
    - 'Gemfile'

    # Dependencies
    - 'FirebaseCore/**'
    - 'FirebaseTestingSupport/**'
    - 'FirebaseAuth/**'
    - 'FirebaseFunctions/**'
    - 'FirebaseStorage/**'
    # - 'Firestore/**' # (Disabled to avoid building Firestore in presubmits)

  schedule:
    # Run every day at 12am (PDT) / 3am (EDT) - cron uses UTC times
    - cron:  '0 7 * * *'

concurrency:
    group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
    cancel-in-progress: true

jobs:
  xcodebuild:
    # Don't run on private repo unless it is a PR.
    if: (github.repository == 'Firebase/firebase-ios-sdk' && github.event_name == 'schedule') || contains(fromJSON('["pull_request", "workflow_dispatch"]'), github.event_name)
    runs-on: macos-15

    strategy:
      matrix:
        target: [iOS]

    steps:
    - uses: actions/checkout@v4
    - uses: mikehardy/buildcache-action@c87cea0ccd718971d6cc39e672c4f26815b6c126
      with:
        cache_key: ${{ matrix.os }}

    - uses: ruby/setup-ruby@354a1ad156761f5ee2b7b13fa8e09943a5e8d252 # v1

    - name: Xcode
      run: sudo xcode-select -s /Applications/Xcode_16.4.app/Contents/Developer

    - name: Install xcpretty
      run: gem install xcpretty

    - name: Setup build
      run:  scripts/install_prereqs.sh CombineSwift ${{ matrix.target }} xcodebuild

    - name: Build and test
      run:  scripts/third_party/travis/retry.sh scripts/build.sh CombineSwift ${{ matrix.target }} xcodebuild

  storage-combine-integration:
    # Don't run on private repo unless it is a PR.
    if: (github.repository == 'Firebase/firebase-ios-sdk' && github.event_name == 'schedule') || contains(fromJSON('["pull_request", "workflow_dispatch"]'), github.event_name)
    env:
      plist_secret: ${{ secrets.GHASecretsGPGPassphrase1 }}
    runs-on: macos-15
    steps:
    - uses: actions/checkout@v4
    - uses: mikehardy/buildcache-action@c87cea0ccd718971d6cc39e672c4f26815b6c126
      with:
        cache_key: ${{ matrix.os }}
    - uses: ruby/setup-ruby@354a1ad156761f5ee2b7b13fa8e09943a5e8d252 # v1
    - name: Xcode
      run: sudo xcode-select -s /Applications/Xcode_16.4.app/Contents/Developer
    - name: Setup Bundler
      run: scripts/setup_bundler.sh
    - name: Install xcpretty
      run: gem install xcpretty
    - name: Install Secret GoogleService-Info.plist
      run: scripts/decrypt_gha_secret.sh scripts/gha-encrypted/storage-db-plist.gpg \
          FirebaseStorage/Tests/Integration/Resources/GoogleService-Info.plist "$plist_secret"
    - name: Install Credentials.h
      run: scripts/decrypt_gha_secret.sh scripts/gha-encrypted/Storage/Credentials.h.gpg \
          FirebaseStorage/Tests/ObjCIntegration/Credentials.h "$plist_secret"
    - name: Install Credentials.swift
      run: |
        scripts/decrypt_gha_secret.sh scripts/gha-encrypted/Storage/Credentials.swift.gpg \
          FirebaseStorage/Tests/Integration/Credentials.swift "$plist_secret"
    - name: BuildAndTest # can be replaced with pod lib lint with CocoaPods 1.10
      run: ([ -z $plist_secret ] || scripts/third_party/travis/retry.sh scripts/build.sh StorageCombine all)
