Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a helper function to send a cloudevent #45

Open
christian-kreuzberger-dtx opened this issue Oct 18, 2021 · 0 comments
Open

Write a helper function to send a cloudevent #45

christian-kreuzberger-dtx opened this issue Oct 18, 2021 · 0 comments
Labels
type:chore Provides value to the (dev) team

Comments

@christian-kreuzberger-dtx
Copy link
Contributor

christian-kreuzberger-dtx commented Oct 18, 2021

To avoid code duplication, a helper function that sends a CloudEvent to Keptn would be nice.

For instance, this is the code that is duplicated right now:

    def requestBody = """{
        |  "data": {
        |    "project": "${project}",
        |    "stage": "${stage}",
        |    "service": "${service}",
        |    "labels": {
        |      "buildId" : "${tag}",
        |      "jobname" : "${JOB_NAME}",
        |      "buildNumber": "${BUILD_NUMBER}",
        |      "joburl" : "${BUILD_URL}"
        |    },
        |    "deployment": {
        |      "deploymentstrategy": "direct",
        |      "deploymentURIsPublic": [
        |                "${deploymentURI}"
        |             ]
        |    }
        |  },
        |  "datacontenttype": "application/json",
        |  "source": "jenkins-library",
        |  "specversion": "1.0",
        |  "type": "sh.keptn.event.deployment.finished",
        |  "shkeptnspecversion": "${KEPTN_SPEC_VERSION}"
        |}
    """.stripMargin()

    // lets add our custom labels
    requestBody = addCustomLabels(requestBody, labels)
     
    def response = httpRequest contentType: 'APPLICATION_JSON', 
      customHeaders: [[maskValue: true, name: 'x-token', value: "${keptn_api_token}"]], 
      httpMode: 'POST', 
      requestBody: requestBody, 
      responseHandle: 'STRING', 
      url: "${keptn_endpoint}/v1/event", 
      validResponseCodes: "100:404", 
      ignoreSslErrors: true

    // write response to keptn.context.json & add to artifacts
    def keptnContext = writeKeptnContextFiles(response)

Optimizations

  • Adding project, service, stage, labels, specversion, shkeptnspecversion, source, datacontenttype, ... could be moved into the helper function
  • sending the http request could be moved into the helper function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:chore Provides value to the (dev) team
Projects
None yet
Development

No branches or pull requests

1 participant