include:
  - project: 'fit-connect/pipeline'
    ref: main
    file: 'reuse.gitlab-ci.yml'

stages:
  - lint
  - test
  - build
  - nuget-pack

reuse:
  extends: .reuse
  variables:
    DOCKER_REGISTRY_READ: $DOCKER_PULL_REGISTRY

build:
  stage: build
  image: mcr.microsoft.com/dotnet/sdk:6.0
  script:
    - cp $CREDENTIALS ./Tests/MockContainer/credentials.json
    - dotnet build FitConnect

nuget-pack:
  only:
    - tags
  stage: nuget-pack
  image: mcr.microsoft.com/dotnet/sdk:6.0
  artifacts:
    paths:
      - ./artifacts
    expire_in: 30 days
  script:
    - cp $CREDENTIALS ./Tests/MockContainer/credentials.json
    - GIT_COMMIT_ID=$CI_COMMIT_SHORT_SHA
    - sed -i -e "s|<PackageVersion>.*</PackageVersion>|<PackageVersion>$CI_COMMIT_TAG</PackageVersion>|" FitConnect/FitConnect.csproj
    - FILE_VERSION=$(echo "$CI_COMMIT_TAG" | sed 's/\-.*//')
    - sed -i -e "s|<AssemblyVersion>.*</AssemblyVersion>|<AssemblyVersion>$FILE_VERSION</AssemblyVersion>|" FitConnect/FitConnect.csproj
    - sed -i -e "s|<FileVersion>.*</FileVersion>|<FileVersion>$FILE_VERSION</FileVersion>|" FitConnect/FitConnect.csproj
    - sed -i -e "s|public const string PackageVersion = \".*\";|public const string PackageVersion = \"$CI_COMMIT_TAG\";|" FitConnect/ProjectSpecification.cs
    - sed -i -e "s|public const string CommitId = \".*\";|public const string CommitId = \"$CI_COMMIT_SHORT_SHA\";|" FitConnect/ProjectSpecification.cs
    - dotnet pack FitConnect -c Release -o ./artifacts
    - dotnet nuget push ./artifacts/FitConnect.$CI_COMMIT_TAG.nupkg --api-key $NUGET_API_KEY -s https://api.nuget.org/v3/index.json

test:
  stage: test
  image: mcr.microsoft.com/dotnet/sdk:6.0
  script:
    - cp $CREDENTIALS ./Tests/MockContainer/credentials.json
    - dotnet test Tests/E2ETest
    - dotnet test Tests/IntegrationTests
    - dotnet test Tests/BasicUnitTest

