Skip to content

Commit

Permalink
chore(db): Write git author in esl metadata and pass it to transforme…
Browse files Browse the repository at this point in the history
…rs (#1700)

The cd-services writes git author name & email in the metadata field of
esl json. The manifest-export service reads it and passes it to
transformers, and uses it for creating git commits.
  • Loading branch information
AminSlk authored Jun 27, 2024
1 parent 54269be commit eea7593
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 70 deletions.
36 changes: 33 additions & 3 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ const (

// ESL EVENTS

type ESLMetadata struct {
AuthorName string `json:"authorName"`
AuthorEmail string `json:"authorEmail"`
}

// DBWriteEslEventInternal writes one event to the event-sourcing-light table, taking arbitrary data as input
func (h *DBHandler) DBWriteEslEventInternal(ctx context.Context, eventType EventType, tx *sql.Tx, data interface{}) error {
func (h *DBHandler) DBWriteEslEventInternal(ctx context.Context, eventType EventType, tx *sql.Tx, data interface{}, metadata ESLMetadata) error {
if h == nil {
return nil
}
Expand All @@ -256,9 +261,18 @@ func (h *DBHandler) DBWriteEslEventInternal(ctx context.Context, eventType Event
span, _ := tracer.StartSpanFromContext(ctx, "DBWriteEslEventInternal")
defer span.Finish()

jsonToInsert, err := json.Marshal(data)
dataMap, err := convertObjectToMap(data)
if err != nil {
return fmt.Errorf("could not marshal json data: %w", err)
return fmt.Errorf("could not convert object to map: %w", err)
}
metadataMap, err := convertObjectToMap(metadata)
if err != nil {
return fmt.Errorf("could not convert object to map: %w", err)
}
dataMap["metadata"] = metadataMap
jsonToInsert, err := json.Marshal(dataMap)
if err != nil {
return fmt.Errorf("could not marshal combined json data: %w", err)
}

insertQuery := h.AdaptQuery("INSERT INTO event_sourcing_light (created, event_type , json) VALUES (?, ?, ?);")
Expand All @@ -276,6 +290,22 @@ func (h *DBHandler) DBWriteEslEventInternal(ctx context.Context, eventType Event
return nil
}

func convertObjectToMap(obj interface{}) (map[string]interface{}, error) {
if obj == nil {
return map[string]interface{}{}, nil
}
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
var result = make(map[string]interface{})
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return result, nil
}

type EslEventRow struct {
EslId EslId
Created time.Time
Expand Down
102 changes: 102 additions & 0 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package db
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -1434,6 +1435,107 @@ func TestReadWriteEnvironment(t *testing.T) {
})
}
}
func TestReadWriteEslEvent(t *testing.T) {
const envName = "dev"
const appName = "my-app"
const lockId = "ui-v2-ke1up"
const message = "test"
const authorName = "testauthor"
const authorEmail = "[email protected]"
const evtType = EvtCreateApplicationVersion
expectedJson, _ := json.Marshal(map[string]interface{}{
"env": envName,
"app": appName,
"lockId": lockId,
"message": message,
"metadata": map[string]string{
"authorEmail": authorEmail,
"authorName": authorName,
},
})
expectedJsonWithoutMetadata, _ := json.Marshal(map[string]interface{}{
"env": envName,
"app": appName,
"lockId": lockId,
"message": message,
"metadata": map[string]string{
"authorEmail": "",
"authorName": "",
},
})

tcs := []struct {
Name string
EventType EventType
EventData *map[string]string
EventMetadata ESLMetadata
ExpectedEsl EslEventRow
}{
{
Name: "Write and read",
EventType: evtType,
EventData: &map[string]string{
"env": envName,
"app": appName,
"lockId": lockId,
"message": message,
},
EventMetadata: ESLMetadata{
AuthorName: authorName,
AuthorEmail: authorEmail,
},
ExpectedEsl: EslEventRow{
EventType: evtType,
EventJson: string(expectedJson),
},
},
{
Name: "Write and read with nil metadata",
EventType: evtType,
EventData: &map[string]string{
"env": envName,
"app": appName,
"lockId": lockId,
"message": message,
},
ExpectedEsl: EslEventRow{
EventType: evtType,
EventJson: string(expectedJsonWithoutMetadata),
},
},
}

for _, tc := range tcs {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
ctx := testutil.MakeTestContext()
dbHandler := setupDB(t)
err := dbHandler.WithTransaction(ctx, func(ctx context.Context, transaction *sql.Tx) error {
err := dbHandler.DBWriteEslEventInternal(ctx, tc.EventType, transaction, tc.EventData, tc.EventMetadata)
if err != nil {
return err
}

actual, err := dbHandler.DBReadEslEventInternal(ctx, transaction, true)
if err != nil {
return err
}

if diff := cmp.Diff(tc.ExpectedEsl.EventType, actual.EventType); diff != "" {
t.Fatalf("event type mismatch (-want, +got):\n%s", diff)
}
if diff := cmp.Diff(tc.ExpectedEsl.EventJson, actual.EventJson); diff != "" {
t.Fatalf("event json mismatch (-want, +got):\n%s", diff)
}
return nil
})
if err != nil {
t.Fatalf("transaction error: %v", err)
}
})
}
}

func TestReadWriteAllEnvironments(t *testing.T) {
type TestCase struct {
Expand Down
15 changes: 14 additions & 1 deletion services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,20 @@ func (r *repository) ApplyTransformersInternal(ctx context.Context, transaction
return nil, nil, nil, &applyErr
}
logger.FromContext(ctx).Info("writing esl event...")
err = r.DB.DBWriteEslEventInternal(ctx, t.GetDBEventType(), transaction, t)

user, readUserErr := auth.ReadUserFromContext(ctx)

if readUserErr != nil {
return nil, nil, nil, &TransformerBatchApplyError{
TransformerError: readUserErr,
Index: -1,
}
}
eventMetadata := db.ESLMetadata{
AuthorName: user.Name,
AuthorEmail: user.Email,
}
err = r.DB.DBWriteEslEventInternal(ctx, t.GetDBEventType(), transaction, t, eventMetadata)
if err != nil {
return nil, nil, nil, &TransformerBatchApplyError{
TransformerError: err,
Expand Down
27 changes: 17 additions & 10 deletions services/manifest-repo-export-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/argocd"
"github.com/freiheit-com/kuberpult/pkg/config"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/mapper"
time2 "github.com/freiheit-com/kuberpult/pkg/time"
"io"
"os"
"path/filepath"
Expand All @@ -37,6 +32,12 @@ import (
"sync"
"time"

"github.com/freiheit-com/kuberpult/pkg/argocd"
"github.com/freiheit-com/kuberpult/pkg/config"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/mapper"
time2 "github.com/freiheit-com/kuberpult/pkg/time"

"github.com/freiheit-com/kuberpult/pkg/grpc"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -546,8 +547,8 @@ func CombineArray(others []*TransformerResult) *TransformerResult {
return r
}

func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx, transformers ...Transformer) (*TransformerResult, *TransformerBatchApplyError) {
commitMsg, state, changes, applyErr := r.ApplyTransformersInternal(ctx, transaction, transformers...)
func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx, transformer Transformer) (*TransformerResult, *TransformerBatchApplyError) {
commitMsg, state, changes, applyErr := r.ApplyTransformersInternal(ctx, transaction, transformer)
if applyErr != nil {
return nil, applyErr
}
Expand All @@ -565,10 +566,16 @@ func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx,
When: time.Now(),
}

// TODO this will be handled in Ref SRX-PA568W
transformerMetadata := transformer.GetMetadata()
if transformerMetadata.AuthorEmail == "" || transformerMetadata.AuthorName == "" {
return nil, &TransformerBatchApplyError{
TransformerError: fmt.Errorf("transformer metadata is empty"),
Index: -1,
}
}
user := auth.User{
Email: "[email protected]",
Name: "invalid",
Email: transformerMetadata.AuthorEmail,
Name: transformerMetadata.AuthorName,
DexAuthContext: nil,
}

Expand Down
Loading

0 comments on commit eea7593

Please sign in to comment.