Skip to content

Core CDEvents to render as CloudEvent and schema validation #41

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

Merged
merged 7 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/dev/cdevents/CDEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public static String cdEventAsJson(CDEvent cdEvent) {
* @return CloudEvent
*/
public static CloudEvent cdEventAsCloudEvent(CDEvent cdEvent) {

String cdEventJson = cdEventAsJson(cdEvent);
if (!validateCDEvent(cdEvent)) {
log.error("CDEvent validation failed against schema URL - {}", cdEvent.schemaURL());
throw new CDEventsException("CDEvent validation failed against schema URL - " + cdEvent.schemaURL());
}
String cdEventJson = cdEventAsJson(cdEvent);
log.info("CDEvent with type {} as json - {}", cdEvent.getContext().getType(), cdEventJson);
CloudEvent ceToSend = new CloudEventBuilder()
.withId(UUID.randomUUID().toString())
.withSource(cdEvent.getContext().getSource())
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/dev/cdevents/constants/CDEventConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ private CDEventConstants() {
/**
* CDEvents Version.
*/
public static final String CDEVENTS_SPEC_VERSION = "0.1.0";
public static final String CDEVENTS_SPEC_VERSION = "0.1.2";

public enum SubjectType {

/**
* Subject Type taskRun.
*/
TASKRUN("taskRun"),
/**
* Subject Type pipelineRun.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ public String schemaURL() {
public String eventSchema() {
return "{\n" +
" \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" +
" \"$id\": \"https://cdevents.dev/0.1.0/schema/pipeline-run-finished-event\",\n" +
" \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-finished-event\",\n" +
" \"properties\": {\n" +
" \"context\": {\n" +
" \"properties\": {\n" +
" \"version\": {\n" +
" \"type\": \"string\",\n" +
" \"enum\": [\n" +
" \"0.1.0\"\n" +
" ],\n" +
" \"default\": \"0.1.0\"\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"string\",\n" +
Expand All @@ -79,7 +76,10 @@ public String eventSchema() {
" },\n" +
" \"type\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" \"enum\": [\n" +
" \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" +
" ],\n" +
" \"default\": \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" +
" },\n" +
" \"timestamp\": {\n" +
" \"type\": \"string\",\n" +
Expand Down
188 changes: 188 additions & 0 deletions src/main/java/dev/cdevents/events/PipelineRunQueuedCDEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package dev.cdevents.events;

import com.fasterxml.jackson.annotation.JsonProperty;
import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.models.CDEvent;
import dev.cdevents.models.PipelineRunQueuedSubject;

import java.net.URI;

public class PipelineRunQueuedCDEvent extends CDEvent {

private static final String CDEVENT_VERSION = "0.1.0";
@JsonProperty(required = true)
private PipelineRunQueuedSubject subject;

/**
* Constructor to init CDEvent and set the Subject for {@link PipelineRunQueuedCDEvent}.
*/
public PipelineRunQueuedCDEvent() {
initCDEvent(currentCDEventType());
setSubject(new PipelineRunQueuedSubject(CDEventConstants.SubjectType.PIPELINERUN));
}

/**
* @return subject
*/
public PipelineRunQueuedSubject getSubject() {
return subject;
}

/**
* @param subject
*/
public void setSubject(PipelineRunQueuedSubject subject) {
this.subject = subject;
}

/**
* @return the PipelineRunQueuedEvent type
*/
@Override
public String currentCDEventType() {
return CDEventConstants.CDEventTypes.PipelineRunQueuedEvent.getEventType().concat(CDEVENT_VERSION);
}

/**
* @return the pipeline-run-queued-event schema URL
*/
@Override
public String schemaURL() {
return String.format("https://cdevents.dev/%s/schema/pipeline-run-queued-event", CDEventConstants.CDEVENTS_SPEC_VERSION);
}

/**
* @return the pipeline-run-queued-event schema Json
*/
@Override
public String eventSchema() {
return "{\n" +
" \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" +
" \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-queued-event\",\n" +
" \"properties\": {\n" +
" \"context\": {\n" +
" \"properties\": {\n" +
" \"version\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"source\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"type\": {\n" +
" \"type\": \"string\",\n" +
" \"enum\": [\n" +
" \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" +
" ],\n" +
" \"default\": \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" +
" },\n" +
" \"timestamp\": {\n" +
" \"type\": \"string\",\n" +
" \"format\": \"date-time\"\n" +
" }\n" +
" },\n" +
" \"additionalProperties\": false,\n" +
" \"type\": \"object\",\n" +
" \"required\": [\n" +
" \"version\",\n" +
" \"id\",\n" +
" \"source\",\n" +
" \"type\",\n" +
" \"timestamp\"\n" +
" ]\n" +
" },\n" +
" \"subject\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"source\": {\n" +
" \"type\": \"string\"\n" +
" },\n" +
" \"type\": {\n" +
" \"type\": \"string\",\n" +
" \"minLength\": 1\n" +
" },\n" +
" \"content\": {\n" +
" \"properties\": {\n" +
" \"pipelineName\": {\n" +
" \"type\": \"string\"\n" +
" },\n" +
" \"url\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" },\n" +
" \"additionalProperties\": false,\n" +
" \"type\": \"object\"\n" +
" }\n" +
" },\n" +
" \"additionalProperties\": false,\n" +
" \"type\": \"object\",\n" +
" \"required\": [\n" +
" \"id\",\n" +
" \"type\",\n" +
" \"content\"\n" +
" ]\n" +
" },\n" +
" \"customData\": {\n" +
" \"oneOf\": [\n" +
" {\n" +
" \"type\": \"object\"\n" +
" },\n" +
" {\n" +
" \"type\": \"string\",\n" +
" \"contentEncoding\": \"base64\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"customDataContentType\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" },\n" +
" \"additionalProperties\": false,\n" +
" \"type\": \"object\",\n" +
" \"required\": [\n" +
" \"context\",\n" +
" \"subject\"\n" +
" ]\n" +
"}";
}

/**
* @param subjectId
* sets the subject Id
*/
public void setSubjectId(String subjectId) {
getSubject().setId(subjectId);
}

/**
* @param subjectSource
* sets the pipeline source
*/
public void setSubjectSource(URI subjectSource) {
getSubject().setSource(subjectSource);
}

/**
* @param pipelineName
* sets the pipeline name
*/
public void setSubjectPipelineName(String pipelineName) {
getSubject().getContent().setPipelineName(pipelineName);
}

/**
* @param subjectUrl
* sets the pipeline URL
*/
public void setSubjectUrl(URI subjectUrl) {
getSubject().getContent().setUrl(subjectUrl);
}
}
Loading