# yaml-language-server: $schema=https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json

stages:
  - checks
  - build

variables:
  CGO_ENABLED: 0
  COVERAGEREPORTDIR: "_build/coverage"

default:
  image: golang:1.24

## -- Check stage -------------------------------------------------------------

lint:
  stage: checks
  script:
    - go tool golangci-lint run

govulncheck:
  stage: checks
  script:
    - go mod tidy
    - go tool govulncheck -show color ./...

# A test job template, to make different test jobs by branch/tags
.go test: &_go_test_template
  stage: checks
  variables:
    GOTESTFLAGS: -v
  script:
    - mkdir -p $COVERAGEREPORTDIR
    - go test -cover --covermode=atomic --coverprofile=$COVERAGEREPORTDIR/main.cover ./...
    - go tool cover -func $COVERAGEREPORTDIR/main.cover
    - go tool cover -html $COVERAGEREPORTDIR/main.cover -o $COVERAGEREPORTDIR/main.html
  coverage: /^total:\s*\(statements\)\s*(\d+.\d+)\%/

# Coverage on branches are only kept for 1 week
go test:
  <<: *_go_test_template
  only:
    - branches
  artifacts:
    paths:
      - $COVERAGEREPORTDIR/
    expire_in: 1 week

# Coverage results on tags are kept much longer (1 year)
go test release:
  <<: *_go_test_template
  only:
    - tags
  artifacts:
    paths:
      - $COVERAGEREPORTDIR/
    expire_in: 1 year

## -- Build stage -------------------------------------------------------------

dev build:
  stage: build
  image:
    name: goreleaser/goreleaser
    entrypoint: [""]
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
  rules:
    - if: $CI_COMMIT_BRANCH
  before_script:
    - apk add --update docker-cli
  script:
    - goreleaser release --clean --snapshot --skip=publish
  artifacts:
    paths:
      - .build/dist/
    expire_in: 1 week

release:
  stage: build
  image:
    name: goreleaser/goreleaser
    entrypoint: [""]
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
    # Disable shallow cloning so that goreleaser can diff between tags to
    # generate a changelog.
    GIT_DEPTH: 0
  rules:
    - if: $CI_COMMIT_TAG && '$CI_COMMIT_TAG =~ /^v\d+.\d+.\d+$/'
  before_script:
    - apk add --update py3-pip jq docker-cli
    - pip install --upgrade --break-system-packages cloudsmith-cli
    - echo "$DOCKER_HUB_TOKEN" | docker login --username $DOCKER_HUB_USER --password-stdin
  script:
    - goreleaser release --clean
    # Uploading RAW artifacts on cloudsmith is done sequentially outside Goreleaser, as it seems there is some bug
    # when goreleaser do it in parallel mode (only for raw artifact, no issue with linux packages
    - test -f .build/dist/metadata.json
    - test -f .build/dist/artifacts.json
    - export VERSION="$(cat .build/dist/metadata.json | jq -r '.version')"
    - |
      cat .build/dist/artifacts.json | jq -r '[.[] | select((.type == "Binary") and (.extra.ID=="gitlab-ci-linter"))] | sort_by(.goos, .goarch) | .[] | [.goos, .goarch, .path] | @tsv' | while read GOOS GOARCH ARTIFACT_PATH
      do
        echo "-> Uploading artifact $ARTIFACT_PATH $VERSION for ${GOOS}/${GOARCH}"
        cloudsmith push raw --republish --version "$VERSION" --name "gitlab-ci-linter_${GOOS}_${GOARCH}" --tags "${GOOS},${GOARCH}" orobardet/gitlab-ci-linter "$ARTIFACT_PATH" || exit 1
      done
