image: python:latest
stages:
  - tests
  - diffs
  - export

variables:
  # By default, use the master branch of the main KLU repository
  # You can override this in your projects GitLab UI
  KICAD_LIBRARY_UTILS_REPO: https://gitlab.com/kicad/libraries/kicad-library-utils.git
  KICAD_LIBRARY_UTILS_BRANCH: master

default:
  before_script:
    - echo "Cloning ${KICAD_LIBRARY_UTILS_REPO} branch ${KICAD_LIBRARY_UTILS_BRANCH}"
    - git clone --depth 1 "${KICAD_LIBRARY_UTILS_REPO}" $CI_BUILDS_DIR/kicad-library-utils --branch "${KICAD_LIBRARY_UTILS_BRANCH}"
    - git config --global --add safe.directory $(realpath .)

klc-check:
  stage: tests
  needs: []
  allow_failure:
    exit_codes:
     - 2  # KLC checks have warnings
  only:
    refs:
      - merge_requests
    changes:
      - "*.kicad_sym"
      - "sym-lib-table"
  script:
    - python3 -V
    - pip install pytest
    # Don't let Gitlab force return code 1 on error
    - set +e
    - $CI_BUILDS_DIR/kicad-library-utils/tools/gitlabci/check_symbols.sh

  artifacts:
    reports:
      metrics: metrics.txt
      junit: junit.xml

visual_diff:
  stage: diffs
  needs: []
  tags:
    - saas-linux-medium-amd64
  image: kicad/kicad:9.0
  allow_failure: true
  when: always
  only:
    refs:
      - merge_requests
    changes:
      - "*.kicad_sym"
  script:
    - mkdir -p $HOME/.config/kicad/9.0/colors
    - sudo apt update -qq
    - sudo apt install curl python3 python3-pip -qqy --no-install-recommends
    - python3 -V
    - pip install --break-system-packages --no-warn-script-location
        pygments
        wsdiff
        jinja2
    # Get the MR SHA values
    - source $CI_BUILDS_DIR/kicad-library-utils/tools/gitlabci/common.sh
    - $CI_BUILDS_DIR/kicad-library-utils/tools/gitlabci/visual_diff.sh
        -r "${CI_PROJECT_DIR}"
        -b "${BASE_SHA}"
        -t "${TARGET_SHA}"
        -o "diffs"
  artifacts:
    expose_as: "Visual Diff"
    paths:
      - "diffs/"
    reports:
      dotenv: deploy.env
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: $DYNAMIC_ENVIRONMENT_URL

nightly-convert:
  stage: export
  image: kicad/kicad:nightly
  only:
    - schedules
  before_script:
    - sudo apt update -q && sudo apt install -qqy git
    - git config --global "http.${CI_SERVER_URL}.sslCAInfo" "$CI_SERVER_TLS_CA_FILE"
    - git clone --depth 1 "${KICAD_LIBRARY_UTILS_REPO}" $CI_BUILDS_DIR/kicad-library-utils --branch "${KICAD_LIBRARY_UTILS_BRANCH}"
  script:
    - export LIB_NAME_MASK="*.kicad_sym" LIB_TYPE=sym LIB_DEST="$CI_BUILDS_DIR/libraries" LIB_REMOTE_BRANCH=nightly-rolling
    - git clone --branch $LIB_REMOTE_BRANCH --depth 1 "https://token:$GITLAB_ACCESS_TOKEN@gitlab.com/kicad/libraries/kicad-symbols.git" $CI_BUILDS_DIR/libraries
    - mkdir -p $HOME/.config/kicad/7.0/colors
    - $CI_BUILDS_DIR/kicad-library-utils/scripts/migrate_libraries.sh
    - cd $CI_BUILDS_DIR/libraries/
    - git config user.name "CI Pipeline"
    - git config user.email "cipipeline@noreply.kicad.org"
    - git add -A && git commit -m "Update to master ${CI_COMMIT_SHA:0:12}"
    - git push origin HEAD:$LIB_REMOTE_BRANCH
