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

variables:
  ENTRY_FOLDER: "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: &manifest
  stage: Manifest
  image: node:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - cd src/build_job
    - npm install
    - npm run start
    - cat ../../${ENTRY_FOLDER}/${PACKAGE_NAME}/theme.json
  artifacts:
    paths:
      - ${ENTRY_FOLDER}/${PACKAGE_NAME}/theme.json
    when: on_success
    expire_in: "1 hour"

manifest-job/valid_set:
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *manifest

.test-images-job: &test-images
  stage: Test
  image: python:latest
  rules:
    - 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_job/test_wpp.py --tb=line -rA --color=yes --code-highlight=yes -q
  allow_failure: true

test-images-job/valid_set:
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *test-images

test-images-job/missing_tag_set:
  variables:
    PACKAGE_NAME: "missing_tag_set"
  <<: *test-images

.test-manifest-job: &test-manifest
  stage: Test
  image: node:latest
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - ${ENTRY_FOLDER}/${PACKAGE_NAME}/*
  script:
    - cd src/test_manifest_job
    - npm install ajv ajv-errors mocha mocha-steps chai
    - cd ../..
    - node src/test_manifest_job/node_modules/mocha/bin/_mocha --require mocha-steps --colors src/test_manifest_job/wpp_checker.js

test-manifest-job/valid_set:
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *test-manifest

test-manifest-job/missing_tag_set:
  variables:
    PACKAGE_NAME: "missing_tag_set"
  <<: *test-manifest

# 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: &bundle
  stage: Bundle
  image: javieraviles/zip:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - 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/valid_set:
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *bundle

bundle-job/missing_tag_set:
  variables:
    PACKAGE_NAME: "missing_tag_set"
  <<: *bundle

.upload-job: &upload
  stage: Distribute
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - 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/valid_set:
  needs:
    - bundle-job/valid_set
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *upload

upload-job/missing_tag_set:
  needs:
    - bundle-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  <<: *upload

.release-job: &release
  stage: Release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - 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/valid_set:
  needs:
    - upload-job/valid_set
  variables:
    PACKAGE_NAME: "valid_set"
  <<: *release

release-job/missing_tag_set:
  needs:
    - upload-job/missing_tag_set
  variables:
    PACKAGE_NAME: "missing_tag_set"
  <<: *release
