Skip to content

Commit

Permalink
feat(notifications/cdEvents): added support for customData at pipelin…
Browse files Browse the repository at this point in the history
…e notification config level. (#1445)

Co-authored-by: GARCIA, JOSE <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent 98eb168 commit 4fcd274
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dev.cdevents.constants.CDEventConstants.CDEventTypes;
import dev.cdevents.exception.CDEventsException;
import io.cloudevents.CloudEvent;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -72,11 +73,18 @@ public CloudEvent createCDEvent(
.map(p -> (String) p.get("cdEventsType"))
.orElseThrow(() -> new FieldNotFoundException("notifications.cdEventsType"));

Object customData =
Optional.ofNullable(event.content)
.map(e -> (Map) e.get("context"))
.map(e -> e.get("customData"))
.orElse(new Object());
// Grab customData object from notification level as higher order precedence. Needed for when
// setting pipeline
// level notifications.
Map<String, Object> customData =
Optional.ofNullable(preference)
.map(p -> (Map<String, Object>) p.get("customData"))
.orElseGet(
() ->
Optional.ofNullable(event.content)
.map(e -> (Map<String, Object>) e.get("context"))
.map(ctx -> (Map<String, Object>) ctx.get("customData"))
.orElseGet(Collections::emptyMap));

log.info("Event type {} received to create CDEvent.", cdEventsType);
// This map will be updated to add more event types that Spinnaker needs to send
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ class CDEventsNotificationAgentSpec extends Specification {
cdevent.get().getType() ==~ expectedType

where:
cdEventsType || expectedType || status
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/
cdEventsType || expectedType || status
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/
"dev.cdevents.pipelinerun.finished" || /dev.cdevents.pipelinerun.finished.0.1.1/ || /complete/
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/


brokerURL = "http://dev.cdevents.server/default/events-broker"
Expand All @@ -70,7 +70,7 @@ class CDEventsNotificationAgentSpec extends Specification {
}

@Unroll
def "sends cdEvent with customData #customData"() {
def "sends cdEvent with customData #customData when set at event.context level"() {

given:
def cdevent = new BlockingVariable<CloudEvent>()
Expand All @@ -86,19 +86,85 @@ class CDEventsNotificationAgentSpec extends Specification {
cdevent.get().getType() ==~ expectedType

where:
cdEventsType || expectedType || status || customData
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/ || [foo: "pipelinerun.queued"]
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/ || [foo: "pipelinerun.started"]
cdEventsType || expectedType || status || customData
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/ || [foo: "pipelinerun.queued"]
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/ || [foo: "pipelinerun.started"]
"dev.cdevents.pipelinerun.finished" || /dev.cdevents.pipelinerun.finished.0.1.1/ || /complete/ || [foo: "pipelinerun.finished"]
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/ || [foo: "taskrun.started"]
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/ || [foo: "taskrun.finished"]
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/ || [foo: "taskrun.started"]
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/ || [foo: "taskrun.finished"]

brokerURL = "http://dev.cdevents.server/default/events-broker"
application = "whatever"
event = new Event(content: [
execution: [id: "1", name: "foo-pipeline"],
context : [customData: customData]
])
type = "pipeline"
}

@Unroll
def "sends cdEvent with customData #customData when set at notification preference level"() {

given:
def cdevent = new BlockingVariable<CloudEvent>()
cdeventsSender.sendCDEvent(*_) >> { ceToSend, eventsBrokerURL ->
cdevent.set(ceToSend)
}

when:
agent.sendNotifications([address: brokerURL, cdEventsType: cdEventsType, customData: customData], application, event, [type: type, link: "link"], status)

then:
def m = convertToMap(cdevent.get().getData().toBytes())
convertToMap(cdevent.get().getData().toBytes()).customData == customData
cdevent.get().getType() ==~ expectedType

where:
cdEventsType || expectedType || status || customData
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/ || [foo: "pipelinerun.queued"]
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/ || [foo: "pipelinerun.started"]
"dev.cdevents.pipelinerun.finished" || /dev.cdevents.pipelinerun.finished.0.1.1/ || /complete/ || [foo: "pipelinerun.finished"]
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/ || [foo: "taskrun.started"]
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/ || [foo: "taskrun.finished"]

brokerURL = "http://dev.cdevents.server/default/events-broker"
application = "whatever"
event = new Event(content: [
execution: [id: "1", name: "foo-pipeline"]
])
type = "pipeline"
}

@Unroll
def "sends cdEvent with customData #customData when set at notification preference level and event.context level"() {

given:
def cdevent = new BlockingVariable<CloudEvent>()
cdeventsSender.sendCDEvent(*_) >> { ceToSend, eventsBrokerURL ->
cdevent.set(ceToSend)
}

when:
agent.sendNotifications([address: brokerURL, cdEventsType: cdEventsType, customData: preferenceCustomData], application, event, [type: type, link: "link"], status)

then:
//Preference level should take higher order precedence
convertToMap(cdevent.get().getData().toBytes()).customData == preferenceCustomData
cdevent.get().getType() ==~ expectedType

where:
cdEventsType || expectedType || status || customData || preferenceCustomData
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.1/ || /starting/ || [foo: "pipelinerun.queued"] || [zoo: "pipelinerun.queued"]
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.1/ || /started/ || [foo: "pipelinerun.started"] || [zoo: "pipelinerun.started"]
"dev.cdevents.pipelinerun.finished" || /dev.cdevents.pipelinerun.finished.0.1.1/ || /complete/ || [foo: "pipelinerun.finished"] || [zoo: "pipelinerun.finished"]
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.1/ || /started/ || [foo: "taskrun.started"] || [zoo: "taskrun.started"]
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.1/ || /complete/ || [foo: "taskrun.finished"] || [zoo: "taskrun.finished"]

brokerURL = "http://dev.cdevents.server/default/events-broker"
application = "whatever"
event = new Event(content: [
execution: [id: "1", name: "foo-pipeline"],
context: [customData: customData]
context : [customData: customData]
])
type = "pipeline"
}
Expand Down

0 comments on commit 4fcd274

Please sign in to comment.