Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat: Propagate git commit ID for sequence in CloudEvent context (#374)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Jan 18, 2022
1 parent 5c52509 commit fa37290
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
3 changes: 3 additions & 0 deletions pkg/api/models/keptn_context_extended_c_e.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 5 additions & 2 deletions pkg/api/models/keptn_context_extended_c_e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,6 +25,7 @@ func TestKeptnContextExtendedCE_Validate(t *testing.T) {
Specversion string
Time time.Time
Triggeredid string
Gitcommitid string
Type *string
}
tests := []struct {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/api/models/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/lib/v0_2_0/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)

Expand All @@ -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()),
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/lib/v0_2_0/events_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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"
"github.com/keptn/go-utils/pkg/common/strutils"
"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"
)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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()

Expand All @@ -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)
}

Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions pkg/lib/v0_2_0/keptn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion pkg/lib/v0_2_0/keptn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand All @@ -140,6 +142,7 @@ func TestKeptn_SendEventConvenienceFunctions(t *testing.T) {
eventType string
keptnContext string
triggeredID string
gitCommitID string
keptnSpecVersion string
eventData keptn.EventProperties
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fa37290

Please sign in to comment.