Skip to content

Commit

Permalink
feat: Adding EventPayloadFunction for sendFinishEvent (#105)
Browse files Browse the repository at this point in the history
* build(deps): update keptn/gh-automation action to v1.5.1 (#103)

Signed-off-by: Renovate Bot <bot@renovateapp.com>

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventType payload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding EventPayloadFunction

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* adding eventPayload

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

* rework some typos in release notes, chagend eventPayload to eventTypePayload for consistency

Signed-off-by: reneforstner <rene.forstner@nttdata.com>

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Christian Kreuzberger <christian.kreuzberger@dynatrace.com>
  • Loading branch information
4 people authored Apr 28, 2022
1 parent c65b022 commit ecbd794
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ keptn.keptnConfigureMonitoring monitoring:"dynatrace"
def labels=[:]
labels.put('TriggeredBy', 'Andi')
// Send Finished Event Use Case
// ------------------------------------------
// Send back a finished event to keptn for any triggered task which was handled by Jenkins
// keptn.sendFinishedEvent functions have optional event type payload, depending on the type of an event
// Example #1: Send a finished Event for a test task
def eventTypePayload=[:]
eventTypePayload.put('start', '2019-06-07T07:00:00.0000Z')
eventTypePayload.put('end', '2019-06-07T08:00:00.0000Z')
def keptnContext = keptn.sendFinishedEvent eventType: "test", keptnContext: "${params.shkeptncontext}", triggeredId: "${params.triggeredid}", result:"pass", status:"succeeded", eventTypePayload: eventTypePayload, lables: lables
// Example #2: Send a finished Event for a deployment task
// keptn.sendFinishedEvent functions have optional event type payload - example payload for keptn.sendFinishedEvent eventType: "deployment"
def eventTypePayload=[:]
eventTypePayload.put('deploymentstrategy', 'direct')
eventTypePayload.put('deploymentURIsLocal', ['carts.sockshop-staging.svc.cluster.local','another.cartsUri.local'])
def keptnContext = keptn.sendFinishedEvent eventType: "deployment", keptnContext: "${params.shkeptncontext}", triggeredId: "${params.triggeredid}", result:"pass", status:"succeeded", eventTypePayload: eventTypePayload, lables: lables
// Quality Gate Evaluation Use Case
// ------------------------------------------
// Start a quality gate evaluation. There are multiple timeframe options, e.g: using timestamps or number minutes from Now()
Expand Down
28 changes: 24 additions & 4 deletions src/sh/keptn/Keptn.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,23 @@ def addCustomLabels(requestBody, labels) {
return requestBody
}

/**
* takes the request JSON body, adds eventpayload and reports back that JSON as string
*/
def addEventTypePayload(requestBody, eventType, eventTypePayload) {
def requestBodyAsJSON = readJSON text: requestBody, returnPojo: true
if (eventTypePayload != null) {
requestBodyAsJSON['data'][eventType] = eventTypePayload
}

writeJSON file: "helper.json", json: requestBodyAsJSON
requestBody = readFile "helper.json"

return requestBody
}



/**
* sendStartEvaluationEvent(project, stage, service, starttime, endtime, [labels, keptn_endpoint, keptn_api_token])
* will start an evaluation and stores the keptn context in keptn.context.json
Expand Down Expand Up @@ -764,7 +781,7 @@ def waitForEvaluationDoneEvent(Map args) {
}

/**
* sendFinishedEvent(KEPTN_INIT_PARAMS, keptnContext, eventType, triggeredId, [result, status, message, labels])
* sendFinishedEvent(KEPTN_INIT_PARAMS, keptnContext, eventType, triggeredId, [result, status, message, labels, eventTypePayload])
* Will send a finished event of type eventType (e.g., eventType.finished)
* will stores the API result in keptn.context.json
*/
Expand All @@ -780,7 +797,8 @@ def sendFinishedEvent(Map args) {

// load labels from args (if set)
def labels = args.containsKey('labels') ? args.labels : [:]

def eventTypePayload = args.containsKey('eventTypePayload') ? args.eventTypePayload : [:]

// verify keptnContext is set in args
if (!args.containsKey('keptnContext')) {
error("sendFinishedEvent requires keptnContext to be passed")
Expand Down Expand Up @@ -821,7 +839,8 @@ def sendFinishedEvent(Map args) {
| },
| "result": "${result}",
| "status": "${status}",
| "message": "${message}"
| "message": "${message}",
| "${eventType}": {}
| },
| "datacontenttype": "application/json",
| "source": "${KEPTN_EVENT_SOURCE}",
Expand All @@ -833,8 +852,9 @@ def sendFinishedEvent(Map args) {
|}
""".stripMargin()

// lets add our custom labels
// lets add our custom labels & eventTypePayload
requestBody = addCustomLabels(requestBody, labels)
requestBody = addEventTypePayload(requestBody, eventType, eventTypePayload)

def response = httpRequest contentType: 'APPLICATION_JSON',
customHeaders: [[maskValue: true, name: 'x-token', value: "${keptn_api_token}"]],
Expand Down

0 comments on commit ecbd794

Please sign in to comment.