image: golang:1.23

stages:
  - test
  - sonarcloud
  - build

checks:
  stage: test
  script:
    - go fmt $(go list ./...)
    - go vet $(go list ./...)

code coverage:
  stage: test
  script:
    - go test -covermode=count -coverprofile coverage.cov $(go list ./...)
    - go tool cover -func=coverage.cov
    - go tool cover -html=coverage.cov -o coverage.html
  coverage: '/\(statements\)\W+\d+\.\d+%/'
  artifacts:
    paths:
      - coverage.cov
      - coverage.html

codecov.io:
  stage: test
  script:
    - curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --import
    - curl -Os https://uploader.codecov.io/latest/linux/codecov
    - curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
    - curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
    - gpg --verify codecov.SHA256SUM.sig codecov.SHA256SUM
    - shasum -a 256 -c codecov.SHA256SUM
    - chmod +x codecov
    - go test -race -coverprofile=coverage.out -covermode=atomic
    - ./codecov -t ${CODECOV_TOKEN}
  rules:
    - if: $CODECOV_TOKEN
      when: on_success

# stage "sonarcloud" is only needed because of this issue:
# https://gitlab.com/gitlab-org/gitlab/-/issues/30632
sonarcloud-check:
  stage: sonarcloud
  # result of coverage is needed
  needs:
    - code coverage
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner
  rules:
    - if: $SONAR_HOST_URL
      when: on_success

# build template
# execute the following command for all os/arch combinations: go tool dist list
.compile:
  stage: build
  # no dependencies -> no download of artifacts from previous jobs/stages
  dependencies: []
  script:
    - go build .

darwin-amd64:
  extends: .compile
  variables:
    GOOS: "darwin"
    GOARCH: "amd64"

darwin-arm64:
  extends: .compile
  variables:
    GOOS: "darwin"
    GOARCH: "arm64"

linux-amd64:
  extends: .compile
  variables:
    GOOS: "linux"
    GOARCH: "amd64"

linux-arm64:
  extends: .compile
  variables:
    GOOS: "linux"
    GOARCH: "arm64"

windows-amd64:
  extends: .compile
  variables:
    GOOS: "windows"
    GOARCH: "amd64"
