def shared

// number of build results and days to keep has to be defined in jenkinsfile
// for multibranch pipelines
// Lesson learned: if you run a properties() block later again all former properties
// get overridden!!!!! so be careful. all properties should be set in here
properties([
  buildDiscarder([$class: 'EnhancedOldBuildDiscarder', artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', discardOnlyOnSuccess: false, numToKeepStr: '3'])
])

node('adp-jenkins-agent-nodejs-with-azure-cli') {
  skipDefaultCheckout()

  scmVars = checkout scm

  shared = load 'ci/shared.groovy'
  shared.gitAuthenticate()

  env.GIT_COMMIT = scmVars.GIT_COMMIT

  branch = env.BRANCH_NAME


  stage("Prepare") {
    shared.withNotification('ci/prepare', "Prepare") {
      echo 'Preparing'
      sh 'npm -v'
      sh 'node -v'
      sh "http_proxy=''"
      sh 'npm config set fetch-retries 3'
      sh 'npm config set fetch-retry-mintimeout 15000'
      sh 'npm config set fetch-retry-maxtimeout 90000'
    }
  }

  stage("Package install") {
    shared.withNotification('ci/install', "Package install") {
      echo 'Installing packages'
      sh 'npm ci'
    }
  }

  stage("Generate documentation") {
    shared.withNotification('ci/generate-documentation', "Generate documentation") {
      echo 'Generating documentation'
      sh 'npm run library:generate:docs'
    }
  }

  stage("Lint check") {
    shared.withNotification('ci/lint', "Lint check") {
      echo 'Lint checking'
      sh 'npm run lint'
    }
  }

  stage("Format check") {
    shared.withNotification('ci/format', "Format check") {
      echo 'Format checking '
      sh 'npm run format'
    }
  }

  stage("Test ngx-docs-ui") {
    shared.withNotification('ci/test/ngx-docs-ui', "Test ngx-docs-ui") {
      echo 'Testing ngx-docs-ui'

      sh 'COMPACT_TEST_RESULTS=true npm run test:docs-ui:coverage'
      // The unit test will generate some report files which we want to save in the jenkins job (junit log & coverage)
      junit 'reports/ngx-docs-ui/junit/*.xml'
      publishHTML target: [
          allowMissing: false,
          alwaysLinkToLastBuild: false,
          keepAll: true,
          reportDir: 'reports/ngx-docs-ui/coverage',
          reportFiles: 'index.html',
          reportName: 'Coverage Report ngx-docs-ui (Istanbul)'
        ]
    }
  }

  stage("Test ng-aquila") {
    shared.withNotification('ci/test/ng-aquila', "Test ng-aquila") {
      echo 'Testing ng-aquila'

      sh 'COMPACT_TEST_RESULTS=true npm run test:lib:coverage'
      // The unit test will generate some report files which we want to save in the jenkins job (junit log & coverage)
      junit 'reports/ng-aquila/junit/*.xml'
      publishHTML target: [
        allowMissing: false,
        alwaysLinkToLastBuild: false,
        keepAll: true,
        reportDir: 'reports/ng-aquila/coverage',
        reportFiles: 'index.html',
        reportName: 'Coverage Report Aquila (Istanbul)'
      ]
    }
  }

  stage("Test ng-aquila/schematics") {
    shared.withNotification('ci/test/ng-aquila/schematics', "Test ng-aquila/schematics") {
      echo 'Testing ng-aquila/schematics'
      sh 'npm run test:schematics'
    }
  }

  stage("Build ng-aquila") {
    shared.withNotification('ci/build/ng-aquila', "Build ng-aquila") {
      echo 'Building ng-aquila'
      sh 'npm run build:lib'
    }
  }

  stage("Build documentation") {
    shared.withNotification('ci/build/documentation', "Build documentation") {
      echo "Building documentation"
      sh "npm run build:docs -- --progress=false"
    }
  }
}

