name: Electron Lint

on:
  workflow_call:
    inputs:
      container:
        required: true
        description: 'Container to run lint in'
        type: string

permissions: {}

concurrency:
  group: electron-lint-${{ github.ref_protected == true && github.run_id || github.ref }}
  cancel-in-progress: ${{ github.ref_protected != true }}

env:
  CHROMIUM_GIT_COOKIE: ${{ secrets.CHROMIUM_GIT_COOKIE }}  

jobs:
  lint:
    name: Lint
    runs-on: electron-arc-centralus-linux-amd64-4core
    permissions:
      contents: read
    timeout-minutes: 20
    container: ${{ fromJSON(inputs.container) }}
    steps:
    - name: Checkout Electron
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
      with:
        path: src/electron
        fetch-depth: 0
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Install Dependencies
      uses: ./src/electron/.github/actions/install-dependencies
    - name: Set Chromium Git Cookie
      uses: ./src/electron/.github/actions/set-chromium-cookie
    - name: Setup third_party Depot Tools
      shell: bash
      run: |
        # "depot_tools" has to be checkout into "//third_party/depot_tools" so pylint.py can a "pylintrc" file.
        git clone --filter=tree:0 https://chromium.googlesource.com/chromium/tools/depot_tools.git src/third_party/depot_tools
        echo "$(pwd)/src/third_party/depot_tools" >> $GITHUB_PATH
    - name: Download GN Binary
      shell: bash
      run: |
        chromium_revision="$(grep -A1 chromium_version src/electron/DEPS | tr -d '\n' | cut -d\' -f4)"
        if [[ ! "$chromium_revision" =~ ^[a-zA-Z0-9._-]+$ ]]; then
          echo "::error::Invalid chromium_revision: $chromium_revision"
          exit 1
        fi
        gn_version="$(curl -sL "https://raw.githubusercontent.com/chromium/chromium/refs/tags/${chromium_revision}/DEPS" | grep gn_version | head -n1 | cut -d\' -f4)"

        cipd ensure -ensure-file - -root . <<-CIPD
        \$ServiceURL https://chrome-infra-packages.appspot.com/
        @Subdir src/buildtools/linux64
        gn/gn/linux-amd64 $gn_version
        CIPD

        buildtools_path="$(pwd)/src/buildtools"
        echo "CHROMIUM_BUILDTOOLS_PATH=$buildtools_path" >> $GITHUB_ENV
    - name: Download clang-format Binary
      shell: bash
      run: |
        chromium_revision="$(grep -A1 chromium_version src/electron/DEPS | tr -d '\n' | cut -d\' -f4)"
        if [[ ! "$chromium_revision" =~ ^[a-zA-Z0-9._-]+$ ]]; then
          echo "::error::Invalid chromium_revision: $chromium_revision"
          exit 1
        fi

        mkdir -p src/buildtools
        curl -sL "https://raw.githubusercontent.com/chromium/chromium/refs/tags/${chromium_revision}/buildtools/DEPS" > src/buildtools/DEPS

        gclient sync --spec="solutions=[{'name':'src/buildtools','url':None,'deps_file':'DEPS','custom_vars':{'process_deps':True},'managed':False}]"
    - name: Add problem matchers
      shell: bash
      run: |
        echo "::add-matcher::src/electron/.github/problem-matchers/eslint-stylish.json"
        echo "::add-matcher::src/electron/.github/problem-matchers/markdownlint.json"
    - name: Run Lint
      shell: bash
      run: |
        # gn.py tries to find a gclient root folder starting from the current dir.
        # When it fails and returns "None" path, the whole script fails. Let's "fix" it.
        touch .gclient
        # Another option would be to checkout "buildtools" inside the Electron checkout,
        # but then we would lint its contents (at least gn format), and it doesn't pass it.

        cd src/electron
        node script/yarn.js install --immutable
        node script/yarn.js lint
    - name: Run Script Typechecker
      shell: bash
      run: |
        cd src/electron
        node script/yarn.js tsc -p tsconfig.script.json
    - name: Check GHA Workflows
      shell: bash
      run: |
        cd src/electron
        node script/copy-pipeline-segment-publish.js --check
