#!groovy
@Library('jenkins-pipeline-lib@pj/new-bs') _

pipeline
{
  agent any
  environment
  {
    GIT_CHANGE_LOG = gitChangeLog(currentBuild.changeSets)
  }
  parameters
  {
    string(defaultValue: '1', description: 'Major version number (x.0.0)', name: 'MAJOR_VERSION')
    string(defaultValue: '1', description: 'Minor version number (0.x.0)', name: 'MINOR_VERSION')
  }
  triggers
  {
    //At 04:00 on every day-of-week from Monday through Friday.
    pollSCM('H 4 * * 1-5')
  }
  stages
  {
    stage('Setup')
    {
      steps
      {
        gitTagPreBuild "${params.MAJOR_VERSION}.${params.MINOR_VERSION}.${BUILD_NUMBER}"

        echo 'Removing existing build results'
        sh 'make distclean'
      }
    }
    stage('Build')
    {
      steps
      {
        sh 'make'
      }
    }
    stage('Cross compile for ARM')
    {
      steps
      {
        sh 'make CROSS=arm:cortex-m4_hardfloat BUILDRESULTS=buildresults/arm'
        sh 'ninja -C buildresults/arm/'
      }
    }
    stage('Test')
    {
      steps
      {
        sh 'make test'
      }
      post
      {
        always
        {
          // Report Tests
          junit 'buildresults/test/*.xml'
        }
      }
    }
    stage('Documentation')
    {
      steps
      {
        sh 'make docs'
      }
      post
      {
        success
        {
          dir('buildresults')
          {
            sh 'tar cf documentation.tgz docs/'
          }
        }
      }
    }
    stage('CppCheck')
    {
      steps
      {
        sh 'make cppcheck-xml'
      }
    }
    stage('Clang Tidy')
    {
      steps
      {
        sh 'make tidy'
      }
    }
    stage('Clang Analyzer')
    {
      steps
      {
        sh 'make scan-build'
      }
    }
    stage('Coverage')
    {
      steps
      {
        sh 'make coverage'
      }
      post
      {
        success
        {
          cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'buildresults/coverage/meson-logs/coverage.xml', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
        }
      }
    }
    stage('Archive')
    {
      steps
      {
        echo 'Archiving artifacts'
        dir('buildresults')
        {
          archiveArtifacts 'documentation.tgz'
        }
      }
      post
      {
        failure
        {
          sendNotifications 'ARCHIVE_ERROR'
        }
      }
    }
  }
  post
  {
    always
    {
      // Scan for open tasks, warnings, issues, etc.
      recordIssues(
        enabledForFailure: true,
        healthy: 5,
        unhealthy: 10,
        referenceJobName: 'ea-nightly/libc/master',
        qualityGates: [
          // 3 new issue: unstable
          //[threshold: 3, type: 'DELTA', unstable: true],
          // 5 new issues: failed build
          //[threshold: 5, type: 'DELTA', unstable: false],
          // 10 total issues: unstable
          //[threshold: 55, type: 'TOTAL', unstable: true],
          // 20 total issues: fail
          //[threshold: 75, type: 'TOTAL', unstable: false]
        ],
        tools: [
          clang(),
          taskScanner(
            excludePattern: 'buildresults/**, printf/**, openlibm/**, gdtoa/**',
            includePattern: '**/*.c, **/*.cpp, **/*.h, **/*.hpp, **/*.lua, **/*.sh, **/*.build',
            normalTags: 'TODO, to do, WIP',
            highTags: 'FIXME, FIX',
            ignoreCase: true,
          ),
          cppCheck(
            pattern: 'buildresults/**/cppcheck.xml',
          ),
        ]
      )

      // Report Status
      slackNotify(currentBuild.currentResult)
      gitTagCleanup "${params.MAJOR_VERSION}.${params.MINOR_VERSION}.${BUILD_NUMBER}"
    }
    success
    {
      gitTagSuccess "${params.MAJOR_VERSION}.${params.MINOR_VERSION}.${BUILD_NUMBER}"
    }
    failure
    {
      /*
      * This job does not have a GitHub configuration,
      * so we need to create a dummy config
      */
      githubSetConfig('69e4682e-2951-492f-b828-da06364c322d')
      githubFileIssue()
      emailNotify(currentBuild.currentResult)
    }
  }
}
