def prNumber = BRANCH_NAME.tokenize("PR-")[0]
pipeline {
  agent any
  options {
    skipDefaultCheckout false
  }
  triggers {
    GenericTrigger(
     genericVariables: [
        [
            key: 'action', 
            value: '$.action',
            expressionType: 'JSONPath', //Optional, defaults to JSONPath
            regexpFilter: '[^(created)]', //Optional, defaults to empty string
            defaultValue: '' //Optional, defaults to empty string
        ],
        [
            key: 'comment',
            value: '$.comment.body',
            expressionType: 'JSONPath', //Optional, defaults to JSONPath
            regexpFilter: '', //Optional, defaults to empty string
            defaultValue: '' //Optional, defaults to empty string
        ],
        [
            key: 'org',
            value: '$.organization.login',
            expressionType: 'JSONPath', //Optional, defaults to JSONPath
            regexpFilter: '', //Optional, defaults to empty string
            defaultValue: 'JuliaSmoothOptimizers' //Optional, defaults to empty string
        ],
        [
            key: 'pullrequest',
            value: '$.issue.number',
            expressionType: 'JSONPath', //Optional, defaults to JSONPath
            regexpFilter: '[^0-9]', //Optional, defaults to empty string
            defaultValue: '' //Optional, defaults to empty string
        ],
        [
            key: 'repo',
            value: '$.repository.name',
            expressionType: 'JSONPath', //Optional, defaults to JSONPath
            regexpFilter: '', //Optional, defaults to empty string
            defaultValue: '' //Optional, defaults to empty string
        ]
     ],

     causeString: 'Triggered on comment',

     token: "NLPModelsKnitroTest",

     printContributedVariables: true,
     printPostContent: true,

     silentResponse: false,

     regexpFilterText: '$comment $pullrequest',
     regexpFilterExpression: '@JSOBot runtests ' + prNumber
    )
  }
  stages {
    stage('run tests') {
      when {
        expression { env.comment }
      }
      steps {
        sh "chmod +x test/run_tests.sh"
        sh "mkdir -p $HOME/tests/${org}/${repo}"
        sh "qsub -N ${repo}_${pullrequest}_test -V -cwd -e $HOME/tests/${org}/${repo}/${pullrequest}_${BUILD_NUMBER}_error.log test/run_tests.sh"
      }
    }
  }
  post {
    success {
      echo "SUCCESS!"  
    }
    cleanup {
      sh 'printenv'
      sh '''
      git clean -fd
      git reset --hard
      '''
    }
  }
}
