when:
  - event: [pull_request]
  - event: push
    branch:
      - ${CI_REPO_DEFAULT_BRANCH}

services:
  database:
    image: postgres:14
    pull: true
    environment:
      POSTGRES_DB: twhelp
      POSTGRES_PASSWORD: twhelp
  rmq:
    image: rabbitmq:3.12.10-alpine
    environment:
      RABBITMQ_DEFAULT_USER: twhelp
      RABBITMQ_DEFAULT_PASS: twhelp

variables:
  - &go_image 'golang:1.22'

steps:
  generate:
    image: *go_image
    pull: true
    commands:
      - go mod vendor
      - make generate

  test:
    image: *go_image
    depends_on:
      - generate
    pull: true
    environment:
      TESTS_POSTGRES_CONNECTION_STRING:
        postgres://postgres:twhelp@database:5432/twhelp?sslmode=disable
      TESTS_RABBITMQ_CONNECTION_STRING:
        amqp://twhelp:twhelp@rmq:5672/
    commands:
      - go test -race -coverprofile=coverage.txt -covermode=atomic ./...

  lint:
    image: golangci/golangci-lint:v1.57
    pull: true
    depends_on:
      - generate
    commands:
      - golangci-lint run

  check-go-mod:
    image: *go_image
    depends_on:
      - generate
    pull: true
    commands:
      - go mod tidy
      - git diff --exit-code go.mod

  hadolint:
    image: hadolint/hadolint:2.12.0-debian
    commands:
      - hadolint build/docker/twhelp/prod/Dockerfile build/docker/twhelp/dev/Dockerfile
    when:
      - path:
          - build/docker/twhelp/prod/Dockerfile
          - build/docker/twhelp/dev/Dockerfile

  yamllint:
    image: cytopia/yamllint:1
    pull: true
    commands:
      - yamllint --strict .
    when:
      - path:
          include:
            - "**/*.yaml"
            - "**/*.yml"
