Skip to content

Commit

Permalink
feat(gcpPublishEvent): Add additionalEventData param (#4928)
Browse files Browse the repository at this point in the history
* add additionalEventData param

* fix double JSON marshalling

* go generate

* add logging of events and config

* change logging to debug

* add event log

* fix CloudEvent JSON type

* apply review feedback

* fix log

* add missing error handling

---------

Co-authored-by: jliempt <>
  • Loading branch information
jliempt authored May 17, 2024
1 parent 065fedb commit 7de5fdf
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
23 changes: 21 additions & 2 deletions cmd/gcpPublishEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error {
log.Entry().WithError(err).Warning("Cannot infer config from CI environment")
}

data, err = events.NewEvent(config.EventType, config.EventSource).CreateWithJSONData(config.EventData).ToBytes()
data, err = createNewEvent(config)
if err != nil {
return errors.Wrap(err, "failed to create event data")
}
Expand All @@ -101,7 +101,26 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error {
return errors.Wrap(err, "failed to publish event")
}

log.Entry().Info("event published successfully!")
log.Entry().Infof("Event published successfully! With topic: %s", config.Topic)

return nil
}

func createNewEvent(config *gcpPublishEventOptions) ([]byte, error) {
event, err := events.NewEvent(config.EventType, config.EventSource).CreateWithJSONData(config.EventData)
if err != nil {
return []byte{}, errors.Wrap(err, "failed to create new event")
}

err = event.AddToCloudEventData(config.AdditionalEventData)
if err != nil {
log.Entry().Debugf("couldn't add additionalData to cloud event data: %s", err)
}

eventBytes, err := event.ToBytes()
if err != nil {
return []byte{}, errors.Wrap(err, "casting event to bytes failed")
}
log.Entry().Debugf("CloudEvent created: %s", string(eventBytes))
return eventBytes, nil
}
11 changes: 11 additions & 0 deletions cmd/gcpPublishEvent_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 36 additions & 3 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ func NewEvent(eventType, eventSource string) Event {
}
}

func (e Event) CreateWithJSONData(data string, opts ...Option) Event {
return e.Create(data, opts...)
func (e Event) CreateWithJSONData(data string, opts ...Option) (Event, error) {
// passing a string to e.cloudEvent.SetData will result in the string being marshalled, ending up with double escape characters
// therefore pass a map instead
var dataMap map[string]interface{}
if data != "" {
err := json.Unmarshal([]byte(data), &dataMap)
if err != nil {
return e, errors.Wrap(err, "eventData is an invalid JSON")
}
}
return e.Create(dataMap, opts...), nil
}

func (e Event) Create(data any, opts ...Option) Event {
Expand All @@ -46,7 +55,6 @@ func (e Event) Create(data any, opts ...Option) Event {
for _, applyOpt := range opts {
applyOpt(e.cloudEvent.Context.AsV1())
}

return e
}

Expand All @@ -57,3 +65,28 @@ func (e Event) ToBytes() ([]byte, error) {
}
return data, nil
}

func (e *Event) AddToCloudEventData(additionalDataString string) error {
if additionalDataString == "" {
return nil
}

var additionalData map[string]interface{}
err := json.Unmarshal([]byte(additionalDataString), &additionalData)
if err != nil {
errors.Wrap(err, "couldn't add additional data to cloud event")
}

var newEventData map[string]interface{}
err = json.Unmarshal(e.cloudEvent.DataEncoded, &newEventData)
if err != nil {
errors.Wrap(err, "couldn't add additional data to cloud event")
}

for k, v := range additionalData {
newEventData[k] = v
}

e.cloudEvent.SetData("application/json", newEventData)
return nil
}
22 changes: 22 additions & 0 deletions pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,26 @@ func TestEventCreation(t *testing.T) {
assert.Equal(t, mock.Anything, event.cloudEvent.Type())
assert.Equal(t, mock.Anything, event.cloudEvent.Source())
})

t.Run("CreateWithJSONData - no additional data", func(t *testing.T) {
// init
testData := `{"testKey":"testValue"}`
// test
event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData)
// asserts
assert.NoError(t, err)
assert.Equal(t, string(event.cloudEvent.Data()), testData)
})

t.Run("CreateWithJSONData + AddToCloudEventData", func(t *testing.T) {
// init
testData := `{"testKey": "testValue"}`
additionalData := `{"additionalKey": "additionalValue"}`
// test
event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData)
event.AddToCloudEventData(additionalData)
// asserts
assert.NoError(t, err)
assert.Equal(t, string(event.cloudEvent.Data()), `{"additionalKey":"additionalValue","testKey":"testValue"}`)
})
}
2 changes: 2 additions & 0 deletions pkg/gcp/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"

"github.com/SAP/jenkins-library/pkg/log"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -38,6 +39,7 @@ func Publish(projectNumber string, topic string, token string, key string, data
if err != nil {
return errors.Wrap(err, "failed to marshal event")
}
log.Entry().Debugf("created pubsub event: %s", string(eventBytes))

// create request
request, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf(api_url, projectNumber, topic), bytes.NewReader(eventBytes))
Expand Down
5 changes: 5 additions & 0 deletions resources/metadata/gcpPublishEvent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ spec:
resourceRef:
- name: commonPipelineEnvironment
param: custom/eventData
- name: additionalEventData
description: Data (formatted as JSON string) to add to eventData. This can be used to enrich eventData that comes from the pipeline environment.
type: string
scope:
- PARAMETERS

0 comments on commit 7de5fdf

Please sign in to comment.