---
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2026 The Linux Foundation
name: Gerrit Verify - Unit Tests

# yamllint disable-line rule:truthy
on:
  workflow_call:
    inputs:
      # Gerrit-to-Github defaults
      GERRIT_BRANCH:
        description: "Branch that change is against"
        required: true
        type: string
      GERRIT_CHANGE_ID:
        description: "The ID for the change"
        required: true
        type: string
      GERRIT_CHANGE_NUMBER:
        description: "The Gerrit number"
        required: true
        type: string
      GERRIT_CHANGE_URL:
        description: "URL to the change"
        required: true
        type: string
      GERRIT_EVENT_TYPE:
        description: "Type of Gerrit event"
        required: true
        type: string
      GERRIT_PATCHSET_NUMBER:
        description: "The patch number for the change"
        required: true
        type: string
      GERRIT_PATCHSET_REVISION:
        description: "The revision sha"
        required: true
        type: string
      GERRIT_PROJECT:
        description: "Project in Gerrit"
        required: true
        type: string
      GERRIT_REFSPEC:
        description: "Gerrit refspec of change"
        required: true
        type: string
      # Workflow-specific inputs
      UNIT_TEST_TARGETS:
        description: "List of targets in json-formatted string"
        required: false
        type: string
        default: '["lint", "sca", "test"]'
      DEST_GOPATH:
        description: "Specifies part of the GOPATH. Usually set to Github org."
        required: false
        type: string
        default: "github.com/opencord"
      GOPROXY:
        description: "Url for Go proxy"
        required: false
        type: string
        default: "https://proxy.golang.org"
    secrets:
      GERRIT_SSH_PRIVKEY:
        description: "SSH Key for the authorized user account"
        required: true

concurrency:
  # yamllint disable-line rule:line-length
  group: verify-unit-tests-${{ github.workflow }}-${{ github.event.inputs.GERRIT_CHANGE_ID || github.run_id }}
  cancel-in-progress: true

jobs:
  prepare:
    runs-on: ubuntu-latest
    steps:
      - name: Clear votes
        # yamllint disable-line rule:line-length
        uses: lfreleng-actions/gerrit-review-action@c4157b2200e1c2464a52f741c01ff85909c62811
        with:
          host: ${{ vars.GERRIT_SERVER }}
          username: ${{ vars.GERRIT_SSH_USER }}
          key: ${{ secrets.GERRIT_SSH_PRIVKEY }}
          known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }}
          gerrit-change-number: ${{ inputs.GERRIT_CHANGE_NUMBER }}
          gerrit-patchset-number: ${{ inputs.GERRIT_PATCHSET_NUMBER }}
          vote-type: clear
          comment-only: false
      - name: Allow replication
        run: sleep 10s

  unit-tests:
    needs: prepare
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: ${{ fromJSON(inputs.UNIT_TEST_TARGETS) }}
    steps:
      - name: Gerrit Checkout
        # yamllint disable-line rule:line-length
        uses: lfit/checkout-gerrit-change-action@fc57a3dd3318f31bd34d435d134e30443593eb80 # v1.0.1
        with:
          gerrit-refspec: ${{ inputs.GERRIT_REFSPEC }}
          gerrit-project: ${{ inputs.GERRIT_PROJECT }}
          gerrit-url: ${{ vars.GERRIT_URL }}
          delay: "0s"
          submodules: "true"
          fetch-depth: 0
      # yamllint disable-line rule:line-length
      - name: Install python
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.10"
      - name: Install virtualenv
        run: pip install virtualenv
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod
      - name: Check License Headers
        uses: ./.github/actions/license-check-action
      - name: Run unit tests
        env:
          DEST_GOPATH: ${{ inputs.DEST_GOPATH }}
          UNIT_TEST_TARGETS: ${{ matrix.target }}
          GOPROXY: ${{ inputs.GOPROXY }}
          TEST_PROJECT: ${{ github.repository }}
          WORKSPACE: ${{ github.workspace }}
        run: |
          make SHELL="/bin/bash" ${{ matrix.target }}

  vote:
    if: ${{ always() }}
    # yamllint enable rule:line-length
    needs: [prepare, unit-tests]
    runs-on: ubuntu-latest
    steps:
      - name: Get conclusion
        # yamllint disable-line rule:line-length
        uses: im-open/workflow-conclusion@e4f7c4980600fbe0818173e30931d3550801b992 # v2.2.3
      - name: Set vote
        # yamllint disable-line rule:line-length
        uses: lfreleng-actions/gerrit-review-action@c4157b2200e1c2464a52f741c01ff85909c62811
        with:
          host: ${{ vars.GERRIT_SERVER }}
          username: ${{ vars.GERRIT_SSH_USER }}
          key: ${{ secrets.GERRIT_SSH_PRIVKEY }}
          known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }}
          gerrit-change-number: ${{ inputs.GERRIT_CHANGE_NUMBER }}
          gerrit-patchset-number: ${{ inputs.GERRIT_PATCHSET_NUMBER }}
          vote-type: ${{ env.WORKFLOW_CONCLUSION }}
          comment-only: false
