stages:
  - auto-tagging
  - build
  - deploy
  - update
  
variables:
  MYSQL_CONNECTION: "mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASS "
  # STATUS_SCRIPT: /home/gitlab-runner/monitor/deployment-status.sh
  # HELM_CHART: /home/gitlab-runner/kubernetes/ilens/$QA_ENV/ilens-modules
  # VARIABLES_YML: variables.yml
  # DEPLOYMENT_YML: metadata-services.yml
  TIMEOUT: 960s

before_script:
  - val=`echo $($MYSQL_CONNECTION -e "SELECT COUNT(*) FROM $VERSION_DB.$DB_TABLE WHERE category='customer-implementations' AND type='AI' AND os='docker' AND module_name='$CI_PROJECT_NAME' ") | cut -d " " -f2`
  - if [ $val == 0 ]; then $MYSQL_CONNECTION -e "INSERT INTO $VERSION_DB.$DB_TABLE values('customer-implementations','AI','$CI_PROJECT_NAME','docker', '0', '0', '0', '0')";fi
  - QA=$($MYSQL_CONNECTION -N -e "SELECT qa FROM $VERSION_DB.$DB_TABLE where module_name = '$CI_PROJECT_NAME' AND type = 'AI' AND category = 'customer-implementations' AND os = 'docker'")
  - DEV=$($MYSQL_CONNECTION -N -e "SELECT dev FROM $VERSION_DB.$DB_TABLE where module_name = '$CI_PROJECT_NAME' AND type = 'AI' AND category = 'customer-implementations' AND os = 'docker'")
  - UAT=$(mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASS -N -e "SELECT uat FROM $VERSION_DB.$DB_TABLE where module_name = '$CI_PROJECT_NAME' AND type = 'AI' AND category = 'customer-implementations' AND os = 'docker'")
  - PROD=$($MYSQL_CONNECTION -N -e "SELECT prod FROM $VERSION_DB.$DB_TABLE where module_name = '$CI_PROJECT_NAME' AND type = 'AI' AND category = 'customer-implementations' AND os = 'docker'")

auto-tagging:
  stage: auto-tagging
  before_script:
    - val=`echo $($MYSQL_CONNECTION -e "SELECT COUNT(*) FROM $VERSION_DB.$VERSION_RELEASE_TABLE WHERE  module_name='$CI_PROJECT_NAME' ") | cut -d " " -f2`
    - if [ $val == 0 ]; then $MYSQL_CONNECTION -N -e "INSERT INTO $VERSION_DB.$VERSION_RELEASE_TABLE values('$CI_PROJECT_NAME', 'iLens', '0', '0', '0', '0')";fi
    - ILENS=$($MYSQL_CONNECTION -N -e "SELECT ilens_version FROM "$VERSION_DB.$VERSION_RELEASE_TABLE" where module_name = '$CI_PROJECT_NAME'")
    - RELEASE=$($MYSQL_CONNECTION -N -e "SELECT release_version FROM "$VERSION_DB.$VERSION_RELEASE_TABLE" where module_name = '$CI_PROJECT_NAME'")
    - FEATURE=$($MYSQL_CONNECTION -N -e "SELECT feature_version FROM "$VERSION_DB.$VERSION_RELEASE_TABLE" where module_name = '$CI_PROJECT_NAME'")
    - PATCH=$($MYSQL_CONNECTION -N -e "SELECT patch_version FROM "$VERSION_DB.$VERSION_RELEASE_TABLE" where module_name = '$CI_PROJECT_NAME'")

  script:
    - SOURCE_BRANCH=$(echo $CI_COMMIT_TITLE | cut -f 3 -d " " | cut -f 1 -d "/" | cut -f 2 -d "'")
    - >
        if [ "$SOURCE_BRANCH" = "QA" ]; then
          ((RELEASE=RELEASE+1)) && FEATURE=0 && PATCH=0;
          TAG_NAME=v$RELEASE.$FEATURE
          IMAGE_URL=azracrilensai.azurecr.io/repository/ilens-ai/customer-implementations/acc/$CI_PROJECT_NAME:"$TAG_NAME"
          PROD=$RELEASE; QA=0; DEV=0;
          $MYSQL_CONNECTION -e "UPDATE $VERSION_DB.$DB_TABLE SET prod='$PROD' ,qa='$QA', dev='$DEV' WHERE module_name='$CI_PROJECT_NAME' AND type='AI' AND category='customer-implementations' AND os='docker'"         
        elif [ $SOURCE_BRANCH == "feature" ]; then
          ((FEATURE=FEATURE+1)) && PATCH=0;
          TAG_NAME=v$RELEASE.$FEATURE
          IMAGE_URL=azracrilensai.azurecr.io/repository/ilens-ai/customer-implementations/acc/$CI_PROJECT_NAME:"$TAG_NAME"
        elif [ $SOURCE_BRANCH == "patch" ]; then
          ((PATCH=PATCH+1));
          TAG_NAME=v$RELEASE.$FEATURE.$PATCH
          IMAGE_URL=azracrilensai.azurecr.io/repository/ilens-ai/customer-implementations/acc/$CI_PROJECT_NAME:"$TAG_NAME"
        else
          exit 1
        fi
        
    - echo -e "\n\nImage:" $IMAGE_URL >> ReleaseNote.txt
    - sed -i "1s|^|Version":" $TAG_NAME\n|" ReleaseNote.txt
    - sed -i "1s|^|Module Name":" $CI_PROJECT_NAME\n|" ReleaseNote.txt
    - docker build -t $IMAGE_URL .
    - docker push $IMAGE_URL
    - docker rmi --force $IMAGE_URL
    - URL=$(echo $CI_PROJECT_URL | sed 's|https://||')
    - git remote set-url origin https://$GIT_USRNAME:$GIT_USRPASSWD@$URL
    - git config user.email "devopsilens@gmail.com"
    - git config user.name "$GIT_USRNAME"    
    - git tag -a $TAG_NAME -F ReleaseNote.txt
    - git push origin $TAG_NAME
    - $MYSQL_CONNECTION -e "UPDATE $VERSION_DB.$VERSION_RELEASE_TABLE SET release_version='$RELEASE', feature_version='$FEATURE', patch_version='$PATCH'  WHERE module_name = '$CI_PROJECT_NAME' "
    - $MYSQL_CONNECTION -e "INSERT INTO $HISTORY_DB.$VERSION_RELEASE_TABLE values('$CI_JOB_ID', '$CI_PROJECT_NAME','iLens', '$ILENS.$RELEASE.$FEATURE', '$CI_COMMIT_SHA', '$GITLAB_USER_NAME', '$CI_COMMIT_REF_NAME')"
  tags:
    - shell
  only:
    - master

#~~~~~| CODE QUALITY |~~~~~#
codequality:
  stage: deploy
  image: azacrknowledgelens.azurecr.io/knowledgelens/klit-operation/devops/gitlab-runner:ubuntu-sonarscanner
  script:
     - /opt/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=$CI_PROJECT_NAME -Dsonar.projectName=$CI_PROJECT_NAME -Dsonar.typescript.node=./node/node -Dsonar.login=admin -Dsonar.password=admin -Dsonar.sources=.
     - sleep 5
     - python3 /opt/code_quality_report/static_code_quality_report_csv_v2.py $CI_PROJECT_NAME $GITLAB_USER_EMAIL,$EMAIL_TO $EMAIL_FROM $EMAIL_PASSWD False admin admin
  only:
     - develop
  tags:
     - docker
