# `rules: if $CI_PIPELINE_SOURCE == 'merge_request_event'` seems to convert the whole
# workflow into a merge request pipeline, effectively removing all the jobs.
# To work around that, add:
# ```
# rules:
#   when: on_success
# ```
# To your pipeline.
# https://docs.gitlab.com/ee/ci/pipelines/merge_request_pipelines.html#use-rules-to-add-jobs
include:
  - local: /backend/backend/.gitlab-ci.yml
  - local: /frontend/app/.gitlab-ci.yml
  - local: /.gitlab/schedules.gitlab-ci.yml
  - local: /.gitlab/publishing.gitlab-ci.yml
    rules:
      - if: '$CI_COMMIT_MESSAGE =~ /ci: publish release$/'
        when: always

variables:
  TURBO_CACHE_BYPASS:
    value: ''
    options:
      - '--force'
      - '--no-cache'
      - ''
    description: Bypass turbo cache

default:
  image: registry.gitlab.com/openstapps/openstapps/node-builder
  tags:
    - performance
  interruptible: true
  before_script:
    - corepack enable
    - corepack prepare pnpm@latest-8 --activate
    - echo TURBO_API=$TURBO_API >> .env.local
    - echo TURBO_TOKEN=$TURBO_TOKEN >> .env.local
    - echo TURBO_TEAM=$TURBO_TEAM >> .env.local
    - pnpm config set store-dir .pnpm-store
  cache: &pnpm_cache
    key:
      files:
          - pnpm-lock.yaml
    paths:
      - .pnpm-store/
    policy: pull

stages:
  - setup
  - build
  - test
  - audit
  - publish

build:
  stage: build
  script:
    # - pnpm config set recursive-install false
    # - pnpm i --prefer-offline
    # - pnpm build:full:skip || pnpm i -r --prefer-offline
    - pnpm install
    # TODO: whats the benifit for CI? something doesn't work with git here...
    # "🦋  error Error: Failed to find where HEAD diverged from master. Does master exist?"
    # - pnpm changeset:status
    - pnpm build:full $TURBO_CACHE_BYPASS
    - .gitlab/ci/enableGitlabReviewToolbar.sh frontend/app/www/index.html "$CI_PROJECT_ID" "$CI_OPEN_MERGE_REQUESTS"
    - cp frontend/app/www/index.html frontend/app/www/200.html
    - pnpm dlx surge -p frontend/app/www -d https://$CI_PROJECT_NAME-$DEPLOY_ID.surge.sh/
  cache:
    <<: *pnpm_cache
    policy: pull-push
  environment:
    name: review/$DEPLOY_ID
    url: https://$CI_PROJECT_NAME-$DEPLOY_ID.surge.sh/
    on_stop: stop review
  rules: &deploy-rules
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      variables:
        DEPLOY_ID: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
    - if: $CI_COMMIT_BRANCH == 'main'
      variables:
        DEPLOY_ID: production
    - if: $CI_COMMIT_BRANCH == 'develop'
      variables:
        DEPLOY_ID: staging

stop review:
  stage: build
  needs: ['build']
  script:
    - pnpm dlx surge teardown $CI_PROJECT_NAME-$DEPLOY_ID.surge.sh
  environment:
    name: review/$DEPLOY_ID
    action: stop
  when: manual
  rules: *deploy-rules

unit:
  image: registry.gitlab.com/openstapps/openstapps/app-builder
  stage: test
  needs: ['build']
  script:
    # - pnpm config set recursive-install false
    # - pnpm i --prefer-offline
    # - pnpm test:skip || pnpm i -r --prefer-offline
    - pnpm install
    - cd node_modules/.pnpm/re2*/node_modules/re2
    - npm run install
    - cd $CI_PROJECT_DIR
    - pnpm test $TURBO_CACHE_BYPASS
  coverage: '/Statements[^:]*\:[^:]*\s+([\d\.]+)%/'
  artifacts:
    when: on_failure
    paths:
      - report-junit.xml
      - coverage.xml
    reports:
      junit:
        - report-junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' || $CI_COMMIT_BRANCH == 'main' || $CI_COMMIT_BRANCH == 'develop'

audit:
  stage: audit
  needs: []
  script:
    - pnpm audit --prod
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' || $CI_COMMIT_BRANCH == 'develop'
      allow_failure: true
    - if: $CI_COMMIT_BRANCH == 'main'
      allow_failure: false
