Skip to content

Commit

Permalink
add ordering key for pubsub event
Browse files Browse the repository at this point in the history
  • Loading branch information
CCFenner committed May 3, 2024
1 parent 34ef952 commit 6733c38
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
14 changes: 10 additions & 4 deletions cmd/gcpPublishEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/SAP/jenkins-library/pkg/events"
"github.com/SAP/jenkins-library/pkg/gcp"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/SAP/jenkins-library/pkg/telemetry"

"github.com/pkg/errors"
Expand All @@ -13,7 +14,7 @@ type gcpPublishEventUtils interface {
GetConfig() *gcpPublishEventOptions
GetOIDCTokenByValidation(roleID string) (string, error)
GetFederatedToken(projectNumber, pool, provider, token string) (string, error)
Publish(projectNumber string, topic string, token string, data []byte) error
Publish(projectNumber string, topic string, token string, key string, data []byte) error
}

type gcpPublishEventUtilsBundle struct {
Expand All @@ -28,8 +29,8 @@ func (g gcpPublishEventUtilsBundle) GetFederatedToken(projectNumber, pool, provi
return gcp.GetFederatedToken(projectNumber, pool, provider, token)
}

func (g gcpPublishEventUtilsBundle) Publish(projectNumber string, topic string, token string, data []byte) error {
return gcp.Publish(projectNumber, topic, token, data)
func (g gcpPublishEventUtilsBundle) Publish(projectNumber string, topic string, token string, key string, data []byte) error {
return gcp.Publish(projectNumber, topic, token, key, data)
}

// to be implemented through another PR!
Expand All @@ -55,6 +56,11 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error {
var data []byte
var err error

provider, err := orchestrator.GetOrchestratorConfigProvider(nil)
if err != nil {
log.Entry().WithError(err).Warning("Cannot infer config from CI environment")
}

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

err = utils.Publish(config.GcpProjectNumber, config.Topic, token, data)
err = utils.Publish(config.GcpProjectNumber, config.Topic, token, provider.BuildURL(), data)
if err != nil {
return errors.Wrap(err, "failed to publish event")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gcpPublishEvent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (g *mockGcpPublishEventUtilsBundle) GetFederatedToken(projectNumber, pool,
return "testFederatedToken123", nil
}

func (g *mockGcpPublishEventUtilsBundle) Publish(projectNumber string, topic string, token string, data []byte) error {
func (g *mockGcpPublishEventUtilsBundle) Publish(projectNumber string, topic string, token string, key string, data []byte) error {
if topic == "goodTestCase" {
return nil
} else if topic == "badTestCase" {
Expand Down
8 changes: 5 additions & 3 deletions pkg/gcp/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ const api_url = "https://pubsub.googleapis.com/v1/projects/%s/topics/%s:publish"

// https://pkg.go.dev/cloud.google.com/go/pubsub#Message
type EventMessage struct {
Data []byte `json:"data"`
Data []byte `json:"data"`
OrderingKey string `json:"orderingKey"`
}

type Event struct {
Messages []EventMessage `json:"messages"`
}

func Publish(projectNumber string, topic string, token string, data []byte) error {
func Publish(projectNumber string, topic string, token string, key string, data []byte) error {
ctx := context.Background()

// build event
event := Event{
Messages: []EventMessage{{
Data: data,
Data: data,
OrderingKey: key,
}},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gcp/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPublish(t *testing.T) {
)

// test
err := Publish(projectNumber, topic, token, data)
err := Publish(projectNumber, topic, token, mock.Anything, data)
// asserts
assert.NoError(t, err)
})
Expand Down

0 comments on commit 6733c38

Please sign in to comment.