name: generate_notices

permissions:
  pull-requests: write
  contents: write

on:
  pull_request:
    paths:
    - '.github/workflows/notice_generation.yml'
    - '.github/actions/notices_generation**'
  schedule:
    # Run every day at 2am (PST) - cron uses UTC times
    - cron:  '0 10 * * *'
jobs:
  generate_a_notice:
    # Don't run on private repo.
    if: github.repository == 'Firebase/firebase-ios-sdk' || github.event_name == 'workflow_dispatch'
    runs-on: macos-14
    name: Generate NOTICES
    env:
      # The path of NOTICES based on the root dir of repo."
      NOTICES_PATH: "CoreOnly/NOTICES"
    steps:
    - uses: actions/checkout@v4
    - name: Get all pod names
      run: |
        cd "${GITHUB_WORKSPACE}/ReleaseTooling/"
        swift run manifest --output-file-path ./output.txt --for-notices-generation
        PODS=`cat ./output.txt`
        echo "PODS=${PODS}" >> $GITHUB_ENV
        echo "NOTICES_PATH=${GITHUB_WORKSPACE}/${NOTICES_PATH}" >> $GITHUB_ENV
    - name: Create a local specs repo
      run: |
        cd "${GITHUB_WORKSPACE}/ReleaseTooling/"
        swift run podspecs-tester --git-root "${GITHUB_WORKSPACE}"
    - name: Create a NOTICES file
      id: notices
      uses: ./.github/actions/notices_generation/
      with:
        pods: ${{ env.PODS }}
        sources: "https://github.com/firebase/SpecsTesting,https://github.com/firebase/SpecsStaging,https://cdn.cocoapods.org"
        # This should match the highest minimum supported iOS version.
        min-ios-version: "15.0"
        search-local-pod-version: true
        notices-path:  ${{ env.NOTICES_PATH }}
    - name: Create a pull request
      run: |
        RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
        gem install octokit
        ruby scripts/create_pull_request.rb \
        --repo-root ${GITHUB_WORKSPACE} \
        --repo-token ${{ secrets.GITHUB_TOKEN }} \
        --target-path  ${{ env.NOTICES_PATH }} \
        --pr-title "NOTICES Change" \
        --pr-body "NOTICES Change is detected in [this GitHub Actions Run](${RUN_URL})." \
        --commit-comment "NOTICES change."
      shell: bash
