Skip to content

Commit 5d9b7b6

Browse files
authored
Core CDEvents to render as CloudEvent and schema validation (#41)
* changes to Create PipelineRunQueuedCDEvent * adding other core events * update the java doc comment * updating subject source in unit test * updating spec version to 0.1.2
1 parent bffc18f commit 5d9b7b6

12 files changed

+1469
-12
lines changed

src/main/java/dev/cdevents/CDEvents.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public static String cdEventAsJson(CDEvent cdEvent) {
5050
* @return CloudEvent
5151
*/
5252
public static CloudEvent cdEventAsCloudEvent(CDEvent cdEvent) {
53-
54-
String cdEventJson = cdEventAsJson(cdEvent);
5553
if (!validateCDEvent(cdEvent)) {
5654
log.error("CDEvent validation failed against schema URL - {}", cdEvent.schemaURL());
5755
throw new CDEventsException("CDEvent validation failed against schema URL - " + cdEvent.schemaURL());
5856
}
57+
String cdEventJson = cdEventAsJson(cdEvent);
58+
log.info("CDEvent with type {} as json - {}", cdEvent.getContext().getType(), cdEventJson);
5959
CloudEvent ceToSend = new CloudEventBuilder()
6060
.withId(UUID.randomUUID().toString())
6161
.withSource(cdEvent.getContext().getSource())

src/main/java/dev/cdevents/constants/CDEventConstants.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ private CDEventConstants() {
88
/**
99
* CDEvents Version.
1010
*/
11-
public static final String CDEVENTS_SPEC_VERSION = "0.1.0";
11+
public static final String CDEVENTS_SPEC_VERSION = "0.1.2";
1212

1313
public enum SubjectType {
14+
15+
/**
16+
* Subject Type taskRun.
17+
*/
18+
TASKRUN("taskRun"),
1419
/**
1520
* Subject Type pipelineRun.
1621
*/

src/main/java/dev/cdevents/events/PipelineRunFinishedCDEvent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,13 @@ public String schemaURL() {
5858
public String eventSchema() {
5959
return "{\n" +
6060
" \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" +
61-
" \"$id\": \"https://cdevents.dev/0.1.0/schema/pipeline-run-finished-event\",\n" +
61+
" \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-finished-event\",\n" +
6262
" \"properties\": {\n" +
6363
" \"context\": {\n" +
6464
" \"properties\": {\n" +
6565
" \"version\": {\n" +
6666
" \"type\": \"string\",\n" +
67-
" \"enum\": [\n" +
68-
" \"0.1.0\"\n" +
69-
" ],\n" +
70-
" \"default\": \"0.1.0\"\n" +
67+
" \"minLength\": 1\n" +
7168
" },\n" +
7269
" \"id\": {\n" +
7370
" \"type\": \"string\",\n" +
@@ -79,7 +76,10 @@ public String eventSchema() {
7976
" },\n" +
8077
" \"type\": {\n" +
8178
" \"type\": \"string\",\n" +
82-
" \"minLength\": 1\n" +
79+
" \"enum\": [\n" +
80+
" \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" +
81+
" ],\n" +
82+
" \"default\": \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" +
8383
" },\n" +
8484
" \"timestamp\": {\n" +
8585
" \"type\": \"string\",\n" +
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package dev.cdevents.events;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import dev.cdevents.constants.CDEventConstants;
5+
import dev.cdevents.models.CDEvent;
6+
import dev.cdevents.models.PipelineRunQueuedSubject;
7+
8+
import java.net.URI;
9+
10+
public class PipelineRunQueuedCDEvent extends CDEvent {
11+
12+
private static final String CDEVENT_VERSION = "0.1.0";
13+
@JsonProperty(required = true)
14+
private PipelineRunQueuedSubject subject;
15+
16+
/**
17+
* Constructor to init CDEvent and set the Subject for {@link PipelineRunQueuedCDEvent}.
18+
*/
19+
public PipelineRunQueuedCDEvent() {
20+
initCDEvent(currentCDEventType());
21+
setSubject(new PipelineRunQueuedSubject(CDEventConstants.SubjectType.PIPELINERUN));
22+
}
23+
24+
/**
25+
* @return subject
26+
*/
27+
public PipelineRunQueuedSubject getSubject() {
28+
return subject;
29+
}
30+
31+
/**
32+
* @param subject
33+
*/
34+
public void setSubject(PipelineRunQueuedSubject subject) {
35+
this.subject = subject;
36+
}
37+
38+
/**
39+
* @return the PipelineRunQueuedEvent type
40+
*/
41+
@Override
42+
public String currentCDEventType() {
43+
return CDEventConstants.CDEventTypes.PipelineRunQueuedEvent.getEventType().concat(CDEVENT_VERSION);
44+
}
45+
46+
/**
47+
* @return the pipeline-run-queued-event schema URL
48+
*/
49+
@Override
50+
public String schemaURL() {
51+
return String.format("https://cdevents.dev/%s/schema/pipeline-run-queued-event", CDEventConstants.CDEVENTS_SPEC_VERSION);
52+
}
53+
54+
/**
55+
* @return the pipeline-run-queued-event schema Json
56+
*/
57+
@Override
58+
public String eventSchema() {
59+
return "{\n" +
60+
" \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" +
61+
" \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-queued-event\",\n" +
62+
" \"properties\": {\n" +
63+
" \"context\": {\n" +
64+
" \"properties\": {\n" +
65+
" \"version\": {\n" +
66+
" \"type\": \"string\",\n" +
67+
" \"minLength\": 1\n" +
68+
" },\n" +
69+
" \"id\": {\n" +
70+
" \"type\": \"string\",\n" +
71+
" \"minLength\": 1\n" +
72+
" },\n" +
73+
" \"source\": {\n" +
74+
" \"type\": \"string\",\n" +
75+
" \"minLength\": 1\n" +
76+
" },\n" +
77+
" \"type\": {\n" +
78+
" \"type\": \"string\",\n" +
79+
" \"enum\": [\n" +
80+
" \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" +
81+
" ],\n" +
82+
" \"default\": \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" +
83+
" },\n" +
84+
" \"timestamp\": {\n" +
85+
" \"type\": \"string\",\n" +
86+
" \"format\": \"date-time\"\n" +
87+
" }\n" +
88+
" },\n" +
89+
" \"additionalProperties\": false,\n" +
90+
" \"type\": \"object\",\n" +
91+
" \"required\": [\n" +
92+
" \"version\",\n" +
93+
" \"id\",\n" +
94+
" \"source\",\n" +
95+
" \"type\",\n" +
96+
" \"timestamp\"\n" +
97+
" ]\n" +
98+
" },\n" +
99+
" \"subject\": {\n" +
100+
" \"properties\": {\n" +
101+
" \"id\": {\n" +
102+
" \"type\": \"string\",\n" +
103+
" \"minLength\": 1\n" +
104+
" },\n" +
105+
" \"source\": {\n" +
106+
" \"type\": \"string\"\n" +
107+
" },\n" +
108+
" \"type\": {\n" +
109+
" \"type\": \"string\",\n" +
110+
" \"minLength\": 1\n" +
111+
" },\n" +
112+
" \"content\": {\n" +
113+
" \"properties\": {\n" +
114+
" \"pipelineName\": {\n" +
115+
" \"type\": \"string\"\n" +
116+
" },\n" +
117+
" \"url\": {\n" +
118+
" \"type\": \"string\"\n" +
119+
" }\n" +
120+
" },\n" +
121+
" \"additionalProperties\": false,\n" +
122+
" \"type\": \"object\"\n" +
123+
" }\n" +
124+
" },\n" +
125+
" \"additionalProperties\": false,\n" +
126+
" \"type\": \"object\",\n" +
127+
" \"required\": [\n" +
128+
" \"id\",\n" +
129+
" \"type\",\n" +
130+
" \"content\"\n" +
131+
" ]\n" +
132+
" },\n" +
133+
" \"customData\": {\n" +
134+
" \"oneOf\": [\n" +
135+
" {\n" +
136+
" \"type\": \"object\"\n" +
137+
" },\n" +
138+
" {\n" +
139+
" \"type\": \"string\",\n" +
140+
" \"contentEncoding\": \"base64\"\n" +
141+
" }\n" +
142+
" ]\n" +
143+
" },\n" +
144+
" \"customDataContentType\": {\n" +
145+
" \"type\": \"string\"\n" +
146+
" }\n" +
147+
" },\n" +
148+
" \"additionalProperties\": false,\n" +
149+
" \"type\": \"object\",\n" +
150+
" \"required\": [\n" +
151+
" \"context\",\n" +
152+
" \"subject\"\n" +
153+
" ]\n" +
154+
"}";
155+
}
156+
157+
/**
158+
* @param subjectId
159+
* sets the subject Id
160+
*/
161+
public void setSubjectId(String subjectId) {
162+
getSubject().setId(subjectId);
163+
}
164+
165+
/**
166+
* @param subjectSource
167+
* sets the pipeline source
168+
*/
169+
public void setSubjectSource(URI subjectSource) {
170+
getSubject().setSource(subjectSource);
171+
}
172+
173+
/**
174+
* @param pipelineName
175+
* sets the pipeline name
176+
*/
177+
public void setSubjectPipelineName(String pipelineName) {
178+
getSubject().getContent().setPipelineName(pipelineName);
179+
}
180+
181+
/**
182+
* @param subjectUrl
183+
* sets the pipeline URL
184+
*/
185+
public void setSubjectUrl(URI subjectUrl) {
186+
getSubject().getContent().setUrl(subjectUrl);
187+
}
188+
}

0 commit comments

Comments
 (0)