include:
  - template: Dependency-Scanning.gitlab-ci.yml
  - template: SAST.gitlab-ci.yml

stages:
  - test
  - install
  - package
  - installer
  - release-check
  - release-note
  - release

###############################################################################
##              TEST
###############################################################################

code_quality:
  stage: test
  image: docker:stable
  allow_failure: true
  services:
    - docker:stable-dind
  variables:
    DOCKER_DRIVER: overlay2
  script:
    - |
      if ! docker info &>/dev/null; then
        if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then
          export DOCKER_HOST='tcp://localhost:2375'
        fi
      fi
    - docker run
        --env CODECLIMATE_CODE="$PWD"
        --volume "$PWD":/code
        --volume /var/run/docker.sock:/var/run/docker.sock
        --volume /tmp/cc:/tmp/cc
        --entrypoint "/bin/sh" codeclimate/codeclimate -c "/usr/src/app/bin/codeclimate engines:install && /usr/src/app/bin/codeclimate analyze"
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  except:
    variables:
      - $CODE_QUALITY_DISABLED

dependency_scanning:
  stage: test
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  except: []

sast:
  stage: test
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  except: []

js_syntax:
  stage: test
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  image: node:carbon
  tags: [ docker ]
  cache: {}
  dependencies: []
  before_script:
    - npm install -g acorn
    - rm -rf node_modules
  script:
    - find . -iname "*.js" -print0 | xargs -0 -I % sh -c 'echo "%"; acorn --ecma6 --silent "%"'

###############################################################################
##              INSTALL
###############################################################################

node:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: install
  tags: [ docker ]
  image: electronuserland/builder:wine-mono
  script:
    - npm install
    - cp vendor/7z/7z.exe node_modules/electron-winstaller/vendor/7z.exe
    - cp vendor/7z/7z.dll node_modules/electron-winstaller/vendor/7z.dll
  cache:
    paths:
      - node_modules/
  artifacts:
    paths:
      - node_modules/

###############################################################################
##              PACKAGE
###############################################################################

win32:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: package
  tags: [ docker ]
  image: electronuserland/builder:wine-mono
  script:
    - rm -rf vendor/*
    - npm run-script package-win32
  artifacts:
    paths:
      - release-builds/
  dependencies:
    - node

linux:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: package
  tags: [ docker ]
  image: electronuserland/builder:wine-mono
  script:
    - rm -rf bin/*
    - npm run-script package-linux
  artifacts:
    paths:
      - release-builds/
  dependencies:
    - node

###############################################################################
##              INSTALLER
###############################################################################

exe-installer:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: installer
  tags: [ docker ]
  image: docker:stable
  services:
    - docker:stable-dind
  variables:
    DOCKER_DRIVER: overlay2
  script:
    - |
      if ! docker info &>/dev/null; then
        if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then
          export DOCKER_HOST='tcp://localhost:2375'
        fi
      fi
    - mkdir installer-builds
    - chmod 777 installer-builds
    - docker run
        --env ELECTRON_CACHE="/root/.cache/electron"
        --env ELECTRON_BUILDER_CACHE="/root/.cache/electron-builder"
        -v ${PWD}:/project
        -v ~/.cache/electron:/root/.cache/electron
        -v ~/.cache/electron-builder:/root/.cache/electron-builder
        electronuserland/builder:wine-mono /bin/bash -c "npm run-script build-exe"
    - ls installer-builds/RyzenControllerInstaller.exe
    - echo "https://gitlab.com/ryzen-controller-team/ryzen-controller/-/jobs/${CI_JOB_ID}/artifacts/browse/installer-builds/" > .exe_link
  artifacts:
    paths:
      - installer-builds/
      - .exe_link
  dependencies:
    - node
    - win32

deb-installer:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: installer
  tags: [ docker ]
  image: debian:stretch
  before_script:
    - apt update -y
    - apt install fakeroot curl -y
    - curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
    - export NVM_DIR="$HOME/.nvm"
    - '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"'
    - nvm install 10.15.1
    - nvm use 10.15.1
    - echo "https://gitlab.com/ryzen-controller-team/ryzen-controller/-/jobs/${CI_JOB_ID}/artifacts/browse/installer-builds/installers/" > .deb_link
  script:
    - npm run-script build-deb
  artifacts:
    paths:
      - installer-builds/
      - .deb_link
  dependencies:
    - node
    - linux

rpm-installer:
  only:
    - branches
    - schedules@ryzen-controller-team/ryzen-controller
  stage: installer
  tags: [ docker ]
  image: electronuserland/builder:wine-mono
  script:
    - npm run-script build-rpm
    - echo "https://gitlab.com/ryzen-controller-team/ryzen-controller/-/jobs/${CI_JOB_ID}/artifacts/browse/installer-builds/installers/" > .rpm_link
  artifacts:
    paths:
      - installer-builds/
      - .rpm_link
  dependencies:
    - node
    - linux

###############################################################################
##              RELEASE-CHECK
###############################################################################

release-check:
  tags: [ docker ]
  image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.20.4
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  stage: release-check
  dependencies: []
  script:
    - release next-version > .next_version
  artifacts:
    paths:
      - .next_version

###############################################################################
##              RELEASE-NOTE
###############################################################################

create-rpm-link:
  tags: [ docker ]
  image: alpine
  stage: release-note
  dependencies:
    - rpm-installer
    - release-check
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  before_script:
    - apk add curl jq
  script:
    - URL=`cat .rpm_link`
    - VERSION=`cat .next_version`
    - VERSION_ESCAPED=`echo $VERSION | tr '.' '-'`
    - curl -H "Content-Type:application/json"
      -X POST
      -H "Authorization:Bearer ${BL_TOKEN}"
      -d "{\"title\":\"${VERSION} rpm\",\"tags\":[\"RC_rpm\",\"RC_${VERSION_ESCAPED}\",\"RC\"],\"long_url\":\"${URL}\",\"group_guid\":\"Bj4p9bnMV93\"}"
      "https://api-ssl.bitly.com/v4/bitlinks" > .rpm_res.json
    - cat .rpm_res.json | jq -e -r .link > .rpm_short_link
  artifacts:
    paths:
      - .rpm_short_link
      - .rpm_res.json

create-deb-link:
  tags: [ docker ]
  image: alpine
  stage: release-note
  dependencies:
    - deb-installer
    - release-check
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  before_script:
    - apk add curl jq
  script:
    - URL=`cat .deb_link`
    - VERSION=`cat .next_version`
    - VERSION_ESCAPED=`echo $VERSION | tr '.' '-'`
    - curl -H "Content-Type:application/json"
      -X POST
      -H "Authorization:Bearer ${BL_TOKEN}"
      -d "{\"title\":\"${VERSION} deb\",\"tags\":[\"RC_deb\",\"RC_${VERSION_ESCAPED}\",\"RC\"],\"long_url\":\"${URL}\",\"group_guid\":\"Bj4p9bnMV93\"}"
      "https://api-ssl.bitly.com/v4/bitlinks" > .deb_res.json
    - cat .deb_res.json | jq -e -r .link > .deb_short_link
  artifacts:
    paths:
      - .deb_short_link
      - .deb_res.json

create-exe-link:
  tags: [ docker ]
  image: alpine
  stage: release-note
  dependencies:
    - exe-installer
    - release-check
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  before_script:
    - apk add curl jq
  script:
    - URL=`cat .exe_link`
    - VERSION=`cat .next_version`
    - VERSION_ESCAPED=`echo $VERSION | tr '.' '-'`
    - curl -H "Content-Type:application/json"
      -X POST
      -H "Authorization:Bearer ${BL_TOKEN}"
      -d "{\"title\":\"${VERSION} exe\",\"tags\":[\"RC_exe\",\"RC_${VERSION_ESCAPED}\",\"RC\"],\"long_url\":\"${URL}\",\"group_guid\":\"Bj4p9bnMV93\"}"
      "https://api-ssl.bitly.com/v4/bitlinks" > .exe_res.json
    - cat .exe_res.json | jq -e -r .link > .exe_short_link
  artifacts:
    paths:
      - .exe_short_link
      - .exe_res.json

update-changelog:
  tags: [ docker ]
  image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.20.4
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  stage: release-note
  script:
    - release changelog
  artifacts:
    paths:
      - CHANGELOG.md

update-package-version:
  tags: [ docker ]
  image: electronuserland/builder:wine-mono
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  stage: release-note
  dependencies:
    - release-check
  script:
    - VERSION=`cat .next_version`
    - npm version --no-git-tag-version ${VERSION}
  artifacts:
    paths:
      - package.json
      - package-lock.json

###############################################################################
##              RELEASE
###############################################################################

publish:
  tags: [ docker ]
  image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.20.4
  only:
    - schedules@ryzen-controller-team/ryzen-controller
  stage: release
  dependencies:
    - release-check
    - create-rpm-link
    - create-deb-link
    - create-exe-link
    - update-changelog
    - update-package-version
  script:
    - VERSION=`cat .next_version`
    - release changelog
    - release --ci-commit-tag ${VERSION} commit-and-tag CHANGELOG.md package.json package-lock.json
    - release --ci-commit-tag ${VERSION} add-download-link -d "Debian installer (and variant)" -n "ryzencontroller_${VERSION}_amd64.deb" -u "`cat .deb_short_link`"
    - release --ci-commit-tag ${VERSION} add-download-link -d "Redhat installer (and variant)" -n "ryzencontroller_${VERSION}.amd64.rpm" -u "`cat .rpm_short_link`"
    - release --ci-commit-tag ${VERSION} add-download-link -d "Windows installer" -n "RyzenControllerInstaller.exe" -u "`cat .exe_short_link`"