Skip to content

Commit 42f09f4

Browse files
committed
Add empty data model events from the spec
Add all remaining events from the spec, with limitations: - empty data model (no extra fields in the subject) - no event specific test cases yet Apart from that, the events code is complete and the factory is updated to include all events, but commented out, so that we can only produce events that are ready. Signed-off-by: Andrea Frittoli <[email protected]>
1 parent 78a4caf commit 42f09f4

34 files changed

+3706
-69
lines changed

hack/add-event.sh

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env bash
2+
3+
# This script can be used to add a new event with empty data model
4+
# based on the subject and predicate names
5+
6+
# Usage:
7+
# ./add-event.sh <subject> <predicate>
8+
#
9+
# Both subect and predicate should be give in camelcase
10+
11+
BASE_DIR="$( cd "$( dirname "$0" )/.." >/dev/null 2>&1 && pwd )"
12+
13+
set -e
14+
15+
SUBJECT=$1
16+
PREDICATE=$2
17+
SUBJECT_LOWER_CAMEL=${SUBJECT,}
18+
SUBJECT_UPPER_CAMEL=${SUBJECT^}
19+
SUBJECT_LOWER=${SUBJECT,,}
20+
PREDICATE_LOWER_CAMEL=${PREDICATE,}
21+
PREDICATE_UPPER_CAMEL=${PREDICATE^}
22+
PREDICATE_LOWER=${PREDICATE,,}
23+
24+
cat > "${BASE_DIR}/pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER}.go" << EOF
25+
/*
26+
Copyright 2022 The CDEvents Authors
27+
28+
Licensed under the Apache License, Version 2.0 (the "License");
29+
you may not use this file except in compliance with the License.
30+
You may obtain a copy of the License at
31+
32+
http://www.apache.org/licenses/LICENSE-2.0
33+
34+
Unless required by applicable law or agreed to in writing, software
35+
distributed under the License is distributed on an "AS IS" BASIS,
36+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37+
See the License for the specific language governing permissions and
38+
limitations under the License.
39+
40+
SPDX-License-Identifier: Apache-2.0
41+
*/
42+
43+
package api
44+
45+
import (
46+
"time"
47+
)
48+
49+
const (
50+
// ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} event
51+
${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1 CDEventType = "dev.cdevents.${SUBJECT_LOWER}.${PREDICATE_LOWER}.v1"
52+
)
53+
54+
type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}SubjectContent struct {}
55+
56+
type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject struct {
57+
SubjectBase
58+
Content ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}SubjectContent \`json:"content"\`
59+
}
60+
61+
func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject) GetEventType() CDEventType {
62+
return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1
63+
}
64+
65+
func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject) GetSubjectType() SubjectType {
66+
return ${SUBJECT_UPPER_CAMEL}SubjectType
67+
}
68+
69+
type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event struct {
70+
Context Context \`json:"context"\`
71+
Subject ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject \`json:"subject"\`
72+
}
73+
74+
// CDEventsReader implementation
75+
76+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetType() CDEventType {
77+
return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1
78+
}
79+
80+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetVersion() string {
81+
return CDEventsSpecVersion
82+
}
83+
84+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetId() string {
85+
return e.Context.Id
86+
}
87+
88+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSource() string {
89+
return e.Context.Source
90+
}
91+
92+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetTimestamp() time.Time {
93+
return e.Context.Timestamp
94+
}
95+
96+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubjectId() string {
97+
return e.Subject.Id
98+
}
99+
100+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubjectSource() string {
101+
return e.Subject.Source
102+
}
103+
104+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubject() Subject {
105+
return e.Subject
106+
}
107+
108+
// CDEventsWriter implementation
109+
110+
func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetId(id string) {
111+
e.Context.Id = id
112+
}
113+
114+
func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSource(source string) {
115+
e.Context.Source = source
116+
// Default the subject source to the event source
117+
if e.Subject.Source == "" {
118+
e.Subject.Source = source
119+
}
120+
}
121+
122+
func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetTimestamp(timestamp time.Time) {
123+
e.Context.Timestamp = timestamp
124+
}
125+
126+
func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSubjectId(subjectId string) {
127+
e.Subject.Id = subjectId
128+
}
129+
130+
func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSubjectSource(subjectSource string) {
131+
e.Subject.Source = subjectSource
132+
}
133+
134+
func new${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event() CDEvent {
135+
return &${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event{
136+
Context: Context{
137+
Type: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1,
138+
Version: CDEventsSpecVersion,
139+
},
140+
Subject: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject{},
141+
}
142+
}
143+
EOF
144+
145+
echo "Created ${BASE_DIR}/pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER}.go"

hack/add-events.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
BASE_DIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
4+
cd $BASE_DIR
5+
6+
./add-event.sh repository created
7+
./add-event.sh repository modified
8+
./add-event.sh repository deleted
9+
./add-event.sh branch created
10+
./add-event.sh branch deleted
11+
./add-event.sh change created
12+
./add-event.sh change updated
13+
./add-event.sh change reviewed
14+
./add-event.sh change merged
15+
./add-event.sh change abandoned
16+
./add-event.sh build started
17+
./add-event.sh build queued
18+
./add-event.sh build finished
19+
./add-event.sh testCase started
20+
./add-event.sh testCase queued
21+
./add-event.sh testCase finished
22+
./add-event.sh testSuite started
23+
./add-event.sh testSuite queued
24+
./add-event.sh testSuite finished
25+
./add-event.sh artifact packaged
26+
./add-event.sh artifact published
27+
./add-event.sh environment created
28+
./add-event.sh environment modified
29+
./add-event.sh environment deleted
30+
./add-event.sh service deployed
31+
./add-event.sh service upgraded
32+
./add-event.sh service rolledback
33+
./add-event.sh service removed
34+
./add-event.sh service published

pkg/api/artifactpackaged.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright 2022 The CDEvents Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
package api
20+
21+
import (
22+
"time"
23+
)
24+
25+
const (
26+
// ArtifactPackaged event
27+
ArtifactPackagedEventV1 CDEventType = "dev.cdevents.artifact.packaged.v1"
28+
)
29+
30+
type ArtifactPackagedSubjectContent struct {}
31+
32+
type ArtifactPackagedSubject struct {
33+
SubjectBase
34+
Content ArtifactPackagedSubjectContent `json:"content"`
35+
}
36+
37+
func (sc ArtifactPackagedSubject) GetEventType() CDEventType {
38+
return ArtifactPackagedEventV1
39+
}
40+
41+
func (sc ArtifactPackagedSubject) GetSubjectType() SubjectType {
42+
return ArtifactSubjectType
43+
}
44+
45+
type ArtifactPackagedEvent struct {
46+
Context Context `json:"context"`
47+
Subject ArtifactPackagedSubject `json:"subject"`
48+
}
49+
50+
// CDEventsReader implementation
51+
52+
func (e ArtifactPackagedEvent) GetType() CDEventType {
53+
return ArtifactPackagedEventV1
54+
}
55+
56+
func (e ArtifactPackagedEvent) GetVersion() string {
57+
return CDEventsSpecVersion
58+
}
59+
60+
func (e ArtifactPackagedEvent) GetId() string {
61+
return e.Context.Id
62+
}
63+
64+
func (e ArtifactPackagedEvent) GetSource() string {
65+
return e.Context.Source
66+
}
67+
68+
func (e ArtifactPackagedEvent) GetTimestamp() time.Time {
69+
return e.Context.Timestamp
70+
}
71+
72+
func (e ArtifactPackagedEvent) GetSubjectId() string {
73+
return e.Subject.Id
74+
}
75+
76+
func (e ArtifactPackagedEvent) GetSubjectSource() string {
77+
return e.Subject.Source
78+
}
79+
80+
func (e ArtifactPackagedEvent) GetSubject() Subject {
81+
return e.Subject
82+
}
83+
84+
// CDEventsWriter implementation
85+
86+
func (e *ArtifactPackagedEvent) SetId(id string) {
87+
e.Context.Id = id
88+
}
89+
90+
func (e *ArtifactPackagedEvent) SetSource(source string) {
91+
e.Context.Source = source
92+
// Default the subject source to the event source
93+
if e.Subject.Source == "" {
94+
e.Subject.Source = source
95+
}
96+
}
97+
98+
func (e *ArtifactPackagedEvent) SetTimestamp(timestamp time.Time) {
99+
e.Context.Timestamp = timestamp
100+
}
101+
102+
func (e *ArtifactPackagedEvent) SetSubjectId(subjectId string) {
103+
e.Subject.Id = subjectId
104+
}
105+
106+
func (e *ArtifactPackagedEvent) SetSubjectSource(subjectSource string) {
107+
e.Subject.Source = subjectSource
108+
}
109+
110+
func newArtifactPackagedEvent() CDEvent {
111+
return &ArtifactPackagedEvent{
112+
Context: Context{
113+
Type: ArtifactPackagedEventV1,
114+
Version: CDEventsSpecVersion,
115+
},
116+
Subject: ArtifactPackagedSubject{},
117+
}
118+
}

0 commit comments

Comments
 (0)