Skip to content

Commit

Permalink
feat(cdevents-notification): Implementing produce CDEvents using Noti…
Browse files Browse the repository at this point in the history
…fication (#1295)

* feat(cdevents-notification): Implementing produce CDEvents using Notification agent

* feat(cdevents-notification): unit tests to produce CDEvents using Notification

* feat(cdevents-notification): update variable name to cdEventsType

* Updating cdevents-sdk-java dependency version

Signed-off-by: Jalander Ramagiri <[email protected]>

* fix: unit test failure with sdk

* using retrofit to send cdevent

* fixing format violations

* addressing review comments

* Interface to create CDEvents

* update Copyright information

* Making CDEventCreator as an abstract

* Class name changed to BaseCDEvent and uing lombok.Getter

* addressing review comments

* addressing minor review comments

---------

Signed-off-by: Jalander Ramagiri <[email protected]>
  • Loading branch information
rjalander authored Oct 26, 2023
1 parent 4fa2665 commit 8bdcaa5
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 0 deletions.
3 changes: 3 additions & 0 deletions echo-notifications/echo-notifications.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +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")
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
@@ -0,0 +1,45 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package com.netflix.spinnaker.echo.cdevents;

import io.cloudevents.CloudEvent;
import lombok.Getter;

public abstract class BaseCDEvent {
/**
* This method can be implemented to create various types of CDEvents that can return a specific
* type of CDEvents in a CloudEvent format, more details on CDEvent types can be found in
* Documentation at https://cdevents.dev
*
* @return cdEvent
*/
abstract CloudEvent createCDEvent();

/** Common properties used in most of the CDEvents */
@Getter private String source;

@Getter private String subjectId;
@Getter private String subjectSource;
@Getter private String subjectUrl;

public BaseCDEvent(String source, String subjectId, String subjectSource, String subjectUrl) {
this.source = source;
this.subjectId = subjectId;
this.subjectSource = subjectSource;
this.subjectUrl = subjectUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.events.PipelineRunFinishedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;

public class CDEventPipelineRunFinished extends BaseCDEvent {

@Getter private String subjectPipelineName;
@Getter private String subjectError;

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

@Override
public CloudEvent createCDEvent() {
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.setSubjectErrors(getSubjectError());

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

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
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;

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

@Override
public CloudEvent createCDEvent() {
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()));

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
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;

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

@Override
public CloudEvent createCDEvent() {
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()));

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package com.netflix.spinnaker.echo.cdevents;

import dev.cdevents.CDEvents;
import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.events.TaskRunFinishedCDEvent;
import io.cloudevents.CloudEvent;
import java.net.URI;
import lombok.Getter;

public class CDEventTaskRunFinished extends BaseCDEvent {

@Getter private String subjectTaskName;
@Getter private String subjectPipelineRunId;
@Getter private String subjectError;

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

@Override
public CloudEvent createCDEvent() {
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.setSubjectErrors(getSubjectError());
cdEvent.setSubjectPipelineRunId(getSubjectPipelineRunId());
if ("complete".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS);
} else if ("failed".equals(getSubjectError())) {
cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE);
}

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (C) 2023 Nordix Foundation.
For a full list of individual contributors, please see the commit history.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package com.netflix.spinnaker.echo.cdevents;

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

public class CDEventTaskRunStarted extends BaseCDEvent {

@Getter private String subjectTaskName;
@Getter private String subjectPipelineRunId;

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

@Override
public CloudEvent createCDEvent() {
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.setSubjectPipelineRunId(getSubjectPipelineRunId());

return CDEvents.cdEventAsCloudEvent(cdEvent);
}
}
Loading

0 comments on commit 8bdcaa5

Please sign in to comment.