Skip to main content

Update Entities with Jenkins

Below is an example of Jenkins script which gathers all comments from build commits and then updates all related entities mentioned in these commits with the parameters defined in the payload.

API requests are implemented with HTTP Request Plugin and declartive Jenkinsfile

@NonCPS
def getCommentsString() {
    def list = []
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            if (entry.msg != null) {
               list.add("""${entry.commitId} on ${new Date(entry.timestamp)}: ${entry.msg.replaceAll("[\n\r]", " ")}""")
            }
        }
    }
    return list.join(',')
}


pipeline {
    agent any

    stages {
        stage('Clone') {
            steps {
                echo 'Clone Code'
            }
        }
        stage('Version') {
            steps {
                echo 'Version'
            }
        }
        stage('Test') {
            steps {
                echo 'Test'
            }
        }
        stage('Build') {
            steps {
                echo 'Build'
            }
        }
        stage('Publish') {
            steps {
                echo 'Publish'
            }
        }
        stage('Notify SyncNow') {
            steps {
                echo 'Notify'
                echo "Job Name is ${JOB_NAME}, Build ID is ${env.BUILD_ID}"
                script {

                    def commits = getCommentsString()

                    def payload = """
      [
 {
  "fields": [
   {
    "key": "Param1",
    "value": "string"
   },
   {
    "key": "Param2",
    "value": "string"
   },
   {
    "key": "Param3",
    "value": "string"
   }
  ],
  "entityType": "Bug",
  "comments": "${commits}"
 }
]
                    """

                    // Create entities and update entities from commit. Result in format for adding attachments
                    string response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', authentication: 'SyncNow', requestBody: "${payload}", url: "https://syncnowserver/api/v1.0/app/DevOpsGate/Enrich/<DevOpsGateProcessID>?action=update"


                    println("Status: ${response.status}")
                    println("Content: ${response.content}")
                }
            }
        }
    }
}