#
# You can call this workflow in your repository. See the example caller workflow:
#
# --------------------------------------------------------------------------------------
# name: My License Check Caller
# on: pull_request
# jobs:
#   call-workflow:
#     uses: nrfconnect/sdk-nrf/.github/workflows/license-reusable.yml@main
#     with:
#       path: my_module_path
#       license_allow_list: my_module_path/license_allow_list.yaml
#         # You can find more details about the inputs below.
# --------------------------------------------------------------------------------------

name: Reusable License Check

on:
  workflow_call:
    inputs:
      path:
        # Required path to your module relative to west workspace.
        type: string
        required: true

      license_allow_list:
        # Optional path to your custom license allow list file relative to west workspace.
        # By default, the list form the "sdk-nrf" repository is used. For details about
        # format of the list, see "scripts/ci/license_allow_list.yaml" in the
        # "sdk-nrf" repository.
        type: string
        default: nrf/scripts/ci/license_allow_list.yaml

jobs:
  license_job:
    runs-on: ubuntu-latest
    name: Run license checks on patch series (PR)
    steps:
    - name: Checkout sources
      uses: nrfconnect/action-checkout-west-update@main
      with:
        git-fetch-depth: 0
        git-ref: ${{ github.event.pull_request.head.sha }}
        path: ncs/${{ inputs.path }}
        west-update-args: '-n zephyr bsim'

    - name: cache-pip
      uses: actions/cache@v3
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-doc-pip

    - name: Install python dependencies
      run: |
        pip3 install -U pip
        pip3 install -U setuptools
        export PATH="$HOME/.local/bin:$PATH"
        pip3 install -U wheel
        pip3 install --user -U west
        pip3 show -f west

    - name: Git rebase
      env:
        BASE_REF: ${{ github.base_ref }}
      working-directory: ncs/${{ inputs.path }}
      run: |
        git remote -v
        git branch
        git rebase origin/${BASE_REF}
        # debug
        git log --pretty=oneline | head -n 10

    - name: West zephyr-export
      working-directory: ncs
      env:
        PR_REF: ${{ github.event.pull_request.head.sha }}
      run: |
        export PATH="$HOME/.local/bin:$PATH"
        export PATH="$HOME/bin:$PATH"
        west zephyr-export
        echo "ZEPHYR_BASE=$(pwd)/zephyr" >> $GITHUB_ENV
        # debug
        ( cd ${{ inputs.path }}; echo "${{ inputs.path }}"; git log --pretty=oneline --max-count=10 )
        ( cd nrf; echo "nrf"; git log --pretty=oneline --max-count=10 )
        ( cd zephyr; echo "zephyr"; git log --pretty=oneline --max-count=10 )

    - name: Install license check script dependencies
      run: |
        pip3 install -U -r ncs/nrf/scripts/ci/requirements.txt

    - name: Run license checks
      id: license_checks
      env:
        BASE_REF: ${{ github.base_ref }}
        ZEPHYR_BASE: ${{ env.ZEPHYR_BASE }}
      working-directory: ncs/${{ inputs.path }}
      if: contains(github.event.pull_request.user.login, 'dependabot[bot]') != true
      run: |
        export PATH="$HOME/.local/bin:$PATH"
        export PATH="$HOME/bin:$PATH"
        ${ZEPHYR_BASE}/../nrf/scripts/ci/check_license.py \
            --github \
            -l ${ZEPHYR_BASE}/../${{ inputs.license_allow_list }} \
            -c origin/${BASE_REF}..

    - name: Upload results
      uses: actions/upload-artifact@v3
      continue-on-error: true
      if: always() && contains(github.event.pull_request.user.login, 'dependabot[bot]') != true
      with:
        name: licenses.xml
        path: ncs/${{ inputs.path }}/licenses.xml
