# Stages
stages:
  - test
  - codequality
  - send-reports
 
# Files or directory that should be cached throughout the pipeline
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
  - node_modules/

# Variables that needs be used throughout the pipeline are defined
variables:
  # Sonarqube server Url with slash at the end
  SONAR_SERVER_URL: "http://espy.digite.com:9000/"

  # Text that is defined as a sonar.host.url value in sonar propertes file
  SONAR_SERVER_URL_DUMMY_TEXT: "##SonarServerURL##"
  
  # Text that is defined as sonar.login value in sonar properties file
  SONAR_LOGIN_TOKEN_DUMMY_TEXT: "##ProjectSonarLogin##"

  # Text that is defined as sonar.login value in sonar
  SONAR_PROJECT_KEY_DUMMY_TEXT: "##ProjectSonarKey##"

  # Specify Group Name of the Git repo
  # For example, if git repo is MicroFrontend/trace-hierarchy-dependency then group name is microFrontend in lowercase
  REPO_GROUP_NAME: "microfrontend"

  # Specify Name of the Git repo
  # For example, if git repo is MicroFrontend/trace-hierarchy-dependency then project name is trace-hierarchy-dependency
  REPO_NAME: "#GIT-REPO-NAME#"

  # Provide Elk index.
  # Eg. if git repo is MicroFrontend/trace-hierarchy-dependency, then index can be cmpnt-mf-thd-jest(group & repo's short form)
  ELK_INDEX_NAME: "cmpnt-mf-#GIT-REPO-NAME#-jest"

  # AWS url for storing reports
  AWS_LINK: s3://digite-releases/atlas/$REPO_GROUP_NAME/$REPO_NAME/Jest

  # These are optional. Only required when package.json file requires git repo dependencies
  # Gitlab variable with the name GIT_AUTO_AUTH should be present in pipeline. Keep it as default 
  PACKAGE_GIT_TEXT: "gitserver.digite.in"
  PACKAGE_GIT_AUTH_TEXT: "git_auto:$GIT_AUTO_AUTH@gitserver.digite.in"

  GIT_DEPTH: 0


# Job name 
test:
  only:
# The branch for which this stage should be run. If not specified, it runs for all branches 
    - /^(\d+\.)?(\d+\.)?(\*|\d+)$/
  tags:
# Specify the tag of gitlab runner on which this stage should be run
# Make sure that a gitlab runner with this tag is enabled for this repo
    - mf-thd
# Stage name that will be defined here
  stage: test
  script:
# Here, the scripts are written to be executed
    - sed -i "s+$PACKAGE_GIT_TEXT+$PACKAGE_GIT_AUTH_TEXT+g" package.json
    - yarn  
    - yarn test
# This section runs after the script of stage
  after_script:
# Python file is executed to send elk test data even if testcases fail
    #Make sure that ELK_CRED gitlab variable is present in the repo settings
    - sed -i "s+#elkpass#+$ELK_CRED+g" config.ini
    - pip3 install -r requirements.txt
    - python3 devops_insert_elkdata.py -j $CI_PIPELINE_ID -n $REPO_NAME -p $REPO_GROUP_NAME -i $ELK_INDEX_NAME -b $CI_BUILD_REF_NAME
# This section is required to pass required reports to the furthur stages(For code coverage in sonar).
  artifacts:
    paths:
# Files/folders which are needed for furthur use
# Currently, its coverage folder and test report file created by jest
      - src/coverage
      - test-report.html
#The period till these folder/files are required
    expire_in: 20 min 

sonarqube-check:
  only:
    - /^(\d+\.)?(\d+\.)?(\*|\d+)$/
  tags:
    - mf-thd
  stage: codequality
  script:
    - sed -i "s+$SONAR_SERVER_URL_DUMMY_TEXT+$SONAR_SERVER_URL+g" sonar-scanner.properties
#Make sure that SONAR_LOGIN_TOKEN gitlab variable is present in the repo settings
    - sed -i "s+$SONAR_LOGIN_TOKEN_DUMMY_TEXT+$SONAR_LOGIN_TOKEN+g" sonar-scanner.properties
    - sed -i "s+$SONAR_PROJECT_KEY_DUMMY_TEXT+$REPO_NAME+g" sonar-scanner.properties
#This one is required for code coverage in sonar
    - sed -i "s+SF:$(pwd)/src+SF:/usr/src+g" src/coverage/lcov.info
#The sonar scanner container is started
#Currently it will mount the src folder of repo
    - docker run --rm --user="$(id -u):$(id -g)" -v $(pwd)/src:/usr/src -v $(pwd)/sonar-scanner.properties:/usr/lib/sonar-scanner/conf/sonar-scanner.properties newtmitch/sonar-scanner:4-alpine
    - sleep 15s
    - echo "Sonar analysis of project can be viewed through url "$(cat ./src/.scannerwork/report-task.txt | grep dashboardUrl | cut -c14- )
#This ignores the stage failure
  allow_failure: true

send-reports:
  only:
    - /^(\d+\.)?(\d+\.)?(\*|\d+)$/
  tags:
    - mf-thd
  stage: send-reports
  script:
#Reports are uploaded to aws
#Make sure that gitlab variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are present in the repo settings
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
#Here, you have to specify the aws paths in which reports will be uploaded
    - aws s3 cp test-report.html $AWS_LINK/CI_PIPELINE_ID/
    - aws s3 cp src/coverage/lcov-report $AWS_LINK/$CI_PIPELINE_ID/coverage/ --recursive
    - echo "$CI_PIPELINE_ID" >> latest-build-no.txt
    - aws s3 cp latest-build-no.txt $AWS_LINK/
#Python file is executed which sends email
#Make sure that ELK_CRED gitlab variable is present in the repo settings
    - sed -i "s+#elkpass#+$ELK_CRED+g" config.ini
    - sed -i "s+#sonarurl#+$SONAR_SERVER_URL+g" config.ini
    - sed -i "s+#sonartoken#+$SONAR_LOGIN_TOKEN+g" config.ini
#Make sure that gitlab variables SES_AWS_ACCESS_KEY_ID and SES_AWS_SECRET_ACCESS_KEY are present in the repo settings
    - export AWS_ACCESS_KEY_ID=$SES_AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$SES_AWS_SECRET_ACCESS_KEY    
    - pip3 install -r requirements.txt
    - python3 devops_jestparser_result.py -j $CI_PIPELINE_ID
