Skip to content

Commit

Permalink
Added support for cdEvents customData
Browse files Browse the repository at this point in the history
  • Loading branch information
spinnakerbot authored and GARCIA, JOSE committed Jun 11, 2024
1 parent 9d31dcc commit 81055b1
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 41 deletions.
6 changes: 3 additions & 3 deletions echo-notifications/echo-notifications.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ dependencies {
implementation "org.jsoup:jsoup:1.8.3"
implementation "com.atlassian.commonmark:commonmark:0.9.0"
implementation "org.codehaus.groovy:groovy-json"
implementation "io.cloudevents:cloudevents-http-basic:2.5.0"
implementation "io.cloudevents:cloudevents-json-jackson:2.5.0"
implementation ("dev.cdevents:cdevents-sdk-java:0.1.2")
implementation "io.cloudevents:cloudevents-http-basic:3.0.0"
implementation "io.cloudevents:cloudevents-json-jackson:3.0.0"
implementation ("dev.cdevents:cdevents-sdk-java:0.3.1")
testImplementation("com.icegreen:greenmail:1.5.14") {
exclude group: "com.sun.mail", module: "javax.mail"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import dev.cdevents.CDEvents;
import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.events.PipelineRunFinishedCDEvent;
import dev.cdevents.events.PipelinerunFinishedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;
Expand All @@ -27,34 +27,37 @@ public class CDEventPipelineRunFinished extends BaseCDEvent {

@Getter private String subjectPipelineName;
@Getter private String subjectError;
@Getter private Object customData;

public CDEventPipelineRunFinished(
String executionId,
String executionUrl,
String executionName,
String spinnakerUrl,
String status) {
String status,
Object customData) {
super(spinnakerUrl, executionId, spinnakerUrl, executionUrl);
this.subjectPipelineName = executionName;
this.subjectError = status;
this.customData = customData;
}

@Override
public CloudEvent createCDEvent() {
PipelineRunFinishedCDEvent cdEvent = new PipelineRunFinishedCDEvent();
PipelinerunFinishedCDEvent cdEvent = new PipelinerunFinishedCDEvent();
cdEvent.setSource(URI.create(getSource()));
cdEvent.setSubjectId(getSubjectId());
cdEvent.setSubjectSource(URI.create(getSubjectSource()));
cdEvent.setSubjectPipelineName(getSubjectPipelineName());
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()));
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()).toString());
cdEvent.setSubjectErrors(getSubjectError());

if ("complete".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS);
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS.getOutcome());
} else if ("failed".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE);
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE.getOutcome());
}

cdEvent.setCustomData(customData);
return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@
package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
import dev.cdevents.events.PipelineRunQueuedCDEvent;
import dev.cdevents.events.PipelinerunQueuedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;

public class CDEventPipelineRunQueued extends BaseCDEvent {

@Getter private String subjectPipelineName;
@Getter private Object customData;

public CDEventPipelineRunQueued(
String executionId, String executionUrl, String executionName, String spinnakerUrl) {
String executionId,
String executionUrl,
String executionName,
String spinnakerUrl,
Object customData) {
super(spinnakerUrl, executionId, spinnakerUrl, executionUrl);
this.subjectPipelineName = executionName;
this.customData = customData;
}

@Override
public CloudEvent createCDEvent() {
PipelineRunQueuedCDEvent cdEvent = new PipelineRunQueuedCDEvent();
PipelinerunQueuedCDEvent cdEvent = new PipelinerunQueuedCDEvent();
cdEvent.setSource(URI.create(getSource()));
cdEvent.setSubjectId(getSubjectId());
cdEvent.setSubjectSource(URI.create(getSubjectSource()));
cdEvent.setSubjectPipelineName(getSubjectPipelineName());
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()));

cdEvent.setSubjectUrl(URI.create(getSubjectUrl()).toString());
cdEvent.setCustomData(customData);
return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@
package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
import dev.cdevents.events.PipelineRunStartedCDEvent;
import dev.cdevents.events.PipelinerunStartedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;

public class CDEventPipelineRunStarted extends BaseCDEvent {

@Getter private String subjectPipelineName;
@Getter private Object customData;

public CDEventPipelineRunStarted(
String executionId, String executionUrl, String executionName, String spinnakerUrl) {
String executionId,
String executionUrl,
String executionName,
String spinnakerUrl,
Object customData) {
super(spinnakerUrl, executionId, spinnakerUrl, executionUrl);
this.subjectPipelineName = executionName;
this.customData = customData;
}

@Override
public CloudEvent createCDEvent() {
PipelineRunStartedCDEvent cdEvent = new PipelineRunStartedCDEvent();
PipelinerunStartedCDEvent cdEvent = new PipelinerunStartedCDEvent();
cdEvent.setSource(URI.create(getSource()));
cdEvent.setSubjectId(getSubjectId());
cdEvent.setSubjectSource(URI.create(getSubjectSource()));
cdEvent.setSubjectPipelineName(getSubjectPipelineName());
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()));
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()).toString());
cdEvent.setCustomData(customData);

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import dev.cdevents.CDEvents;
import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.events.TaskRunFinishedCDEvent;
import dev.cdevents.events.TaskrunFinishedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;
Expand All @@ -28,35 +28,38 @@ public class CDEventTaskRunFinished extends BaseCDEvent {
@Getter private String subjectTaskName;
@Getter private String subjectPipelineRunId;
@Getter private String subjectError;
@Getter private Object customData;

public CDEventTaskRunFinished(
String executionId,
String executionUrl,
String executionName,
String spinnakerUrl,
String status) {
String status,
Object customData) {
super(spinnakerUrl, executionId, spinnakerUrl, executionUrl);
this.subjectTaskName = executionName;
this.subjectPipelineRunId = executionId;
this.subjectError = status;
this.customData = customData;
}

@Override
public CloudEvent createCDEvent() {
TaskRunFinishedCDEvent cdEvent = new TaskRunFinishedCDEvent();
TaskrunFinishedCDEvent cdEvent = new TaskrunFinishedCDEvent();
cdEvent.setSource(URI.create(getSource()));
cdEvent.setSubjectId(getSubjectId());
cdEvent.setSubjectSource(URI.create(getSubjectSource()));
cdEvent.setSubjectTaskName(getSubjectTaskName());
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()));
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()).toString());
cdEvent.setSubjectErrors(getSubjectError());
cdEvent.setSubjectPipelineRunId(getSubjectPipelineRunId());
if ("complete".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS);
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS.getOutcome());
} else if ("failed".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE);
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE.getOutcome());
}

cdEvent.setCustomData(customData);
return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
import dev.cdevents.events.TaskRunStartedCDEvent;
import dev.cdevents.events.TaskrunStartedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;
Expand All @@ -26,24 +26,31 @@ public class CDEventTaskRunStarted extends BaseCDEvent {

@Getter private String subjectTaskName;
@Getter private String subjectPipelineRunId;
@Getter private Object customData;

public CDEventTaskRunStarted(
String executionId, String executionUrl, String executionName, String spinnakerUrl) {
String executionId,
String executionUrl,
String executionName,
String spinnakerUrl,
Object customData) {
super(spinnakerUrl, executionId, spinnakerUrl, executionUrl);
this.subjectTaskName = executionName;
this.subjectPipelineRunId = executionId;
this.customData = customData;
}

@Override
public CloudEvent createCDEvent() {
TaskRunStartedCDEvent cdEvent = new TaskRunStartedCDEvent();
TaskrunStartedCDEvent cdEvent = new TaskrunStartedCDEvent();
cdEvent.setSource(URI.create(getSource()));

cdEvent.setSubjectId(getSubjectId());
cdEvent.setSubjectSource(URI.create(getSubjectSource()));
cdEvent.setSubjectTaskName(getSubjectTaskName());
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()));
cdEvent.setSubjectUrl(URI.create(getSubjectUrl()).toString());
cdEvent.setSubjectPipelineRunId(getSubjectPipelineRunId());
cdEvent.setCustomData(customData);

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,32 @@ public CloudEvent createCDEvent(
Optional.ofNullable(preference)
.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());

log.info("Event type {} received to create CDEvent.", cdEventsType);
// This map will be updated to add more event types that Spinnaker needs to send
Map<String, BaseCDEvent> cdEventsMap =
Map.of(
CDEventTypes.PipelineRunQueuedEvent.getEventType(),
new CDEventPipelineRunQueued(
executionId, executionUrl, executionName, spinnakerUrl),
executionId, executionUrl, executionName, spinnakerUrl, customData),
CDEventTypes.PipelineRunStartedEvent.getEventType(),
new CDEventPipelineRunStarted(
executionId, executionUrl, executionName, spinnakerUrl),
executionId, executionUrl, executionName, spinnakerUrl, customData),
CDEventTypes.PipelineRunFinishedEvent.getEventType(),
new CDEventPipelineRunFinished(
executionId, executionUrl, executionName, spinnakerUrl, status),
executionId, executionUrl, executionName, spinnakerUrl, status, customData),
CDEventTypes.TaskRunStartedEvent.getEventType(),
new CDEventTaskRunStarted(executionId, executionUrl, executionName, spinnakerUrl),
new CDEventTaskRunStarted(
executionId, executionUrl, executionName, spinnakerUrl, customData),
CDEventTypes.TaskRunFinishedEvent.getEventType(),
new CDEventTaskRunFinished(
executionId, executionUrl, executionName, spinnakerUrl, status));
executionId, executionUrl, executionName, spinnakerUrl, status, customData));

BaseCDEvent cdEvent =
cdEventsMap.keySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class CDEventsNotificationAgentSpec extends Specification {

where:
cdEventsType || expectedType || status
"dev.cdevents.pipelinerun.queued" || /dev.cdevents.pipelinerun.queued.0.1.0/ || /starting/
"dev.cdevents.pipelinerun.started" || /dev.cdevents.pipelinerun.started.0.1.0/ || /started/
"dev.cdevents.pipelinerun.finished" || /dev.cdevents.pipelinerun.finished.0.1.0/ || /complete/
"dev.cdevents.taskrun.started" || /dev.cdevents.taskrun.started.0.1.0/ || /started/
"dev.cdevents.taskrun.finished" || /dev.cdevents.taskrun.finished.0.1.0/ || /complete/
"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/


brokerURL = "http://dev.cdevents.server/default/events-broker"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fiatVersion=1.47.0
korkVersion=7.228.0
korkVersion=7.229.0
kotlinVersion=1.6.21
org.gradle.parallel=true
spinnakerGradleVersion=8.32.1
Expand Down

0 comments on commit 81055b1

Please sign in to comment.