How Can We Help?

Search for answers or browse our knowledge base.

Deployment | Administration | Support

< All Topics
Print

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}")
                }
            }
        }
    }
} 
Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Previous Link Code with Work System Entities
Next Update Entities with SonarQube Quailty
Table of Contents