Skip to content

Commit

Permalink
Merge branch 'master' into phgermanov/mta-build-artifacts-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
phgermanov authored Oct 29, 2024
2 parents 4227d23 + 183004a commit 85796f8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/gcpPublishEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error {
}

func createNewEvent(config *gcpPublishEventOptions) ([]byte, error) {
event, err := events.NewEvent(config.EventType, config.EventSource).CreateWithJSONData(config.EventData)
event, err := events.NewEvent(config.EventType, config.EventSource, "").CreateWithJSONData(config.EventData)
if err != nil {
return []byte{}, errors.Wrap(err, "failed to create new event")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/mavenBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func runMakeBOMGoal(config *mavenBuildOptions, utils maven.Utils) error {
}
defines = append(defines, createBOMConfig...)

goals := []string{"org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeBom"}
goals := []string{"org.cyclonedx:cyclonedx-maven-plugin:2.7.9:makeBom"}

if config.Flatten {
goals = append(goals, "flatten:flatten")
Expand All @@ -88,7 +88,7 @@ func runMakeBOMGoal(config *mavenBuildOptions, utils maven.Utils) error {
return err
}

func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomData, utils maven.Utils, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) error {
func runMavenBuild(config *mavenBuildOptions, _ *telemetry.CustomData, utils maven.Utils, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) error {

var flags = []string{"-update-snapshots", "--batch-mode"}

Expand All @@ -111,7 +111,7 @@ func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomDat

if config.CreateBOM {
// Append the makeAggregateBOM goal to the rest of the goals
goals = append(goals, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
goals = append(goals, "org.cyclonedx:cyclonedx-maven-plugin:2.7.9:makeAggregateBom")
createBOMConfig := []string{
"-DschemaVersion=1.4",
"-DincludeBomSerialNumber=true",
Expand Down
2 changes: 1 addition & 1 deletion cmd/mavenBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestMavenBuild(t *testing.T) {
assert.Nil(t, err)
if assert.Equal(t, 2, len(mockedUtils.Calls), "Expected two Maven invocations (default + makeAggregateBom)") {
assert.Equal(t, "mvn", mockedUtils.Calls[1].Exec)
assert.Contains(t, mockedUtils.Calls[0].Params, "org.cyclonedx:cyclonedx-maven-plugin:2.7.8:makeAggregateBom")
assert.Contains(t, mockedUtils.Calls[0].Params, "org.cyclonedx:cyclonedx-maven-plugin:2.7.9:makeAggregateBom")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DoutputName=bom-maven")
}
})
Expand Down
16 changes: 14 additions & 2 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type Event struct {
cloudEvent cloudevents.Event
eventType string
eventSource string
uuidData string
}

func NewEvent(eventType, eventSource string) Event {
func NewEvent(eventType, eventSource string, uuidString string) Event {
return Event{
eventType: eventType,
eventSource: eventSource,
uuidData: uuidString,
}
}

Expand All @@ -45,8 +47,14 @@ func (e Event) CreateWithJSONData(data string, opts ...Option) (Event, error) {

func (e Event) Create(data any, opts ...Option) Event {
e.cloudEvent = cloudevents.NewEvent("1.0")

if e.uuidData != "" {
e.cloudEvent.SetID(GetUUID(e.uuidData))
} else {
e.cloudEvent.SetID(uuid.New().String())
}

// set default values
e.cloudEvent.SetID(uuid.New().String())
e.cloudEvent.SetType(e.eventType)
e.cloudEvent.SetTime(time.Now())
e.cloudEvent.SetSource(e.eventSource)
Expand All @@ -58,6 +66,10 @@ func (e Event) Create(data any, opts ...Option) Event {
return e
}

func GetUUID(pipelineIdentifier string) string {
return uuid.NewMD5(uuid.NameSpaceOID, []byte(pipelineIdentifier)).String()
}

func (e Event) ToBytes() ([]byte, error) {
data, err := json.Marshal(e.cloudEvent)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestEventCreation(t *testing.T) {
t.Run("success", func(t *testing.T) {
// init
// test
event := NewEvent(mock.Anything, mock.Anything).Create(nil)
event := NewEvent(mock.Anything, mock.Anything, "").Create(nil)
// asserts
assert.Equal(t, mock.Anything, event.cloudEvent.Type())
assert.Equal(t, mock.Anything, event.cloudEvent.Source())
Expand All @@ -21,7 +21,7 @@ func TestEventCreation(t *testing.T) {
// init
testData := `{"testKey":"testValue"}`
// test
event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData)
event, err := NewEvent(mock.Anything, mock.Anything, "").CreateWithJSONData(testData)
// asserts
assert.NoError(t, err)
assert.Equal(t, string(event.cloudEvent.Data()), testData)
Expand All @@ -32,10 +32,25 @@ func TestEventCreation(t *testing.T) {
testData := `{"testKey": "testValue"}`
additionalData := `{"additionalKey": "additionalValue"}`
// test
event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData)
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"}`)
})
}

func TestGetUUID(t *testing.T) {
pipelineIdentifier := "pipelineIdentifier"
uuid := GetUUID(pipelineIdentifier)

if uuid == "" {
t.Fatalf("expected a UUID but got none")
}

uuid2 := GetUUID(pipelineIdentifier)
if uuid != uuid2 {
t.Fatalf("expected the same UUID but got different ones")
}

}

0 comments on commit 85796f8

Please sign in to comment.