#!/usr/bin/env groovy

/*
 * Copyright (C) 2019 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

library 'canvas-builds-library'

pipeline {
  agent { label 'canvas-docker' }
  options {
    ansiColor('xterm')
    timestamps()
  }

  environment {
    BUILD_REGISTRY_FQDN = configuration.buildRegistryFQDN()
    COMPOSE_FILE = 'docker-compose.new-jenkins.yml:docker-compose.new-jenkins-selenium.yml'
    GERRIT_PORT = '29418'
    GERRIT_URL = "$GERRIT_HOST:$GERRIT_PORT"
    POSTGRES = configuration.postgres()
    RUBY = configuration.ruby()
    RERUNS_RETRY = 0 // no reruns
    POSTGRES_PASSWORD = 'sekret'

    DYNAMODB_IMAGE_TAG = imageTag.dynamodb()
    POSTGRES_IMAGE_TAG = imageTag.postgres()
  }

  stages {
    stage('Setup') {
      steps {
        cleanAndSetup()
      }
    }

    // Copied wholesale out of Jenkinsfile, this needs be abstracted if possible
    stage('Checkout Plugins') {
      steps {
        timeout(time: 10) {
          script {
            pullGerritRepo('gerrit_builder', 'master', '.')
            gems = readFile('gerrit_builder/canvas-lms/config/plugins_list').split()
            println "Plugin list: ${gems}"

            /* fetch plugins */
            gems.each { gem ->
              pullGerritRepo(gem, 'master', 'gems/plugins')
            }
            pullGerritRepo('qti_migration_tool', 'master', 'vendor')

            sh '''
              mv -v gerrit_builder/canvas-lms/config/* config/
              rm -v config/cache_store.yml
              rm -vr gerrit_builder
              cp -v docker-compose/config/selenium.yml config/
              cp -vR docker-compose/config/new-jenkins config/new-jenkins
              cp -v config/delayed_jobs.yml.example config/delayed_jobs.yml
              cp -v config/domain.yml.example config/domain.yml
              cp -v config/external_migration.yml.example config/external_migration.yml
              cp -v config/outgoing_mail.yml.example config/outgoing_mail.yml
            '''
          }
        }
      }
    }

    stage('Setup Containers') {
      steps {
        timeout(time: 20) {
          sh 'build/new-jenkins/docker-compose-pull.sh'
          sh 'build/new-jenkins/docker-compose-build-up.sh'
          sh 'build/new-jenkins/docker-compose-setup-databases.sh'
        }
      }
    }

    stage('Selenium Performance Tests') {
      steps {
        timeout(time: 60) {
          sh 'build/new-jenkins/rspec-with-retries.sh performance'
        }
      }
    }
  }

  post {
    success {
      slackSend(
        channel: env.SLACK_CHANNEL,
        color: 'good',
        message: "[${env.JOB_NAME}] <${env.BUILD_URL}|${env.BUILD_DISPLAY_NAME}> was successful."
      )
    }

    unsuccessful {
      // copy spec failures to local
      sh 'mkdir -vp tmp'
      script {
        try {
          // [JIRA CCI-168]: need to handle canvas container never being spun up
          sh "rm -rf tmp && mkdir -p tmp/ && docker cp test-queue-canvas-1:/usr/src/app/log/spec_failures tmp/ || true"
          script {
            def htmlFiles
            // find all results files
            dir('tmp') {
              htmlFiles = findFiles glob: '**/index.html'
            }
            // publish html
            publishHTML target: [
              allowMissing: false,
              alwaysLinkToLastBuild: false,
              keepAll: true,
              reportDir: 'tmp',
              reportFiles: htmlFiles.join(','),
              reportName: 'Test Failures'
            ]
          }
        } finally {
          slackSend(
            channel: env.SLACK_CHANNEL,
            color: 'danger',
            message: "[${env.JOB_NAME}] <${env.BUILD_URL}|${env.BUILD_DISPLAY_NAME}> was unsuccessful."
          )
        }
      }
    }

    cleanup {
      script {
        sh 'rm -vrf ./tmp/spec_failures/'
        libraryScript.execute 'bash/docker-cleanup.sh --allow-failure'
      }
    }
  }
}
