Mark testresults automatically for each deployment

PROD-35718

Change-Id: I0289d9db8316d4ffcfd984b42be22e02b1abab1d
diff --git a/src/com/mirantis/system_qa/SharedPipeline.groovy b/src/com/mirantis/system_qa/SharedPipeline.groovy
index 6b5f82c..ccb0163 100644
--- a/src/com/mirantis/system_qa/SharedPipeline.groovy
+++ b/src/com/mirantis/system_qa/SharedPipeline.groovy
@@ -680,13 +680,11 @@
     writeFile(file: filename, text: script, encoding: "UTF-8")
 }
 
-def upload_results_to_testrail(report_name, testSuiteName, methodname, testrail_name_template, reporter_extra_options=[]) {
+def upload_results_to_testrail(report_name, testPlanName, testSuiteName, methodname, testrail_name_template, reporter_extra_options=[]) {
   def venvPath = '/home/jenkins/venv_testrail_reporter'
   def testPlanDesc = env.LAB_CONFIG_NAME
   def testrailURL = "https://mirantis.testrail.com"
   def testrailProject = "Mirantis Cloud Platform"
-  def testPlanNamePrefix = env.TEST_PLAN_NAME_PREFIX ?: "[2019.2.0-update]System"
-  def testPlanName = env.TEST_PLAN_NAME ?: "${testPlanNamePrefix}-${MCP_VERSION}-${new Date().format('yyyy-MM-dd')}"
   def testrailMilestone = "MCP1.1"
   def testrailCaseMaxNameLenght = 250
   def jobURL = env.BUILD_URL
@@ -744,3 +742,38 @@
     def status = STATUS_MAP[result ?: 'FAILURE']   // currentBuild.result *must* be set at the finish of the try/catch
     create_xml_report(filename, classname, name, status, "Deploy components: ${deploy_expected_stacks}", text, '', '')
 }
+
+def mark_test_results(testPlanName, testSuiteName) {
+    def venvPath = '/home/jenkins/venv_testrail_analyzer'
+    def testrailURL = "https://mirantis.testrail.com"
+    def testrailProject = "Mirantis Cloud Platform"
+    def configName = env.LAB_CONFIG_NAME
+    def testRunName = "${configName} <${testSuiteName}>"
+
+    def script = """
+    . ${venvPath}/bin/activate
+    set -ex
+    export TESTRAIL_URL=${testrailURL}
+    testrail-reporter analyze -p "${testrailProject}" -t "${testPlanName}" -r "${testRunName}" checklist.yaml
+    """
+
+  def testrail_cred_id = params.TESTRAIL_CRED ?: 'testrail_system_tests'
+
+  withCredentials([
+             [$class          : 'UsernamePasswordMultiBinding',
+             credentialsId   : testrail_cred_id,
+             passwordVariable: 'TESTRAIL_PASSWORD',
+             usernameVariable: 'TESTRAIL_USER']
+  ]) {
+    def ret = [:]
+    ret.stdout = ''
+    ret.exception = ''
+    try {
+        ret.stdout = run_cmd_stdout(script)
+    } catch (Exception ex) {
+        ret.exception = ("""\
+##### Report to '${testRunName}' failed: #####\n""" + ex.message + "\n\n")
+    }
+    return ret
+  }
+}
\ No newline at end of file