image: python:3.12-slim

stages:
  - Static Analysis
  - Test
  # Separate FP and model generate stages, so the FP job isn't
  # overwhelmed by the 3D model (manual) jobs.
  - Footprint Generate
  - 3D Generate

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
  KICAD_FOOTPRINTS_REPO: https://gitlab.com/kicad/libraries/kicad-footprints.git
  KICAD_MODELS_REPO: https://gitlab.com/kicad/libraries/kicad-packages3d.git

check-code-formatting:
  stage: Static Analysis
  needs: []
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - gitlabci/**/*
        - .gitlab-ci.yml
        - src/**/*
        - KicadModTree/**/*
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install .[dev]
  script:
    - ./manage.sh format_check

check-static-typing:
  stage: Static Analysis
  needs: []
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - gitlabci/**/*
        - .gitlab-ci.yml
        - src/**/*
        - KicadModTree/**/*
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install .[dev]
  script:
    - ./manage.sh static_type_check

pytest:
  stage: Test
  needs: []
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - gitlabci/**/*
        - .gitlab-ci.yml
        - src/**/*
        - KicadModTree/**/*
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install .[dev]
  script:
    - python -m pytest --junitxml=report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml

fp-generate:
  stage: Footprint Generate
  needs: []
  tags:
    - saas-linux-medium-amd64
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
  image: python:3.12
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install -e .
  script:
    # Generate footprints
    - cd src/generators
    - ls -lah
    - time ./generate.py --separate-outputs --output-dir-footprints "$CI_PROJECT_DIR/footprints" -j0
    # Collect generated footprints into artifact archive
    - cd "$CI_PROJECT_DIR"
    - tar -cJf footprints.tar.xz footprints/

    # Write out the generation results for metric reporting
    - ./gitlabci/library_metrics.sh "footprints" "metrics.txt"
  artifacts:
    paths:
      - footprints.tar.xz
    reports:
      metrics: metrics.txt
    when: always

fp-generate-and-compare:
  stage: Test
  needs: []
  tags:
    - saas-linux-medium-amd64
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - gitlabci/**/*
        - .gitlab-ci.yml
        - src/**/*
        - KicadModTree/**/*
        - data/**/*
  allow_failure: true
  image: kicad/kicad:9.0
  before_script:
    # Required when using kicad Docker image (kicad-cli needed for visu diff):
    - sudo apt update -qq
    - sudo apt install curl python3 python3-pip -qqy --no-install-recommends
    - sudo ln -s /usr/bin/python3 /usr/bin/python
    - python --version
    - python -m pip install --break-system-packages --upgrade pip
    - pip install --break-system-packages -e .

    - echo "CI_BUILDS_DIR = $CI_BUILDS_DIR"
    - echo "CI_PROJECT_DIR = $CI_PROJECT_DIR"
    - 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 .)
    - export KICAD_LIBRARY_UTILS=$CI_BUILDS_DIR/kicad-library-utils
    - mkdir -p $HOME/.config/kicad/8.0/colors
    - pip install --break-system-packages --no-warn-script-location
        pygments
        wsdiff
        jinja2
    # Get the MR SHA values
    - source ${KICAD_LIBRARY_UTILS}/tools/gitlabci/common.sh
  script:
    # Download and extract base footprints generated by base branch (artifact)
    - BASE_FP_URL="$CI_API_V4_URL/projects/$CI_MERGE_REQUEST_PROJECT_ID/jobs/artifacts/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME/raw/footprints.tar.xz?job=fp-generate"

    # Required due to bug (?) or thing I don't understand in html_diff.py
    # (TODO: Try to fix there?)
    - unset CI_MERGE_REQUEST_ID

    - ./gitlabci/generate_and_compare.sh -u "$BASE_FP_URL" -o "./diffs" -t "./output"

    # Write out the generation results for metric reporting
    - ./gitlabci/library_metrics.sh "./output/generated_footprints" "metrics.txt"
  artifacts:
    expose_as: "Visual Diff"
    paths:
      - "diffs/"
    reports:
      dotenv: deploy.env
      metrics: metrics.txt
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: $DYNAMIC_ENVIRONMENT_URL

make-fp-mr:
  stage: Footprint Generate
  needs: []
  rules:
    - when: manual
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  tags:
    - saas-linux-medium-amd64
  allow_failure: true
  image: python:3.12
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install -e .[dev]
    - pip install python-gitlab
  script:
    # Clone fps
    - export TARGET=`realpath "$CI_BUILDS_DIR"`
    - git clone "${KICAD_FOOTPRINTS_REPO}" "$TARGET/fps" --branch master

    - cd "$TARGET/fps"
    # Clear all existing generated footprints (this allows auto-MRs to handle moves and deletions)
    - git grep --name-only -E "\(generator\s+\"kicad-footprint-generator\"\)" -- "*.kicad_mod" | xargs -d"\n" rm
    # Generate fps in a shell without CI environment variables
    - env -i HOME="$HOME" bash -l -c "time ${CI_PROJECT_DIR}/src/generators/generate.py -j0 --output-dir-footprints $TARGET/fps"

    - export branchname="gen-mr-${CI_MERGE_REQUEST_IID}"
    - git switch -c "$branchname"
    - git config --global user.email "kicad-mr-bot@example.com"
    - git config --global user.name "KiCad MR bot"
    - git add \*.kicad_mod
    - git commit --all
        --message "${CI_MERGE_REQUEST_TITLE}"
        --message "Output of merge request ${CI_MERGE_REQUEST_PROJECT_URL}/-/merge_requests/${CI_MERGE_REQUEST_IID}"
    - git push -uf "https://gitlab-ci-token:${MR_TOKEN}@gitlab.com/kicad/libraries/librarian-internal/kicad-footprints-fork.git" "$branchname"
    - export MR_DESCRIPTION="Output of merge request ${CI_MERGE_REQUEST_PROJECT_URL}/-/merge_requests/${CI_MERGE_REQUEST_IID} ${CI_MERGE_REQUEST_TITLE}"
    - export MR_TITLE="Generator MR ${CI_MERGE_REQUEST_IID} ${CI_MERGE_REQUEST_TITLE}"
    - |
      echo "Creating the FP MR..."
      set +e
      MR_CREATION_OUTPUT=$(
        gitlab --private-token "${MR_TOKEN}" \
        project-merge-request create \
        --project-id 68268360 \
        --source-branch "$branchname" \
        --target-branch master \
        --allow-collaboration true \
        --squash true \
        --remove-source-branch true \
        --target-project-id 21601606 \
        --title "$MR_TITLE" \
        --description "$MR_DESCRIPTION" \
        2>&1
      )
      EXIT_CODE=$?
      set -e
      echo "$MR_CREATION_OUTPUT"
      if [ $EXIT_CODE -ne 0 ]; then
        # Check if the failure is due to the MR already existing.
        if echo "$MR_CREATION_OUTPUT" | grep -q "409"; then
          # MR_CREATION_OUTPUT is something like "Impossible to create object (409: ['Another open merge request already exists for this source branch: !4472'])"
          IID=$(echo "$MR_CREATION_OUTPUT" | grep -o '!.*' | awk -F'!' '{ print $2 }' | awk -F']' '{ print $1 }')
        else
          # Unknown error - we return an error code:
          echo "Unknown error code!"
          exit 1
        fi
      else
        IID=$(echo "$MR_CREATION_OUTPUT" | grep "^iid: " | awk '{print $2}')
      fi
      echo "Merge request URL https://gitlab.com/kicad/libraries/kicad-footprints/-/merge_requests/${IID}"



make-3d-mr:
  stage: Footprint Generate
  needs: []
  rules:
    - when: manual
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  tags:
    - saas-linux-medium-amd64
  allow_failure: true
  image: python:3.12
  before_script:
    - python --version
    - python -m pip install --upgrade pip
    - pip install -e .[3d]
    - pip install dictdiffer python-gitlab
    - apt-get update -q -y
    - apt-get install -y libglx-mesa0 libgl1 libxrender1 git fontconfig
  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 .)
    - source $CI_BUILDS_DIR/kicad-library-utils/tools/gitlabci/common.sh
    - echo "Attempting to create output MR"
    - $CI_BUILDS_DIR/kicad-library-utils/tools/create_3d_mr.sh


# Include the 3d-model-generators pipelines
include:
  - remote: 'https://gitlab.com/kicad/libraries/kicad-library-utils/-/raw/master/tools/gitlabci/gitlab-ci-3dgen.yml'
