include: .gitlab/valid_set.yml

stages:
  - Manifest
  - Test
  - Bundle
  - Distribute
  - Release

variables:
  ENTRY_FOLDER: $CI_PROJECT_DIR/test # test directory for testing CI/CD, or assets directory for normal operations
  RELEASE_VERSION: v0.$CI_PIPELINE_IID
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PACKAGE_NAME}/${RELEASE_VERSION}"

# Jobs from here run on Merge Requests as prerequisite for merging.
.manifest-job:
  stage: Manifest
  image: node:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "cicd"
    - changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - cd src/
    - npm install
    - npm run start
    - cat ${ENTRY_FOLDER}/${PACKAGE_NAME}/theme.json
    - npm run test
  artifacts:
    paths:
      - ${ENTRY_FOLDER}/${PACKAGE_NAME}/theme.json
    when: on_success
    expire_in: "1 hour"

manifest-job/missing_tag_set:
  variables:
    PACKAGE_NAME: "missing_tag_set"
  extends: .manifest-job

.test-job:
  stage: Test
  image: python:latest
  rules:
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "cicd"
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - pip install -U pytest pillow iteration_utilities
    - python -m pytest src/test_images.py --tb=line -rA --color=yes
  allow_failure: true

test-images-job/missing_tag_set:
  needs:
    - manifest-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  extends: .test-job

# Jobs from here run Merged_results or Push/Merge Commits to the main branch. Tag is created on release so no run with tag creation
.bundle-job:
  stage: Bundle
  image: javieraviles/zip:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "cicd"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - zip -j ${PACKAGE_NAME}.zip ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  artifacts:
    paths:
      - ${PACKAGE_NAME}.zip
    when: on_success
    expire_in: "1 hour"

bundle-job/missing_tag_set:
  needs:
    - manifest-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  extends: .bundle-job

.upload-job:
  stage: Distribute
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - |
      curl --fail \
      --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
      --upload-file ${PACKAGE_NAME}.zip "${PACKAGE_REGISTRY_URL}/${PACKAGE_NAME}.zip?select=package_file"

upload-job/missing_tag_set:
  needs:
    - bundle-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  extends: .upload-job

.release-job:
  stage: Release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - echo "Releasing assets for $PACKAGE_NAME"
  release:
    name: "Release $PACKAGE_NAME $RELEASE_VERSION"
    tag_name: "$PACKAGE_NAME.$RELEASE_VERSION"
    description: "Version $RELEASE_VERSION"
    ref: "$CI_COMMIT_SHA"
    assets:
      links:
        - name: ${PACKAGE_NAME}
          url: "${PACKAGE_REGISTRY_URL}/${PACKAGE_NAME}.zip"

release-job/missing_tag_set:
  needs:
    - upload-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  extends: .release-job
