# Note: We use --silent argument on npm invocations to keep the output simple
# and readable through email notifications.
image: node:6

cache:
    key: 'node-modules-kresus'
    paths:
        - node_modules/
        - yarn.lock

stages:
    - lint  # Check code syntax and coding style
    - build  # Try to build Kresus code
    - test  # Run unit tests

# Lint and check last commit in a dedicated task
lint:
    stage: lint
    before_script:
        - yarn install
    script:
        # Lint JS code
        - npm run --silent check:lint-full
        # And check banks and locales are correct
        - npm run --silent check:banks && npm run --silent check:locales

# Build the server and client code in dev and production
build:
    stage: build
    before_script:
        - yarn install
    script:
        # Check everything builds correctly
        - npm run --silent build:ci
    cache:
        policy: pull

# Run tests
test:
    stage: test
    before_script:
        - yarn install
        # Install weboob
        - apt-get update && apt-get install -y python-pip python-lxml python-yaml python-dev
        - mkdir ./weboob
        # Fix for outdated pip and setuptools.
        # See https://github.com/bigchaindb/bigchaindb/issues/236
        - pip install --upgrade setuptools pip
        - pip install weboob --target=./weboob
        - pip install pylint
    script:
        # Lint Python code
        - KRESUS_WEBOOB_DIR=./weboob npm run --silent check:python
        # Run the CI test suite without weboob installed
        - npm run --silent check:test
        # Run the CI test suite again with weboob installed
        - KRESUS_WEBOOB_DIR=./weboob npm run --silent check:test
    cache:
        policy: pull

# Also ensure all the commits in the MR individually pass the tests and checks.
# This is done in this testing stage to fail as soon as possible the CI if there is an
# error in the last commit.
lint_history:
    stage: test
    before_script:
        # Install up to date Git version
        - echo 'deb http://deb.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/backports.list
        - apt-get update && apt-get -t jessie-backports install -y git
        # Install JS deps
        - yarn install
        # Give it a Git identity for the rebase to work
        - git config --global user.email "gitlab-ci@kresus.org"
        - git config --global user.name "Gitlab CI"
        # Abort any rebase in progress
        - git rebase --abort 2> /dev/null || true
        # Abort any merge in progress
        - git merge --abort 2> /dev/null || true
        # Install weboob
        - apt-get update && apt-get install -y python-pip python-lxml python-yaml python-dev
        - mkdir ./weboob
        - pip install weboob --target=./weboob
    script:
        - npm run --silent check:full-history
