diff --git a/pkg/api/models/keptn_context_extended_c_e.go b/pkg/api/models/keptn_context_extended_c_e.go index 1bd04d97..25ca43b1 100644 --- a/pkg/api/models/keptn_context_extended_c_e.go +++ b/pkg/api/models/keptn_context_extended_c_e.go @@ -43,6 +43,9 @@ type KeptnContextExtendedCE struct { // triggeredid Triggeredid string `json:"triggeredid,omitempty"` + // gitcommitid + Gitcommitid string `json:"gitcommitid,omitempty"` + // type // Required: true Type *string `json:"type"` diff --git a/pkg/api/models/keptn_context_extended_c_e_test.go b/pkg/api/models/keptn_context_extended_c_e_test.go index d0228413..2c033525 100644 --- a/pkg/api/models/keptn_context_extended_c_e_test.go +++ b/pkg/api/models/keptn_context_extended_c_e_test.go @@ -2,12 +2,13 @@ package models_test import ( "fmt" + "testing" + "time" + "github.com/keptn/go-utils/pkg/api/models" "github.com/keptn/go-utils/pkg/lib/v0_2_0" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" - "time" ) func TestKeptnContextExtendedCE_Validate(t *testing.T) { @@ -24,6 +25,7 @@ func TestKeptnContextExtendedCE_Validate(t *testing.T) { Specversion string Time time.Time Triggeredid string + Gitcommitid string Type *string } tests := []struct { @@ -91,6 +93,7 @@ func TestKeptnContextExtendedCE_Validate(t *testing.T) { Specversion: tt.fields.Specversion, Time: tt.fields.Time, Triggeredid: tt.fields.Triggeredid, + Gitcommitid: tt.fields.Gitcommitid, Type: tt.fields.Type, } if err := ce.Validate(); (err != nil) != tt.wantErr { diff --git a/pkg/api/models/log.go b/pkg/api/models/log.go index 3534f404..9d7df37c 100644 --- a/pkg/api/models/log.go +++ b/pkg/api/models/log.go @@ -12,6 +12,7 @@ type LogEntry struct { KeptnContext string `json:"shkeptncontext" bson:"shkeptncontext"` Task string `json:"task" bson:"task"` TriggeredID string `json:"triggeredid" bson:"triggeredid"` + GitCommitID string `json:"gitcommitid" bson:"gitcommitid"` } type GetLogsParams struct { diff --git a/pkg/lib/v0_2_0/events.go b/pkg/lib/v0_2_0/events.go index 94b868e9..275179bd 100644 --- a/pkg/lib/v0_2_0/events.go +++ b/pkg/lib/v0_2_0/events.go @@ -39,6 +39,7 @@ const keptnInvalidatedEventSuffix = ".invalidated" const keptnContextCEExtension = "shkeptncontext" const keptnSpecVersionCEExtension = "shkeptnspecversion" const triggeredIDCEExtension = "triggeredid" +const keptnGitCommitIDCEExtension = "gitcommitid" // HTTPEventSender sends CloudEvents via HTTP type HTTPEventSender struct { @@ -395,6 +396,12 @@ func (eb *KeptnEventBuilder) WithTriggeredID(triggeredID string) *KeptnEventBuil return eb } +// WithGitCommitID can be used to set the git commit ID +func (eb *KeptnEventBuilder) WithGitCommitID(gitCommitID string) *KeptnEventBuilder { + eb.Gitcommitid = gitCommitID + return eb +} + // WithID can be used to override the ID, which is auto generated by default func (eb *KeptnEventBuilder) WithID(id string) *KeptnEventBuilder { eb.ID = id @@ -412,6 +419,7 @@ func ToCloudEvent(keptnEvent models.KeptnContextExtendedCE) cloudevents.Event { event.SetData(cloudevents.ApplicationJSON, keptnEvent.Data) event.SetExtension(keptnContextCEExtension, keptnEvent.Shkeptncontext) event.SetExtension(triggeredIDCEExtension, keptnEvent.Triggeredid) + event.SetExtension(keptnGitCommitIDCEExtension, keptnEvent.Gitcommitid) event.SetExtension(keptnSpecVersionCEExtension, keptnEvent.Shkeptnspecversion) return event } @@ -427,6 +435,9 @@ func ToKeptnEvent(event cloudevents.Event) (models.KeptnContextExtendedCE, error var keptnSpecVersion string event.ExtensionAs(keptnSpecVersionCEExtension, &keptnSpecVersion) + var gitCommitID string + event.ExtensionAs(keptnGitCommitIDCEExtension, &gitCommitID) + var data interface{} event.DataAs(&data) @@ -440,6 +451,7 @@ func ToKeptnEvent(event cloudevents.Event) (models.KeptnContextExtendedCE, error Specversion: event.SpecVersion(), Time: event.Time(), Triggeredid: triggeredID, + Gitcommitid: gitCommitID, Type: strutils.Stringp(event.Type()), } diff --git a/pkg/lib/v0_2_0/events_test.go b/pkg/lib/v0_2_0/events_test.go index f8ce55b4..1413c182 100644 --- a/pkg/lib/v0_2_0/events_test.go +++ b/pkg/lib/v0_2_0/events_test.go @@ -1,6 +1,11 @@ package v0_2_0 import ( + "net/http" + "net/http/httptest" + "testing" + "time" + "github.com/keptn/go-utils/config" "github.com/keptn/go-utils/pkg/api/models" api "github.com/keptn/go-utils/pkg/api/utils" @@ -8,10 +13,6 @@ import ( "github.com/keptn/go-utils/pkg/lib/keptn" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "net/http" - "net/http/httptest" - "testing" - "time" cloudevents "github.com/cloudevents/sdk-go/v2" ) @@ -280,6 +281,7 @@ func TestCreateSimpleKeptnEvent(t *testing.T) { require.NotEmpty(t, event.Shkeptnspecversion) require.Equal(t, defaultSpecVersion, event.Specversion) require.Equal(t, "", event.Triggeredid) + require.Equal(t, "", event.Gitcommitid) require.Equal(t, strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), event.Type) } @@ -289,6 +291,7 @@ func TestCreateKeptnEvent(t *testing.T) { WithID("my-id"). WithKeptnContext("my-keptn-context"). WithTriggeredID("my-triggered-id"). + WithGitCommitID("my-commit-id"). WithKeptnSpecVersion("2.0"). Build() @@ -301,6 +304,7 @@ func TestCreateKeptnEvent(t *testing.T) { require.Equal(t, strutils.Stringp("source"), event.Source) require.Equal(t, "my-keptn-context", event.Shkeptncontext) require.Equal(t, "my-triggered-id", event.Triggeredid) + require.Equal(t, "my-commit-id", event.Gitcommitid) require.Equal(t, strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), event.Type) } @@ -319,6 +323,7 @@ func TestToCloudEvent(t *testing.T) { expected.SetSpecVersion(defaultSpecVersion) expected.SetExtension(keptnContextCEExtension, "my-keptn-context") expected.SetExtension(triggeredIDCEExtension, "my-triggered-id") + expected.SetExtension(keptnGitCommitIDCEExtension, "git-commit-id") expected.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) keptnEvent := models.KeptnContextExtendedCE{ @@ -330,6 +335,7 @@ func TestToCloudEvent(t *testing.T) { Shkeptnspecversion: config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion, Specversion: defaultSpecVersion, Triggeredid: "my-triggered-id", + Gitcommitid: "git-commit-id", Type: strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), } cloudevent := ToCloudEvent(keptnEvent) @@ -348,12 +354,13 @@ func TestToKeptnEvent(t *testing.T) { Data: map[string]interface{}{"content": "testdata"}, ID: "my-id", Shkeptncontext: "my-keptn-context", - Source: strutils.Stringp("my-source"), Shkeptnspecversion: config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion, + Source: strutils.Stringp("my-source"), Specversion: defaultSpecVersion, + Time: time.Time{}, Triggeredid: "my-triggered-id", + Gitcommitid: "my-commit-id", Type: strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), - Time: time.Time{}, } ce := cloudevents.NewEvent() @@ -365,6 +372,7 @@ func TestToKeptnEvent(t *testing.T) { ce.SetData(cloudevents.ApplicationJSON, TestData{Content: "testdata"}) ce.SetExtension(keptnContextCEExtension, "my-keptn-context") ce.SetExtension(triggeredIDCEExtension, "my-triggered-id") + ce.SetExtension(keptnGitCommitIDCEExtension, "my-commit-id") ce.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) keptnEvent, err := ToKeptnEvent(ce) diff --git a/pkg/lib/v0_2_0/keptn.go b/pkg/lib/v0_2_0/keptn.go index 83297f3a..568c2e6a 100644 --- a/pkg/lib/v0_2_0/keptn.go +++ b/pkg/lib/v0_2_0/keptn.go @@ -3,13 +3,14 @@ package v0_2_0 import ( "errors" "fmt" + "log" + cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/google/uuid" "github.com/keptn/go-utils/config" api "github.com/keptn/go-utils/pkg/api/utils" "github.com/keptn/go-utils/pkg/lib/keptn" "gopkg.in/yaml.v3" - "log" ) type Keptn struct { @@ -162,7 +163,7 @@ func (k *Keptn) sendEventWithBaseEventContext(data keptn.EventProperties, source return ce.ID(), nil } -// createCloudEventWithContextAndPayload initializes a new CloudEvent and ensures that context attributes such as the triggeredID, keptnContext, +// createCloudEventWithContextAndPayload initializes a new CloudEvent and ensures that context attributes such as the triggeredID, keptnContext, gitcommitid // as well as project, stage, service and labels are included in the resulting event func (k *Keptn) createCloudEventWithContextAndPayload(outEventType string, keptnContext interface{}, source string, data keptn.EventProperties) (*cloudevents.Event, error) { ce := cloudevents.NewEvent() @@ -180,6 +181,11 @@ func (k *Keptn) createCloudEventWithContextAndPayload(outEventType string, keptn ce.SetExtension(keptnSpecVersionCEExtension, keptnSpecVersion) } + // if available, add the gitcommitid extension to the CloudEvent context + if keptnGitCommitID, err := k.CloudEvent.Context.GetExtension(keptnGitCommitIDCEExtension); err == nil && keptnGitCommitID != "" { + ce.SetExtension(keptnGitCommitIDCEExtension, keptnGitCommitID) + } + var eventData keptn.EventProperties if data != nil { eventData = data diff --git a/pkg/lib/v0_2_0/keptn_test.go b/pkg/lib/v0_2_0/keptn_test.go index 7695901b..032b8eef 100644 --- a/pkg/lib/v0_2_0/keptn_test.go +++ b/pkg/lib/v0_2_0/keptn_test.go @@ -2,11 +2,12 @@ package v0_2_0 import ( "errors" + "testing" + cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/keptn/go-utils/pkg/lib/keptn" "github.com/keptn/go-utils/pkg/lib/v0_2_0/fake" "github.com/stretchr/testify/assert" - "testing" ) func Test_ensureContextAttributesAreSet(t *testing.T) { @@ -118,6 +119,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { inputEvent.SetType(GetTriggeredEventType(EvaluationTaskName)) inputEvent.SetExtension(keptnContextCEExtension, "my-context") inputEvent.SetExtension(keptnSpecVersionCEExtension, "0.2.0") + inputEvent.SetExtension(keptnGitCommitIDCEExtension, "my-commit-id") inputEvent.SetID("my-triggered-id") inputEvent.SetDataContentType(cloudevents.ApplicationJSON) inputEvent.SetData(cloudevents.ApplicationJSON, &EventData{ @@ -140,6 +142,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { eventType string keptnContext string triggeredID string + gitCommitID string keptnSpecVersion string eventData keptn.EventProperties } @@ -168,6 +171,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { eventType: GetStartedEventType(EvaluationTaskName), keptnContext: "my-context", triggeredID: "my-triggered-id", + gitCommitID: "my-commit-id", keptnSpecVersion: "0.2.0", eventData: &EventData{ Project: "my-project", @@ -197,6 +201,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { eventType: GetStatusChangedEventType(EvaluationTaskName), keptnContext: "my-context", triggeredID: "my-triggered-id", + gitCommitID: "my-commit-id", keptnSpecVersion: "0.2.0", eventData: &EventData{ Project: "my-project", @@ -226,6 +231,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { eventType: GetFinishedEventType(EvaluationTaskName), keptnContext: "my-context", triggeredID: "my-triggered-id", + gitCommitID: "my-commit-id", keptnSpecVersion: "0.2.0", eventData: &EventData{ Project: "my-project", @@ -258,6 +264,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { eventType: GetFinishedEventType(EvaluationTaskName), keptnContext: "my-context", triggeredID: "my-triggered-id", + gitCommitID: "my-commit-id", keptnSpecVersion: "0.2.0", eventData: &EventData{ Project: "my-project", @@ -356,6 +363,9 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) { triggeredID, err := event.Context.GetExtension(triggeredIDCEExtension) assert.Nil(t, err) assert.Equal(t, triggeredID.(string), tt.wantEvents[index].triggeredID) + gitCommitID, err := event.Context.GetExtension(keptnGitCommitIDCEExtension) + assert.Nil(t, err) + assert.Equal(t, gitCommitID.(string), tt.wantEvents[index].gitCommitID) keptnContext, err := event.Context.GetExtension(keptnContextCEExtension) assert.Nil(t, err) assert.Equal(t, keptnContext.(string), tt.wantEvents[index].keptnContext)