Skip to content
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

Add empty data model events from the spec #3

Merged
merged 1 commit into from
Aug 18, 2022
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
145 changes: 145 additions & 0 deletions hack/add-event.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env bash

# This script can be used to add a new event with empty data model
# based on the subject and predicate names

# Usage:
# ./add-event.sh <subject> <predicate>
#
# Both subect and predicate should be give in camelcase

BASE_DIR="$( cd "$( dirname "$0" )/.." >/dev/null 2>&1 && pwd )"

set -e

SUBJECT=$1
PREDICATE=$2
SUBJECT_LOWER_CAMEL=${SUBJECT,}
SUBJECT_UPPER_CAMEL=${SUBJECT^}
SUBJECT_LOWER=${SUBJECT,,}
PREDICATE_LOWER_CAMEL=${PREDICATE,}
PREDICATE_UPPER_CAMEL=${PREDICATE^}
PREDICATE_LOWER=${PREDICATE,,}

cat > "${BASE_DIR}/pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER}.go" << EOF
/*
Copyright 2022 The CDEvents Authors

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 api

import (
"time"
)

const (
// ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} event
${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1 CDEventType = "dev.cdevents.${SUBJECT_LOWER}.${PREDICATE_LOWER}.v1"
)

type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}SubjectContent struct {}

type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject struct {
SubjectBase
Content ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}SubjectContent \`json:"content"\`
}

func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject) GetEventType() CDEventType {
return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1
}

func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject) GetSubjectType() SubjectType {
return ${SUBJECT_UPPER_CAMEL}SubjectType
}

type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event struct {
Context Context \`json:"context"\`
Subject ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject \`json:"subject"\`
}

// CDEventsReader implementation

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetType() CDEventType {
return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetVersion() string {
return CDEventsSpecVersion
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetId() string {
return e.Context.Id
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSource() string {
return e.Context.Source
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetTimestamp() time.Time {
return e.Context.Timestamp
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubjectId() string {
return e.Subject.Id
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubjectSource() string {
return e.Subject.Source
}

func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSubject() Subject {
return e.Subject
}

// CDEventsWriter implementation

func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetId(id string) {
e.Context.Id = id
}

func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSource(source string) {
e.Context.Source = source
// Default the subject source to the event source
if e.Subject.Source == "" {
e.Subject.Source = source
}
}

func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetTimestamp(timestamp time.Time) {
e.Context.Timestamp = timestamp
}

func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSubjectId(subjectId string) {
e.Subject.Id = subjectId
}

func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetSubjectSource(subjectSource string) {
e.Subject.Source = subjectSource
}

func new${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event() CDEvent {
return &${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event{
Context: Context{
Type: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1,
Version: CDEventsSpecVersion,
},
Subject: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Subject{},
}
}
EOF

echo "Created ${BASE_DIR}/pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER}.go"
34 changes: 34 additions & 0 deletions hack/add-events.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

BASE_DIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
cd $BASE_DIR

./add-event.sh repository created
./add-event.sh repository modified
./add-event.sh repository deleted
./add-event.sh branch created
./add-event.sh branch deleted
./add-event.sh change created
./add-event.sh change updated
./add-event.sh change reviewed
./add-event.sh change merged
./add-event.sh change abandoned
./add-event.sh build started
./add-event.sh build queued
./add-event.sh build finished
./add-event.sh testCase started
./add-event.sh testCase queued
./add-event.sh testCase finished
./add-event.sh testSuite started
./add-event.sh testSuite queued
./add-event.sh testSuite finished
./add-event.sh artifact packaged
./add-event.sh artifact published
./add-event.sh environment created
./add-event.sh environment modified
./add-event.sh environment deleted
./add-event.sh service deployed
./add-event.sh service upgraded
./add-event.sh service rolledback
./add-event.sh service removed
./add-event.sh service published
118 changes: 118 additions & 0 deletions pkg/api/artifactpackaged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright 2022 The CDEvents Authors

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 api

import (
"time"
)

const (
// ArtifactPackaged event
ArtifactPackagedEventV1 CDEventType = "dev.cdevents.artifact.packaged.v1"
)

type ArtifactPackagedSubjectContent struct{}

type ArtifactPackagedSubject struct {
SubjectBase
Content ArtifactPackagedSubjectContent `json:"content"`
}

func (sc ArtifactPackagedSubject) GetEventType() CDEventType {
return ArtifactPackagedEventV1
}

func (sc ArtifactPackagedSubject) GetSubjectType() SubjectType {
return ArtifactSubjectType
}

type ArtifactPackagedEvent struct {
Context Context `json:"context"`
Subject ArtifactPackagedSubject `json:"subject"`
}

// CDEventsReader implementation

func (e ArtifactPackagedEvent) GetType() CDEventType {
return ArtifactPackagedEventV1
}

func (e ArtifactPackagedEvent) GetVersion() string {
return CDEventsSpecVersion
}

func (e ArtifactPackagedEvent) GetId() string {
return e.Context.Id
}

func (e ArtifactPackagedEvent) GetSource() string {
return e.Context.Source
}

func (e ArtifactPackagedEvent) GetTimestamp() time.Time {
return e.Context.Timestamp
}

func (e ArtifactPackagedEvent) GetSubjectId() string {
return e.Subject.Id
}

func (e ArtifactPackagedEvent) GetSubjectSource() string {
return e.Subject.Source
}

func (e ArtifactPackagedEvent) GetSubject() Subject {
return e.Subject
}

// CDEventsWriter implementation

func (e *ArtifactPackagedEvent) SetId(id string) {
e.Context.Id = id
}

func (e *ArtifactPackagedEvent) SetSource(source string) {
e.Context.Source = source
// Default the subject source to the event source
if e.Subject.Source == "" {
e.Subject.Source = source
}
}

func (e *ArtifactPackagedEvent) SetTimestamp(timestamp time.Time) {
e.Context.Timestamp = timestamp
}

func (e *ArtifactPackagedEvent) SetSubjectId(subjectId string) {
e.Subject.Id = subjectId
}

func (e *ArtifactPackagedEvent) SetSubjectSource(subjectSource string) {
e.Subject.Source = subjectSource
}

func newArtifactPackagedEvent() CDEvent {
return &ArtifactPackagedEvent{
Context: Context{
Type: ArtifactPackagedEventV1,
Version: CDEventsSpecVersion,
},
Subject: ArtifactPackagedSubject{},
}
}
Loading