From c15cdd16552901039d4c09a10c4a52bcfe67af02 Mon Sep 17 00:00:00 2001
From: Bastian <bastian.heppener@mgm-tp.com>
Date: Fri, 7 Mar 2025 14:58:36 +0100
Subject: [PATCH] ozg-7493 add send failure message

---
 Jenkinsfile | 42 ++++++++++++------------------------------
 1 file changed, 12 insertions(+), 30 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index ba6272c..6405324 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -21,6 +21,8 @@
  * Die sprachspezifischen Genehmigungen und Beschränkungen
  * unter der Lizenz sind dem Lizenztext zu entnehmen.
  */
+@Library('ozgcloud-libs') _
+
 pipeline {
    agent {
         node {
@@ -29,6 +31,7 @@ pipeline {
     }
 
     environment {
+        PIPELINE_NAME = "Code-SH: antragraum-proxy"
         RELEASE_REGEX = /v\d+.\d+.\d+/
         BETA_REGEX = /v\d+.\d+.\d+-beta.\d/
         FAILED_STAGE = ""
@@ -190,6 +193,7 @@ pipeline {
             }
             steps {
                 script {
+                    FAILED_STAGE = env.STAGE_NAME
                     catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
                         sh '''
                             go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest
@@ -204,7 +208,14 @@ pipeline {
                 }
             }
         }
-    }    
+    }
+    post {
+        failure {
+            script {
+                sendMatrixMessage.sendFailureMessage(FAILED_STAGE)
+            }
+        }
+    }
 }
 
 
@@ -256,35 +267,6 @@ String getBuildProfile() {
     }
 }
 
-
-Void sendFailureMessage() {
-    def room = getRoom()
-    def data = getFailureData()
-
-    sh "curl -XPOST -H 'authorization: Bearer ${getElementAccessToken()}' -d '${data}' https://matrix.ozg-sh.de/_matrix/client/v3/rooms/$room/send/m.room.message"
-}
-
-String getElementAccessToken() {
-    withCredentials([string(credentialsId: 'element-login-json', variable: 'LOGIN_JSON')]) {
-        return readJSON ( text: sh (script: '''curl -XPOST -d \"$LOGIN_JSON\" https://matrix.ozg-sh.de/_matrix/client/v3/login''', returnStdout: true)).access_token
-    }
-}
-
-String getFailureData() {
-    return  """{"msgtype":"m.text", \
-                    "body":"antragraum-proxy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER}", \
-                    "format": "org.matrix.custom.html", \
-                    "formatted_body":"antragraum-proxy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER}"}"""
-}
-
-String getRoom() {
-    if (isReleaseBranch()) {
-        return "!oWZpUGTFsxkJIYNfYg:matrix.ozg-sh.de"
-    } else {
-        return "!iQPAvQIiRwRpNOszjw:matrix.ozg-sh.de"
-    } 
-}
-
 Void configureGit() {
     def email = "jenkins@ozg-sh.de"
     def name = "jenkins"
-- 
GitLab