---
name: CI

on:
  push:
    branches:
      - main
      - release-4.*
  pull_request:
    branches:
      - main
      - release-4.*

jobs:
  commit-check:
    name: Commit Check
    runs-on: ubuntu-latest
    steps:
      - name: commit check
        uses: gsactions/commit-message-checker@v1
        with:
          pattern: |
            ^(.*):\s*(.*)\n.*$
          error: 'Commit must begin with <scope>: <subject>'
          flags: 'gm'
          excludeTitle: true
          excludeDescription: true
          checkAllCommitMessages: true
          accessToken: ${{ secrets.GITHUB_TOKEN }}

  api-reference-check:
    name: API Reference Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: gen api reference
        run: |
          npx widdershins --search false --language_tabs 'python:Python' 'go:Golang' 'javascript:Javascript' --summary ./openapi.yaml -o ./Documentation/reference/api.md
      - name: diff
        run: |
          git diff --exit-code

  tidy:
    name: Tidy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go: ['1.16']
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
      - uses: actions/checkout@v3
      - uses: ./.github/actions/go-tidy
        with:
          go: ${{ matrix.go }}

  documentation:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/documentation
        with:
          publish: false

  tests:
    name: Tests
    runs-on: ubuntu-latest
    container: quay.io/projectquay/golang:${{ matrix.go }}
    env:
      POSTGRES_CONNECTION_STRING: "host=clair-db port=5432 user=clair dbname=clair sslmode=disable"
      RABBITMQ_CONNECTION_STRING: "amqp://guest:guest@clair-rabbitmq:5672/"
      STOMP_CONNECTION_STRING: "clair-activemq:61613"
    services:
      clair-db:
        image: postgres:11.5
        env:
          POSTGRES_USER: "clair"
          POSTGRES_DB: "clair"
          POSTGRES_INITDB_ARGS: "--no-sync"
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      clair-rabbitmq:
        image: rabbitmq:3.8.5
        env:
          RABBITMQ_VM_MEMORY_HIGH_WATERMARK: "85%"
      clair-activemq:
        image: webcenter/activemq:5.14.3

    strategy:
      matrix:
        go: ['1.16', '1.17']
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/go-cache
        with:
          go: ${{ matrix.go }}
      - run: go test -tags integration ./...
      - uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: workspace-${{matrix.go}}
          path: ${{ github.workspace }}
