###############################################################################
# Licensed Materials - Property of IBM
# (c) Copyright IBM Corporation 2017, 2019. All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
# Copyright (c) 2020 Red Hat, Inc.
#
# Contributors:
#  IBM Corporation - initial API and implementation
###############################################################################

sudo: required
language: node_js

services:
  - docker

branches:
  only:
    - master
    - /^release-[0-9]+\..*$/

addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4
  sonarcloud:
    organization: "open-cluster-management"
    token:
      secure: "DIRJBk/UF3HgixDQwyIfUcxbm3SxZ3U4ESyJIUmkgGUFNNMF9ST0OdK3ULIVT4n/EnDkzuB99gXyORdU022c1lGZoIqsuSJ6ZSIP50nIK10upRtsOTijtQVNC2I9Hea/QgjReWmB7Fvel+cK5gg/ixEy1+6IfabKJIq4yXluPXR3YsqQkFbDtJ69mYBLxS95SfWIwVmVqsMuvZJswMVYSnWjJ0PK7BAlK2R+4yOiZRv2OEOgny+jJE18i9byBRjQCA3nw3rC72IUHBwj3QyAiPHe+Grx2K3plA/8bpXRsQ55M1NtUx33W1L03w9NBBxrIj8lM0XYy1aR5cPNmQ9483Tc+RJO5Uw3QqEay7CoZW5YXv3TMsvX+8jXhdDKBmwljaMjOinq2rsclsKoy+8RvcS1IqZ2QH6VxPHWYo3+t4F8ogVWIMpwA2elHCq7O+Q8um2/CFh9g7p7D2zxWI8lRltCit4aDmnu/khzqV/JtCXVLgjrcQaQ5d3bXNLSZctimTnZXYdwNk/HxG11yM0sfOntmPbi4tZgpHrzSM5CtHqcaMiKikq5DPoLyf0OU+2Btc/GYmsbAmDyQPENzdrWnhpysP7qJzJOUgORufyHlFEv+ZitghXw/PQ6ygONj2BjEmIdXmwgEL3UqVdjMnwOeHMX4dwLENaQrJKChBulyH8="

env:
  global:
    # Required
    - OS=linux
    - COMPONENT_TAG_EXTENSION="-${TRAVIS_COMMIT}"

    # Component Specific
    - COMPONENT_INIT_COMMAND=${TRAVIS_BUILD_DIR}/build/install-dependencies.sh
    - COMPONENT_BUILD_COMMAND=${TRAVIS_BUILD_DIR}/build/build.sh
    - COMPONENT_UNIT_TEST_COMMAND=${TRAVIS_BUILD_DIR}/build/run-unit-tests.sh
    - COMPONENT_E2E_TEST_COMMAND=${TRAVIS_BUILD_DIR}/build/run-e2e-tests.sh

stages:
  - build
  - unit-test
  - name: test-e2e
    if: env(USE_CLUSTERPOOL) IS blank
  - name: test-e2e-clusterpool
    if: env(USE_CLUSTERPOOL) IS present
  - release-ff
  - publish

before_script:
  - make init
  - ./keep_alive.sh

jobs:
  include:
    - stage: build
      name: "Build the component and test images and push them"
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - make
        - make component/build || travis_terminate 1
        - make component/push
        - make security/scans
        - make build-test-image
        - make push-test-image
    - stage: unit-test
      name: "Build and run unit tests"
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - make
        - make copyright-check TRAVIS_BRANCH=${TRAVIS_BRANCH}
        - make sonar/js/jest-init
        - make 
        - make component/pull         
        - make component/test/unit
        - make sonar/js
    - stage: test-e2e
      name: "Deploy the image to a cluster and run e2e tests"
      if: type = pull_request
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - make
        - make component/pull
        - make component/test/e2e
    - stage: test-e2e-clusterpool
      name: "Deploy the image to a ClusterPool cluster and run e2e tests"
      if: type = pull_request
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - export COMPONENT_E2E_TEST_COMMAND=${TRAVIS_BUILD_DIR}/build/run-e2e-tests-clusterpool.sh
        - make
        - make component/pull
        - make component/test/e2e
    - stage: release-ff
      name: "Push commits to current release branch"
      if: type = push AND branch =~ /^master$/
      script:
        - make
        - make release-ff
    - stage: publish
      name: "Publish the image to quay with an official version/sha tag and publish entry to integration pipeline stage"
      if: type = push AND branch =~ /^release-[0-9]+\..*$/
      script:
        - make 
        - make pipeline-manifest/update PIPELINE_MANIFEST_COMPONENT_SHA256=${TRAVIS_COMMIT} PIPELINE_MANIFEST_COMPONENT_REPO=${TRAVIS_REPO_SLUG} PIPELINE_MANIFEST_BRANCH=${TRAVIS_BRANCH}
        - rm -rf pipeline
        - echo "Waiting 120s before publishing test image..." && sleep 120
        - make publish-test-image
