name: Coverage

on:
  push:
    branches:
      - "main"
  pull_request:
    types: [opened, reopened, synchronize]
    branches:
      - "main"

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

jobs:
  compliance:
    name: "License Compliance"
    runs-on: ubuntu-24.04
    steps:
      - name: "Checkout Code"
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - name: Check secret
        id: checksecret
        uses: ./.github/actions/exists
        with:
          value: ${{ secrets.FOSSA_API_KEY }}
      - name: "Run FOSSA Scan"
        if: steps.checksecret.outputs.result == 'true'
        uses: fossas/fossa-action@29693cc50323968e039056be419b32989fc5880c # v2.0.0
        with:
          api-key: ${{ secrets.FOSSA_API_KEY }}
      - name: "Run FOSSA Test"
        if: steps.checksecret.outputs.result == 'true'
        uses: fossas/fossa-action@29693cc50323968e039056be419b32989fc5880c # v2.0.0
        with:
          api-key: ${{ secrets.FOSSA_API_KEY }}
          run-tests: true
  sast:
    name: "SAST"
    runs-on: ubuntu-24.04
    env:
      GO111MODULE: on
    permissions:
      security-events: write
      actions: read
      contents: read
    steps:
      - name: Checkout Source
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
        with:
          go-version-file: 'go.mod'
      - name: Run Gosec Security Scanner
        uses: securego/gosec@9e75c0576c9878035d4221392108d458abe10fc3 # v2.28.0
        with:
          args: '-no-fail -fmt sarif -out gosec.sarif ./...'
      - name: Upload SARIF file
        uses: github/codeql-action/upload-sarif@ff2f1c621b7f889edc0d3c761ac2e6a3f8cdb0dd # v4.37.7
        with:
          sarif_file: gosec.sarif
  unit_tests:
    name: "Unit tests"
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
        with:
          go-version-file: 'go.mod'
      - name: Unit Test
        run: make test
      - name: Check secret
        id: checksecret
        uses: ./.github/actions/exists
        with:
          value: ${{ secrets.CODECOV_TOKEN }}
      - name: Upload Report to Codecov
        if: ${{ steps.checksecret.outputs.result == 'true' }}
        uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: projectcapsule/capsule
          files: ./coverage.out
          fail_ci_if_error: true
          verbose: true
